From da96767275e488d775973347162be74ca67eedb9 Mon Sep 17 00:00:00 2001 From: evgeniy-scherbina Date: Mon, 9 Oct 2023 13:56:10 -0400 Subject: [PATCH] TMP Commit --- service/cachemdw/cache_test.go | 75 ++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 3 deletions(-) diff --git a/service/cachemdw/cache_test.go b/service/cachemdw/cache_test.go index 3463efa..4d0a920 100644 --- a/service/cachemdw/cache_test.go +++ b/service/cachemdw/cache_test.go @@ -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" @@ -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) }