github.com/ava-labs/avalanchego@v1.11.11/cache/lru_cache_test.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package cache_test 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/require" 10 11 "github.com/ava-labs/avalanchego/cache/cachetest" 12 "github.com/ava-labs/avalanchego/ids" 13 14 . "github.com/ava-labs/avalanchego/cache" 15 ) 16 17 func TestLRU(t *testing.T) { 18 cache := &LRU[ids.ID, int64]{Size: 1} 19 20 cachetest.TestBasic(t, cache) 21 } 22 23 func TestLRUEviction(t *testing.T) { 24 cache := &LRU[ids.ID, int64]{Size: 2} 25 26 cachetest.TestEviction(t, cache) 27 } 28 29 func TestLRUResize(t *testing.T) { 30 require := require.New(t) 31 cache := LRU[ids.ID, int64]{Size: 2} 32 33 id1 := ids.ID{1} 34 id2 := ids.ID{2} 35 36 expectedVal1 := int64(1) 37 expectedVal2 := int64(2) 38 cache.Put(id1, expectedVal1) 39 cache.Put(id2, expectedVal2) 40 41 val, found := cache.Get(id1) 42 require.True(found) 43 require.Equal(expectedVal1, val) 44 45 val, found = cache.Get(id2) 46 require.True(found) 47 require.Equal(expectedVal2, val) 48 49 cache.Size = 1 50 // id1 evicted 51 52 _, found = cache.Get(id1) 53 require.False(found) 54 55 val, found = cache.Get(id2) 56 require.True(found) 57 require.Equal(expectedVal2, val) 58 59 cache.Size = 0 60 // We reset the size to 1 in resize 61 62 _, found = cache.Get(id1) 63 require.False(found) 64 65 val, found = cache.Get(id2) 66 require.True(found) 67 require.Equal(expectedVal2, val) 68 }