github.com/klaytn/klaytn@v1.12.1/storage/statedb/cache_hybrid_test.go (about)

     1  package statedb
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/klaytn/klaytn/storage"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func getTestHybridConfig() *TrieNodeCacheConfig {
    13  	return &TrieNodeCacheConfig{
    14  		CacheType:          CacheTypeHybrid,
    15  		LocalCacheSizeMiB:  100,
    16  		FastCacheFileDir:   "",
    17  		RedisEndpoints:     []string{"localhost:6379"},
    18  		RedisClusterEnable: false,
    19  	}
    20  }
    21  
    22  // TestHybridCache_Set tests whether a hybrid cache can set an item into both of local and remote caches.
    23  func TestHybridCache_Set(t *testing.T) {
    24  	storage.SkipLocalTest(t)
    25  
    26  	cache, err := newHybridCache(getTestHybridConfig())
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  
    31  	// Set an item
    32  	key, value := randBytes(32), randBytes(500)
    33  	cache.Set(key, value)
    34  	time.Sleep(sleepDurationForAsyncBehavior)
    35  
    36  	// Type assertion to check both of local cache and remote cache
    37  	hybrid, ok := cache.(*HybridCache)
    38  	assert.Equal(t, ok, true)
    39  
    40  	// Check whether the item is set in the local cache
    41  	localVal := hybrid.local.Get(key)
    42  	assert.Equal(t, bytes.Compare(localVal, value), 0)
    43  
    44  	// Check whether the item is set in the remote cache
    45  	remoteVal := hybrid.remote.Get(key)
    46  	assert.Equal(t, bytes.Compare(remoteVal, value), 0)
    47  }
    48  
    49  // TestHybridCache_Get tests whether a hybrid cache can get an item from both of local and remote caches.
    50  func TestHybridCache_Get(t *testing.T) {
    51  	storage.SkipLocalTest(t)
    52  
    53  	// Prepare caches to be integrated with a hybrid cache
    54  	localCache := newFastCache(getTestHybridConfig())
    55  	remoteCache, err := newRedisCache(getTestHybridConfig())
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	var hybrid TrieNodeCache = &HybridCache{
    61  		local:  localCache,
    62  		remote: remoteCache,
    63  	}
    64  
    65  	// Test local cache of the hybrid cache
    66  	{
    67  		// Store an item into local cache
    68  		key, value := randBytes(32), randBytes(500)
    69  		localCache.Set(key, value)
    70  
    71  		// Get the item from the hybrid cache and check the validity
    72  		returnedVal := hybrid.Get(key)
    73  		assert.Equal(t, bytes.Compare(returnedVal, value), 0)
    74  	}
    75  
    76  	// Test remote cache of the hybrid cache
    77  	{
    78  		// Store an item into remote cache
    79  		key, value := randBytes(32), randBytes(500)
    80  		remoteCache.SetAsync(key, value)
    81  		time.Sleep(sleepDurationForAsyncBehavior)
    82  
    83  		// Make sure the item is not stored in the local cache.
    84  		assert.Equal(t, len(localCache.Get(key)), 0)
    85  
    86  		// Get the item from the hybrid cache and check the validity
    87  		returnedVal := hybrid.Get(key)
    88  		assert.Equal(t, bytes.Compare(returnedVal, value), 0)
    89  
    90  		// Make sure that the item retrieved from the remote cache is also stored in the local cache
    91  		assert.Equal(t, bytes.Compare(localCache.Get(key), value), 0)
    92  	}
    93  
    94  	// Test the priority of local and remote caches
    95  	{
    96  		// Store an item into the remote cache
    97  		key, value := randBytes(32), randBytes(500)
    98  		localCache.Set(key, value)
    99  		remoteCache.SetAsync(key, []byte{0x11})
   100  		time.Sleep(sleepDurationForAsyncBehavior)
   101  
   102  		// Get the item from the hybrid cache and check the validity
   103  		returnedVal := hybrid.Get(key)
   104  		assert.Equal(t, bytes.Compare(returnedVal, value), 0)
   105  	}
   106  }
   107  
   108  // TestHybridCache_Has tests whether a hybrid cache can check an item from both of local and remote caches.
   109  func TestHybridCache_Has(t *testing.T) {
   110  	storage.SkipLocalTest(t)
   111  
   112  	// Prepare caches to be integrated with a hybrid cache
   113  	localCache := newFastCache(getTestHybridConfig())
   114  	remoteCache, err := newRedisCache(getTestHybridConfig())
   115  	if err != nil {
   116  		t.Fatal(err)
   117  	}
   118  
   119  	var hybrid TrieNodeCache = &HybridCache{
   120  		local:  localCache,
   121  		remote: remoteCache,
   122  	}
   123  
   124  	// Test local cache of the hybrid cache
   125  	{
   126  		// Store an item into local cache
   127  		key, value := randBytes(32), randBytes(500)
   128  		localCache.Set(key, value)
   129  
   130  		// Get the item from the hybrid cache and check the validity
   131  		returnedVal, returnedExist := hybrid.Has(key)
   132  		assert.Equal(t, bytes.Compare(returnedVal, value), 0)
   133  		assert.Equal(t, returnedExist, true)
   134  	}
   135  
   136  	// Test remote cache of the hybrid cache
   137  	{
   138  		// Store an item into remote cache
   139  		key, value := randBytes(32), randBytes(500)
   140  		remoteCache.SetAsync(key, value)
   141  		time.Sleep(sleepDurationForAsyncBehavior)
   142  
   143  		// Get the item from the hybrid cache and check the validity
   144  		returnedVal, returnedExist := hybrid.Has(key)
   145  		assert.Equal(t, bytes.Compare(returnedVal, value), 0)
   146  		assert.Equal(t, returnedExist, true)
   147  	}
   148  
   149  	// Test the priority of local and remote caches
   150  	{
   151  		// Store an item into the remote cache
   152  		key, value := randBytes(32), randBytes(500)
   153  		localCache.Set(key, value)
   154  		remoteCache.SetAsync(key, []byte{0x11})
   155  		time.Sleep(sleepDurationForAsyncBehavior)
   156  
   157  		// Get the item from the hybrid cache and check the validity
   158  		returnedVal, returnedExist := hybrid.Has(key)
   159  		assert.Equal(t, bytes.Compare(returnedVal, value), 0)
   160  		assert.Equal(t, returnedExist, true)
   161  	}
   162  }