github.com/ethersphere/bee/v2@v2.2.0/pkg/skippeers/skippeers_test.go (about) 1 // Copyright 2023 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package skippeers_test 6 7 import ( 8 "testing" 9 "time" 10 11 "github.com/ethersphere/bee/v2/pkg/skippeers" 12 "github.com/ethersphere/bee/v2/pkg/swarm" 13 ) 14 15 func TestPruneExpiresAfter(t *testing.T) { 16 t.Parallel() 17 18 skipList := skippeers.NewList() 19 t.Cleanup(func() { skipList.Close() }) 20 21 chunk := swarm.RandAddress(t) 22 peer1 := swarm.RandAddress(t) 23 peer2 := swarm.RandAddress(t) 24 25 skipList.Add(chunk, peer1, time.Millisecond*10) 26 skipList.Add(swarm.RandAddress(t), peer1, time.Millisecond*10) 27 if !swarm.ContainsAddress(skipList.ChunkPeers(chunk), peer1) { 28 t.Fatal("peer should be in skiplist") 29 } 30 31 skipList.Add(chunk, peer2, time.Millisecond*10) 32 if skipList.PruneExpiresAfter(chunk, time.Millisecond) > 0 { 33 t.Fatal("entry should NOT be pruned") 34 } 35 36 skipList.PruneExpiresAfter(chunk, time.Millisecond) 37 if len(skipList.ChunkPeers(chunk)) == 0 { 38 t.Fatal("entry should NOT be pruned") 39 } 40 41 if len(skipList.ChunkPeers(swarm.RandAddress(t))) != 0 { 42 t.Fatal("there should be no entry") 43 } 44 45 if skipList.PruneExpiresAfter(chunk, time.Millisecond*10) == 0 { 46 t.Fatal("entry should be pruned") 47 } 48 49 if len(skipList.ChunkPeers(chunk)) != 0 { 50 t.Fatal("entry should be pruned") 51 } 52 53 if len(skipList.ChunkPeers(swarm.RandAddress(t))) != 0 { 54 t.Fatal("there should be no entry") 55 } 56 } 57 58 func TestPeerWait(t *testing.T) { 59 t.Parallel() 60 61 skipList := skippeers.NewList() 62 t.Cleanup(func() { skipList.Close() }) 63 64 chunk1 := swarm.RandAddress(t) 65 chunk2 := swarm.RandAddress(t) 66 peer1 := swarm.RandAddress(t) 67 peer2 := swarm.RandAddress(t) 68 peer3 := swarm.RandAddress(t) 69 70 skipList.Add(chunk1, peer1, time.Millisecond*100) 71 if !swarm.ContainsAddress(skipList.ChunkPeers(chunk1), peer1) { 72 t.Fatal("peer should be in skiplist") 73 } 74 75 skipList.Add(chunk2, peer1, time.Millisecond*150) 76 if !swarm.ContainsAddress(skipList.ChunkPeers(chunk2), peer1) { 77 t.Fatal("peer should be in skiplist") 78 } 79 80 skipList.Add(chunk1, peer2, time.Millisecond*50) 81 if !swarm.ContainsAddress(skipList.ChunkPeers(chunk1), peer2) { 82 t.Fatal("peer should be in skiplist") 83 } 84 85 skipList.Add(chunk1, peer3, -time.Millisecond*50) 86 if swarm.ContainsAddress(skipList.ChunkPeers(chunk1), peer3) { 87 t.Fatal("peer should NOT be in skiplist") 88 } 89 90 time.Sleep(time.Millisecond * 60) 91 92 if len(skipList.ChunkPeers(chunk1)) != 1 || !swarm.ContainsAddress(skipList.ChunkPeers(chunk1), peer1) { 93 t.Fatal("peer should be in skiplist") 94 } 95 96 time.Sleep(time.Millisecond * 60) 97 98 if len(skipList.ChunkPeers(chunk1)) != 0 { 99 t.Fatal("entry should be pruned") 100 } 101 102 time.Sleep(time.Millisecond * 60) 103 104 if len(skipList.ChunkPeers(chunk2)) != 0 { 105 t.Fatal("entry should be pruned") 106 } 107 }