github.com/vipernet-xyz/tm@v0.34.24/mempool/cache_test.go (about)

     1  package mempool
     2  
     3  import (
     4  	"crypto/rand"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestCacheRemove(t *testing.T) {
    11  	cache := NewLRUTxCache(100)
    12  	numTxs := 10
    13  
    14  	txs := make([][]byte, numTxs)
    15  	for i := 0; i < numTxs; i++ {
    16  		// probability of collision is 2**-256
    17  		txBytes := make([]byte, 32)
    18  		_, err := rand.Read(txBytes)
    19  		require.NoError(t, err)
    20  
    21  		txs[i] = txBytes
    22  		cache.Push(txBytes)
    23  
    24  		// make sure its added to both the linked list and the map
    25  		require.Equal(t, i+1, len(cache.cacheMap))
    26  		require.Equal(t, i+1, cache.list.Len())
    27  	}
    28  
    29  	for i := 0; i < numTxs; i++ {
    30  		cache.Remove(txs[i])
    31  		// make sure its removed from both the map and the linked list
    32  		require.Equal(t, numTxs-(i+1), len(cache.cacheMap))
    33  		require.Equal(t, numTxs-(i+1), cache.list.Len())
    34  	}
    35  }