Skip to content

Commit

Permalink
TMP Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed Oct 9, 2023
1 parent 887ee0d commit da96767
Showing 1 changed file with 72 additions and 3 deletions.
75 changes: 72 additions & 3 deletions service/cachemdw/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cachemdw

import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/common"
ethctypes "github.com/ethereum/go-ethereum/core/types"
"github.com/kava-labs/kava-proxy-service/clients/cache"
Expand Down Expand Up @@ -80,7 +79,77 @@ func TestUnitTestCacheQueryResponse(t *testing.T) {
Method: "eth_getBalance",
Params: []interface{}{"0x1234", "42"},
}
resp, err := serviceCache.GetCachedQueryResponse(context.Background(), "api.kava.io", &req)
resp, found := serviceCache.GetCachedQueryResponse(context.Background(), "api.kava.io", &req)
require.False(t, found)
require.Empty(t, resp)

fmt.Printf("resp: %v, err: %v\n", resp, err)
err := serviceCache.CacheQueryResponse(context.Background(), "1", &req, []byte("resp"))
require.NoError(t, err)

resp, found = serviceCache.GetCachedQueryResponse(context.Background(), "api.kava.io", &req)
require.True(t, found)
require.Equal(t, []byte("resp"), resp)
}

func TestUnitTestValidateAndCacheQueryResponse(t *testing.T) {
inMemoryCache := cache.NewInMemoryCache()
evmClient := NewMockEVMClient()
cacheTTL := time.Hour

serviceCache := NewServiceCache(inMemoryCache, evmClient, cacheTTL, service.DecodedRequestContextKey)

req := decode.EVMRPCRequestEnvelope{
JSONRPCVersion: "2.0",
ID: 1,
Method: "eth_getBalance",
Params: []interface{}{"0x1234", "42"},
}
resp, found := serviceCache.GetCachedQueryResponse(context.Background(), "api.kava.io", &req)
require.False(t, found)
require.Empty(t, resp)

err := serviceCache.ValidateAndCacheQueryResponse(context.Background(), &req, "api.kava.io", []byte("resp"))
require.NoError(t, err)

resp, found = serviceCache.GetCachedQueryResponse(context.Background(), "api.kava.io", &req)
require.True(t, found)
require.Equal(t, []byte("resp"), resp)
}

func TestUnitTestGetCachedChainID(t *testing.T) {
inMemoryCache := cache.NewInMemoryCache()
evmClient := NewMockEVMClient()
cacheTTL := time.Hour

serviceCache := NewServiceCache(inMemoryCache, evmClient, cacheTTL, service.DecodedRequestContextKey)

resp, found := serviceCache.GetCachedChainID(context.Background(), "api.kava.io")
require.False(t, found)
require.Empty(t, resp)

err := serviceCache.CacheChainID(context.Background(), "api.kava.io", "1")
require.NoError(t, err)

resp, found = serviceCache.GetCachedChainID(context.Background(), "api.kava.io")
require.True(t, found)
require.Equal(t, "1", resp)
}

func TestUnitTestGetAndCacheChainID(t *testing.T) {
inMemoryCache := cache.NewInMemoryCache()
evmClient := NewMockEVMClient()
cacheTTL := time.Hour

serviceCache := NewServiceCache(inMemoryCache, evmClient, cacheTTL, service.DecodedRequestContextKey)

resp, err := serviceCache.GetAndCacheChainID(context.Background(), "api.kava.io")
require.NoError(t, err)
require.Equal(t, "1", resp)

//err := serviceCache.CacheChainID(context.Background(), "api.kava.io", "1")
//require.NoError(t, err)

resp, found := serviceCache.GetCachedChainID(context.Background(), "api.kava.io")
require.True(t, found)
require.Equal(t, "1", resp)
}

0 comments on commit da96767

Please sign in to comment.