github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/netsync/chainmgr/peers_test.go (about) 1 package chainmgr 2 3 import ( 4 "testing" 5 ) 6 7 func TestAddDel(t *testing.T) { 8 syncPeers := newFastSyncPeers() 9 peers := make(map[string]bool) 10 peers["Peer1"] = true 11 peers["Peer2"] = true 12 for k := range peers { 13 syncPeers.add(k) 14 syncPeers.add(k) 15 } 16 if syncPeers.size() != len(peers) { 17 t.Errorf("add peer test err: got %d\nwant %d", syncPeers.size(), len(peers)) 18 } 19 20 syncPeers.delete("Peer1") 21 if syncPeers.size() != 1 { 22 t.Errorf("add peer test err: got %d\nwant %d", syncPeers.size(), 1) 23 } 24 25 syncPeers.delete("Peer1") 26 if syncPeers.size() != 1 { 27 t.Errorf("add peer test err: got %d\nwant %d", syncPeers.size(), 1) 28 } 29 } 30 31 func TestIdlePeers(t *testing.T) { 32 syncPeers := newFastSyncPeers() 33 peers := make(map[string]bool) 34 peers["Peer1"] = true 35 peers["Peer2"] = true 36 for k := range peers { 37 syncPeers.add(k) 38 syncPeers.add(k) 39 } 40 41 idlePeers := syncPeers.selectIdlePeers() 42 if len(idlePeers) != len(peers) { 43 t.Errorf("selcet idle peers test err: got %d\nwant %d", len(idlePeers), len(peers)) 44 } 45 46 for _, peer := range idlePeers { 47 if ok := peers[peer]; !ok { 48 t.Errorf("selcet idle peers test err: want peers %v got %v", peers, idlePeers) 49 } 50 } 51 52 idlePeers = syncPeers.selectIdlePeers() 53 if len(idlePeers) != 0 { 54 t.Errorf("selcet idle peers test err: got %d\nwant %d", len(idlePeers), 0) 55 } 56 57 } 58 59 func TestIdlePeer(t *testing.T) { 60 syncPeers := newFastSyncPeers() 61 peers := make(map[string]bool) 62 peers["Peer1"] = true 63 peers["Peer2"] = true 64 for k := range peers { 65 syncPeers.add(k) 66 syncPeers.add(k) 67 } 68 idlePeer, err := syncPeers.selectIdlePeer() 69 if err != nil { 70 t.Errorf("selcet idle peers test err: got %v\nwant %v", err, nil) 71 } 72 73 if ok := peers[idlePeer]; !ok { 74 t.Error("selcet idle peers test err.") 75 } 76 idlePeer, err = syncPeers.selectIdlePeer() 77 if err != nil { 78 t.Errorf("selcet idle peers test err: got %v\nwant %v", err, nil) 79 } 80 81 if ok := peers[idlePeer]; !ok { 82 t.Error("selcet idle peers test err.") 83 } 84 idlePeer, err = syncPeers.selectIdlePeer() 85 if err != errNoValidFastSyncPeer { 86 t.Errorf("selcet idle peers test err: got %v\nwant %v", err, errNoValidFastSyncPeer) 87 } 88 } 89 90 func TestSetIdle(t *testing.T) { 91 syncPeers := newFastSyncPeers() 92 peers := make(map[string]bool) 93 peers["Peer2"] = true 94 for k := range peers { 95 syncPeers.add(k) 96 } 97 if syncPeers.size() != len(peers) { 98 t.Errorf("add peer test err: got %d\nwant %d", syncPeers.size(), len(peers)) 99 } 100 idlePeers := syncPeers.selectIdlePeers() 101 if len(idlePeers) != len(peers) { 102 t.Errorf("selcet idle peers test err: got %d\nwant %d", len(idlePeers), len(peers)) 103 } 104 105 syncPeers.setIdle("Peer1") 106 idlePeers = syncPeers.selectIdlePeers() 107 if len(idlePeers) != 0 { 108 t.Errorf("selcet idle peers test err: got %d\nwant %d", len(idlePeers), 0) 109 } 110 111 syncPeers.setIdle("Peer2") 112 idlePeers = syncPeers.selectIdlePeers() 113 if len(idlePeers) != len(peers) { 114 t.Errorf("selcet idle peers test err: got %d\nwant %d", len(idlePeers), len(peers)) 115 } 116 }