cosmos-sdk/store/cache/cache_test.go
cool-developer 3e18f4088b
feat: update store module for new iavl (#15568)
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Marko <marbar3778@yahoo.com>
2023-06-06 19:08:35 +00:00

101 lines
2.8 KiB
Go

package cache_test
import (
"fmt"
"testing"
dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/iavl"
"github.com/stretchr/testify/require"
"cosmossdk.io/log"
"cosmossdk.io/store/cache"
"cosmossdk.io/store/cachekv"
iavlstore "cosmossdk.io/store/iavl"
"cosmossdk.io/store/types"
)
func TestGetOrSetStoreCache(t *testing.T) {
db := dbm.NewMemDB()
mngr := cache.NewCommitKVStoreCacheManager(cache.DefaultCommitKVStoreCacheSize)
sKey := types.NewKVStoreKey("test")
tree := iavl.NewMutableTree(db, 100, false, log.NewNopLogger())
store := iavlstore.UnsafeNewStore(tree)
store2 := mngr.GetStoreCache(sKey, store)
require.NotNil(t, store2)
require.Equal(t, store2, mngr.GetStoreCache(sKey, store))
}
func TestUnwrap(t *testing.T) {
db := dbm.NewMemDB()
mngr := cache.NewCommitKVStoreCacheManager(cache.DefaultCommitKVStoreCacheSize)
sKey := types.NewKVStoreKey("test")
tree := iavl.NewMutableTree(db, 100, false, log.NewNopLogger())
store := iavlstore.UnsafeNewStore(tree)
_ = mngr.GetStoreCache(sKey, store)
require.Equal(t, store, mngr.Unwrap(sKey))
require.Nil(t, mngr.Unwrap(types.NewKVStoreKey("test2")))
}
func TestStoreCache(t *testing.T) {
db := dbm.NewMemDB()
mngr := cache.NewCommitKVStoreCacheManager(cache.DefaultCommitKVStoreCacheSize)
sKey := types.NewKVStoreKey("test")
tree := iavl.NewMutableTree(db, 100, false, log.NewNopLogger())
store := iavlstore.UnsafeNewStore(tree)
kvStore := mngr.GetStoreCache(sKey, store)
for i := uint(0); i < cache.DefaultCommitKVStoreCacheSize*2; i++ {
key := []byte(fmt.Sprintf("key_%d", i))
value := []byte(fmt.Sprintf("value_%d", i))
kvStore.Set(key, value)
res := kvStore.Get(key)
require.Equal(t, res, value)
require.Equal(t, res, store.Get(key))
kvStore.Delete(key)
require.Nil(t, kvStore.Get(key))
require.Nil(t, store.Get(key))
}
}
func TestReset(t *testing.T) {
db := dbm.NewMemDB()
mngr := cache.NewCommitKVStoreCacheManager(cache.DefaultCommitKVStoreCacheSize)
sKey := types.NewKVStoreKey("test")
tree := iavl.NewMutableTree(db, 100, false, log.NewNopLogger())
store := iavlstore.UnsafeNewStore(tree)
store2 := mngr.GetStoreCache(sKey, store)
require.NotNil(t, store2)
require.Equal(t, store2, mngr.GetStoreCache(sKey, store))
// reset and check if the cache is gone
mngr.Reset()
require.Nil(t, mngr.Unwrap(sKey))
// check if the cache is recreated
require.Equal(t, store2, mngr.GetStoreCache(sKey, store))
}
func TestCacheWrap(t *testing.T) {
db := dbm.NewMemDB()
mngr := cache.NewCommitKVStoreCacheManager(cache.DefaultCommitKVStoreCacheSize)
sKey := types.NewKVStoreKey("test")
tree := iavl.NewMutableTree(db, 100, false, log.NewNopLogger())
store := iavlstore.UnsafeNewStore(tree)
cacheWrapper := mngr.GetStoreCache(sKey, store).CacheWrap()
require.IsType(t, &cachekv.Store{}, cacheWrapper)
}