github.com/Finschia/ostracon@v1.1.5/mempool/cache_test.go (about) 1 package mempool 2 3 import ( 4 "crypto/rand" 5 "testing" 6 7 "github.com/Finschia/ostracon/types" 8 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestCacheBasic(t *testing.T) { 13 size := 10 14 cache := NewLRUTxCache(size) 15 require.Equal(t, 0, cache.GetList().Len()) 16 for i := 0; i < size; i++ { 17 cache.Push(types.Tx{byte(i)}) 18 } 19 require.Equal(t, size, cache.GetList().Len()) 20 cache.Push(types.Tx{byte(0)}) // MoveToBack 21 require.Equal(t, size, cache.GetList().Len()) 22 cache.Push(types.Tx{byte(size + 1)}) // Cache out 23 require.Equal(t, size, cache.GetList().Len()) 24 cache.Reset() 25 require.Equal(t, 0, cache.GetList().Len()) 26 } 27 28 func TestCacheRemove(t *testing.T) { 29 cache := NewLRUTxCache(100) 30 numTxs := 10 31 32 txs := make([][]byte, numTxs) 33 for i := 0; i < numTxs; i++ { 34 // probability of collision is 2**-256 35 txBytes := make([]byte, 32) 36 _, err := rand.Read(txBytes) 37 require.NoError(t, err) 38 39 txs[i] = txBytes 40 cache.Push(txBytes) 41 42 // make sure its added to both the linked list and the map 43 require.Equal(t, i+1, len(cache.cacheMap)) 44 require.Equal(t, i+1, cache.list.Len()) 45 } 46 47 for i := 0; i < numTxs; i++ { 48 cache.Remove(txs[i]) 49 // make sure its removed from both the map and the linked list 50 require.Equal(t, numTxs-(i+1), len(cache.cacheMap)) 51 require.Equal(t, numTxs-(i+1), cache.list.Len()) 52 } 53 }