github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/p2pserver/net/netserver/netserver_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 netserver 20 21 import ( 22 "fmt" 23 "testing" 24 "time" 25 26 "github.com/ontio/ontology/common/log" 27 "github.com/sixexorg/magnetic-ring/p2pserver/common" 28 "github.com/sixexorg/magnetic-ring/p2pserver/peer" 29 ) 30 31 func init() { 32 log.Init(log.Stdout) 33 fmt.Println("Start test the netserver...") 34 } 35 36 func creatPeers(cnt uint16) []*peer.Peer { 37 np := []*peer.Peer{} 38 var syncport uint16 39 var consport uint16 40 var id uint64 41 var height uint64 42 for i := uint16(0); i < cnt; i++ { 43 syncport = 20224 + i 44 consport = 20335 + i 45 id = 0x7533345 + uint64(i) 46 height = 434923 + uint64(i) 47 p := peer.NewPeer() 48 p.UpdateInfo(time.Now(), 2, 3, syncport, consport, id, 0, height) 49 p.SetConsState(2) 50 p.SetSyncState(4) 51 p.SetHttpInfoState(true) 52 p.SyncLink.SetAddr("127.0.0.1:10338") 53 np = append(np, p) 54 } 55 return np 56 57 } 58 func TestNewNetServer(t *testing.T) { 59 server := NewNetServer() 60 server.Start() 61 defer server.Halt() 62 63 server.SetHeight(1000) 64 if server.GetHeight() != 1000 { 65 t.Error("TestNewNetServer set server height error") 66 } 67 68 if server.GetRelay() != true { 69 t.Error("TestNewNetServer server relay state error", server.GetRelay()) 70 } 71 if server.GetServices() != 1 { 72 t.Error("TestNewNetServer server service state error", server.GetServices()) 73 } 74 if server.GetVersion() != common.PROTOCOL_VERSION { 75 t.Error("TestNewNetServer server version error", server.GetVersion()) 76 } 77 if server.GetSyncPort() != 20338 { 78 t.Error("TestNewNetServer sync port error", server.GetSyncPort()) 79 } 80 if server.GetConsPort() != 20339 { 81 t.Error("TestNewNetServer sync port error", server.GetConsPort()) 82 } 83 84 fmt.Printf("lastest server time is %s\n", time.Unix(server.GetTime()/1e9, 0).String()) 85 86 } 87 88 func TestNetServerNbrPeer(t *testing.T) { 89 log.Init(log.Stdout) 90 server := NewNetServer() 91 server.Start() 92 defer server.Halt() 93 94 nm := &peer.NbrPeers{} 95 nm.Init() 96 np := creatPeers(5) 97 for _, v := range np { 98 server.AddNbrNode(v) 99 } 100 if server.GetConnectionCnt() != 5 { 101 t.Error("TestNetServerNbrPeer GetConnectionCnt error", server.GetConnectionCnt()) 102 } 103 addrs := server.GetNeighborAddrs() 104 if len(addrs) != 5 { 105 t.Error("TestNetServerNbrPeer GetNeighborAddrs error") 106 } 107 if server.NodeEstablished(0x7533345) == false { 108 t.Error("TestNetServerNbrPeer NodeEstablished error") 109 } 110 if server.GetPeer(0x7533345) == nil { 111 t.Error("TestNetServerNbrPeer GetPeer error") 112 } 113 p, ok := server.DelNbrNode(0x7533345) 114 if ok != true || p == nil { 115 t.Error("TestNetServerNbrPeer DelNbrNode error") 116 } 117 if len(server.GetNeighbors()) != 4 { 118 t.Error("TestNetServerNbrPeer GetNeighbors error") 119 } 120 sp := &peer.Peer{} 121 cp := &peer.Peer{} 122 server.AddPeerSyncAddress("127.0.0.1:10338", sp) 123 server.AddPeerConsAddress("127.0.0.1:20338", cp) 124 if server.GetPeerFromAddr("127.0.0.1:10338") != sp { 125 t.Error("TestNetServerNbrPeer Get/AddPeerConsAddress error") 126 } 127 128 }