github.com/iotexproject/iotex-core@v1.14.1-rc1/api/read_cache_test.go (about) 1 package api 2 3 import ( 4 "testing" 5 6 "github.com/iotexproject/go-pkgs/hash" 7 "github.com/stretchr/testify/require" 8 ) 9 10 func TestReadKey(t *testing.T) { 11 r := require.New(t) 12 13 var keys []hash.Hash160 14 for _, v := range []ReadKey{ 15 {"staking", "10", []byte("activeBuckets"), [][]byte{[]byte{0, 1}, []byte{2, 3, 4, 5, 6, 7, 8}}}, 16 {"staking", "10", []byte("activeBuckets"), [][]byte{[]byte{0, 1, 2}, []byte{3, 4, 5, 6, 7, 8}}}, 17 {"staking", "10", []byte("activeBuckets"), [][]byte{[]byte{0, 1, 2, 3}, []byte{4, 5, 6, 7, 8}}}, 18 {"staking", "10", []byte("activeBuckets"), [][]byte{[]byte{0, 1, 2, 3, 4, 5}, []byte{6, 7, 8}}}, 19 {"staking", "10", []byte("activeBuckets"), [][]byte{[]byte{0, 1, 2, 3, 4, 5, 6, 7}, []byte{8}}}, 20 } { 21 keys = append(keys, v.Hash()) 22 } 23 24 // all keys are different 25 for i := range keys { 26 k := keys[i] 27 for j := i + 1; j < len(keys); j++ { 28 r.NotEqual(k, keys[j]) 29 } 30 } 31 } 32 33 func TestReadCache(t *testing.T) { 34 r := require.New(t) 35 36 c := NewReadCache() 37 rcTests := []struct { 38 k hash.Hash160 39 v []byte 40 }{ 41 {hash.Hash160b([]byte{1}), []byte{1}}, 42 {hash.Hash160b([]byte{2}), []byte{2}}, 43 {hash.Hash160b([]byte{3}), []byte{1}}, 44 {hash.Hash160b([]byte{4}), []byte{2}}, 45 } 46 for _, v := range rcTests { 47 d, ok := c.Get(v.k) 48 r.False(ok) 49 r.Nil(d) 50 c.Put(v.k, v.v) 51 } 52 53 for _, v := range rcTests { 54 d, ok := c.Get(v.k) 55 r.True(ok) 56 r.Equal(v.v, d) 57 } 58 59 c.Clear() 60 for _, v := range rcTests { 61 d, ok := c.Get(v.k) 62 r.False(ok) 63 r.Nil(d) 64 } 65 }