github.com/klaytn/klaytn@v1.10.2/storage/statedb/cache_test.go (about)

     1  // Copyright 2020 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package statedb
    18  
    19  import (
    20  	"io/ioutil"
    21  	"os"
    22  	"reflect"
    23  	"runtime"
    24  	"testing"
    25  
    26  	"github.com/klaytn/klaytn/common"
    27  
    28  	"github.com/docker/docker/pkg/testutil/assert"
    29  )
    30  
    31  // TestNewTrieNodeCache tests creating all kinds of supported trie node caches.
    32  func TestNewTrieNodeCache(t *testing.T) {
    33  	testCases := []struct {
    34  		cacheConfig  *TrieNodeCacheConfig
    35  		expectedType reflect.Type
    36  		err          error
    37  	}{
    38  		{getTestFastCacheConfig(), reflect.TypeOf(&FastCache{}), nil},
    39  		{getTestRedisConfig(), reflect.TypeOf(&RedisCache{}), nil},
    40  		{getTestHybridConfig(), reflect.TypeOf(&HybridCache{}), nil},
    41  		{nil, nil, errNilTrieNodeCacheConfig},
    42  	}
    43  
    44  	for _, tc := range testCases {
    45  		cache, err := NewTrieNodeCache(tc.cacheConfig)
    46  		assert.Equal(t, err, tc.err)
    47  		assert.Equal(t, reflect.TypeOf(cache), tc.expectedType)
    48  	}
    49  }
    50  
    51  func TestFastCache_SaveAndLoad(t *testing.T) {
    52  	// Create test directory
    53  	dirName, err := ioutil.TempDir(os.TempDir(), "fastcache_saveandload")
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  	defer os.RemoveAll(dirName)
    58  
    59  	// Generate test data
    60  	var keys [][]byte
    61  	var vals [][]byte
    62  	for i := 0; i < 10; i++ {
    63  		keys = append(keys, common.MakeRandomBytes(128))
    64  		vals = append(vals, common.MakeRandomBytes(128))
    65  	}
    66  
    67  	config := getTestHybridConfig()
    68  	config.FastCacheFileDir = dirName
    69  
    70  	// Create a fastcache from the file and save the data to the cache
    71  	fastCache := newFastCache(config)
    72  	for idx, key := range keys {
    73  		assert.DeepEqual(t, fastCache.Get(key), []byte(nil))
    74  		fastCache.Set(key, vals[idx])
    75  		assert.DeepEqual(t, fastCache.Get(key), vals[idx])
    76  	}
    77  	// Save the cache to the file
    78  	assert.NilError(t, fastCache.SaveToFile(dirName, runtime.NumCPU()))
    79  
    80  	// Create a fastcache from the file and check if the data exists
    81  	fastCacheFromFile := newFastCache(config)
    82  	for idx, key := range keys {
    83  		assert.DeepEqual(t, fastCacheFromFile.Get(key), vals[idx])
    84  	}
    85  }