github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/p2pserver/peer/nbr_peer_test.go (about) 1 /* 2 * Copyright (C) 2018 The ontology Authors 3 * This file is part of The ontology library. 4 * 5 * The ontology is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Lesser General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * The ontology is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with The ontology. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 package peer 20 21 import ( 22 "fmt" 23 "testing" 24 "time" 25 26 "github.com/ontio/ontology/common/log" 27 ) 28 29 var nm *NbrPeers 30 31 func creatPeers(cnt uint16) []*Peer { 32 np := []*Peer{} 33 var syncport uint16 34 var consport uint16 35 var id uint64 36 var height uint64 37 for i := uint16(0); i < cnt; i++ { 38 syncport = 20224 + i 39 consport = 20335 + i 40 id = 0x7533345 + uint64(i) 41 height = 434923 + uint64(i) 42 p = NewPeer() 43 p.UpdateInfo(time.Now(), 2, 3, syncport, consport, id, 0, height) 44 p.SetConsState(2) 45 p.SetSyncState(3) 46 p.SetHttpInfoState(true) 47 p.SyncLink.SetAddr("127.0.0.1:10338") 48 np = append(np, p) 49 } 50 return np 51 52 } 53 54 func init() { 55 log.Init(log.Stdout) 56 nm = &NbrPeers{} 57 nm.Init() 58 np := creatPeers(5) 59 for _, v := range np { 60 nm.List[v.GetID()] = v 61 } 62 } 63 64 func TestNodeExisted(t *testing.T) { 65 if nm.NodeExisted(0x7533345) == false { 66 t.Fatal("0x7533345 should in nbr peers") 67 } 68 if nm.NodeExisted(0x5533345) == true { 69 t.Fatal("0x5533345 should not in nbr peers") 70 } 71 } 72 73 func TestGetPeer(t *testing.T) { 74 p := nm.GetPeer(0x7533345) 75 if p == nil { 76 t.Fatal("TestGetPeer error") 77 } 78 } 79 80 func TestAddNbrNode(t *testing.T) { 81 p := NewPeer() 82 p.UpdateInfo(time.Now(), 2, 3, 10335, 10336, 0x7123456, 0, 100) 83 p.SetConsState(2) 84 p.SetSyncState(3) 85 p.SetHttpInfoState(true) 86 p.SyncLink.SetAddr("127.0.0.1") 87 nm.AddNbrNode(p) 88 if nm.NodeExisted(0x7123456) == false { 89 t.Fatal("0x7123456 should be added in nbr peer") 90 } 91 if len(nm.List) != 6 { 92 t.Fatal("0x7123456 should be added in nbr peer") 93 } 94 } 95 96 func TestDelNbrNode(t *testing.T) { 97 cnt := len(nm.List) 98 p, ret := nm.DelNbrNode(0x7533345) 99 if p == nil || ret != true { 100 t.Fatal("TestDelNbrNode err") 101 } 102 if len(nm.List) != cnt-1 { 103 t.Fatal("TestDelNbrNode not work") 104 } 105 p.DumpInfo() 106 } 107 108 func TestNodeEstablished(t *testing.T) { 109 p := nm.GetPeer(0x7533346) 110 if p == nil { 111 t.Fatal("TestNodeEstablished:get peer error") 112 } 113 p.SetSyncState(4) 114 if nm.NodeEstablished(0x7533346) == false { 115 t.Fatal("TestNodeEstablished error") 116 } 117 } 118 119 func TestGetNeighborAddrs(t *testing.T) { 120 p := nm.GetPeer(0x7533346) 121 if p == nil { 122 t.Fatal("TestGetNeighborAddrs:get peer error") 123 } 124 p.SetSyncState(4) 125 126 p = nm.GetPeer(0x7533347) 127 if p == nil { 128 t.Fatal("TestGetNeighborAddrs:get peer error") 129 } 130 p.SetSyncState(4) 131 132 pList := nm.GetNeighborAddrs() 133 for i := 0; i < int(cnt); i++ { 134 fmt.Printf("peer id = %x \n", pList[i].ID) 135 } 136 if cnt != 2 { 137 t.Fatal("TestGetNeighborAddrs error") 138 } 139 } 140 141 func TestGetNeighborHeights(t *testing.T) { 142 p := nm.GetPeer(0x7533346) 143 if p == nil { 144 t.Fatal("TestGetNeighborHeights:get peer error") 145 } 146 p.SetSyncState(4) 147 148 p = nm.GetPeer(0x7533347) 149 if p == nil { 150 t.Fatal("TestGetNeighborHeights:get peer error") 151 } 152 p.SetSyncState(4) 153 154 pMap := nm.GetNeighborHeights() 155 for k, v := range pMap { 156 fmt.Printf("peer id = %x height = %d \n", k, v) 157 } 158 } 159 160 func TestGetNeighbors(t *testing.T) { 161 p := nm.GetPeer(0x7533346) 162 if p == nil { 163 t.Fatal("TestGetNeighbors:get peer error") 164 } 165 p.SetSyncState(4) 166 167 p = nm.GetPeer(0x7533347) 168 if p == nil { 169 t.Fatal("TestGetNeighbors:get peer error") 170 } 171 p.SetSyncState(4) 172 173 pList := nm.GetNeighbors() 174 for _, v := range pList { 175 v.DumpInfo() 176 } 177 } 178 179 func TestGetNbrNodeCnt(t *testing.T) { 180 p := nm.GetPeer(0x7533346) 181 if p == nil { 182 t.Fatal("TestGetNbrNodeCnt:get peer error") 183 } 184 p.SetSyncState(4) 185 186 p = nm.GetPeer(0x7533347) 187 if p == nil { 188 t.Fatal("TestGetNbrNodeCnt:get peer error") 189 } 190 p.SetSyncState(4) 191 192 if nm.GetNbrNodeCnt() != 2 { 193 t.Fatal("TestGetNbrNodeCnt error") 194 } 195 }