github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/p2p/logging/internal/peerIdCache_test.go (about) 1 package internal_test 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 8 "github.com/onflow/flow-go/network/p2p/logging/internal" 9 "github.com/onflow/flow-go/utils/unittest" 10 ) 11 12 // TestNewPeerIdCache tests the basic functionality of the peer ID cache. It ensures that the cache 13 // is created successfully. 14 func TestNewPeerIdCache(t *testing.T) { 15 cacheSize := 100 16 cache, err := internal.NewPeerIdCache(cacheSize) 17 assert.NoError(t, err) 18 assert.NotNil(t, cache) 19 } 20 21 // TestPeerIdCache_PeerIdString tests the basic functionality of the peer ID cache. It ensures that the cache 22 // returns the same string as the peer.ID.String() method. 23 func TestPeerIdCache_PeerIdString(t *testing.T) { 24 cacheSize := 100 25 cache, err := internal.NewPeerIdCache(cacheSize) 26 assert.NoError(t, err) 27 28 t.Run("existing peer ID", func(t *testing.T) { 29 pid := unittest.PeerIdFixture(t) 30 pidStr := cache.PeerIdString(pid) 31 assert.NotEmpty(t, pidStr) 32 assert.Equal(t, pid.String(), pidStr) 33 34 gotPidStr, ok := cache.ByPeerId(pid) 35 assert.True(t, ok, "expected pid to be in the cache") 36 assert.Equal(t, pid.String(), gotPidStr) 37 }) 38 39 t.Run("non-existing peer ID", func(t *testing.T) { 40 pid1 := unittest.PeerIdFixture(t) 41 pid2 := unittest.PeerIdFixture(t) 42 43 cache.PeerIdString(pid1) 44 pidStr := cache.PeerIdString(pid2) 45 assert.NotEmpty(t, pidStr) 46 assert.Equal(t, pid2.String(), pidStr) 47 48 gotPidStr, ok := cache.ByPeerId(pid2) 49 assert.True(t, ok, "expected pid to be in the cache") 50 assert.Equal(t, pid2.String(), gotPidStr) 51 52 gotPidStr, ok = cache.ByPeerId(pid1) 53 assert.True(t, ok, "expected pid to be in the cache") 54 assert.Equal(t, pid1.String(), gotPidStr) 55 }) 56 } 57 58 // TestPeerIdCache_EjectionScenarios tests the eviction logic of the peer ID cache. It ensures that the cache 59 // evicts the least recently added peer ID when the cache is full. 60 func TestPeerIdCache_EjectionScenarios(t *testing.T) { 61 cacheSize := 3 62 cache, err := internal.NewPeerIdCache(cacheSize) 63 assert.NoError(t, err) 64 assert.Equal(t, 0, cache.Size()) 65 66 // add peer IDs to fill the cache 67 pid1 := unittest.PeerIdFixture(t) 68 pid2 := unittest.PeerIdFixture(t) 69 pid3 := unittest.PeerIdFixture(t) 70 71 cache.PeerIdString(pid1) 72 assert.Equal(t, 1, cache.Size()) 73 cache.PeerIdString(pid2) 74 assert.Equal(t, 2, cache.Size()) 75 cache.PeerIdString(pid3) 76 assert.Equal(t, 3, cache.Size()) 77 78 // check that all peer IDs are in the cache 79 assert.Equal(t, pid1.String(), cache.PeerIdString(pid1)) 80 assert.Equal(t, pid2.String(), cache.PeerIdString(pid2)) 81 assert.Equal(t, pid3.String(), cache.PeerIdString(pid3)) 82 assert.Equal(t, 3, cache.Size()) 83 84 // add a new peer ID 85 pid4 := unittest.PeerIdFixture(t) 86 cache.PeerIdString(pid4) 87 assert.Equal(t, 3, cache.Size()) 88 89 // check that pid1 is now the one that has been evicted 90 gotId1Str, ok := cache.ByPeerId(pid1) 91 assert.False(t, ok, "expected pid1 to be evicted") 92 assert.Equal(t, "", gotId1Str) 93 94 // confirm other peer IDs are still in the cache 95 gotId2Str, ok := cache.ByPeerId(pid2) 96 assert.True(t, ok, "expected pid2 to be in the cache") 97 assert.Equal(t, pid2.String(), gotId2Str) 98 99 gotId3Str, ok := cache.ByPeerId(pid3) 100 assert.True(t, ok, "expected pid3 to be in the cache") 101 assert.Equal(t, pid3.String(), gotId3Str) 102 103 gotId4Str, ok := cache.ByPeerId(pid4) 104 assert.True(t, ok, "expected pid4 to be in the cache") 105 assert.Equal(t, pid4.String(), gotId4Str) 106 }