github.com/codingfuture/orig-energi3@v0.8.4/swarm/network/hive_test.go (about) 1 // Copyright 2018 The Energi Core Authors 2 // Copyright 2018 The go-ethereum Authors 3 // This file is part of the Energi Core library. 4 // 5 // The Energi Core library 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 Energi Core library 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 Energi Core library. If not, see <http://www.gnu.org/licenses/>. 17 18 package network 19 20 import ( 21 "io/ioutil" 22 "os" 23 "testing" 24 "time" 25 26 p2ptest "github.com/ethereum/go-ethereum/p2p/testing" 27 "github.com/ethereum/go-ethereum/swarm/state" 28 ) 29 30 func newHiveTester(t *testing.T, params *HiveParams, n int, store state.Store) (*bzzTester, *Hive) { 31 // setup 32 addr := RandomAddr() // tested peers peer address 33 to := NewKademlia(addr.OAddr, NewKadParams()) 34 pp := NewHive(params, to, store) // hive 35 36 return newBzzBaseTester(t, n, addr, DiscoverySpec, pp.Run), pp 37 } 38 39 // TestRegisterAndConnect verifies that the protocol runs successfully 40 // and that the peer connection exists afterwards 41 func TestRegisterAndConnect(t *testing.T) { 42 params := NewHiveParams() 43 s, pp := newHiveTester(t, params, 1, nil) 44 45 node := s.Nodes[0] 46 raddr := NewAddr(node) 47 pp.Register(raddr) 48 49 // start the hive 50 err := pp.Start(s.Server) 51 if err != nil { 52 t.Fatal(err) 53 } 54 defer pp.Stop() 55 56 // both hive connect and disconect check have time delays 57 // therefore we need to verify that peer is connected 58 // so that we are sure that the disconnect timeout doesn't complete 59 // before the hive connect method is run at least once 60 timeout := time.After(time.Second) 61 for { 62 select { 63 case <-timeout: 64 t.Fatalf("expected connection") 65 default: 66 } 67 i := 0 68 pp.Kademlia.EachConn(nil, 256, func(addr *Peer, po int) bool { 69 i++ 70 return true 71 }) 72 if i > 0 { 73 break 74 } 75 time.Sleep(time.Millisecond) 76 } 77 78 // check that the connection actually exists 79 // the timeout error means no disconnection events 80 // were received within the a certain timeout 81 err = s.TestDisconnected(&p2ptest.Disconnect{ 82 Peer: s.Nodes[0].ID(), 83 Error: nil, 84 }) 85 86 if err == nil || err.Error() != "timed out waiting for peers to disconnect" { 87 t.Fatalf("expected no disconnection event") 88 } 89 } 90 91 // TestHiveStatePersistance creates a protocol simulation with n peers for a node 92 // After protocols complete, the node is shut down and the state is stored. 93 // Another simulation is created, where 0 nodes are created, but where the stored state is passed 94 // The test succeeds if all the peers from the stored state are known after the protocols of the 95 // second simulation have completed 96 // 97 // Actual connectivity is not in scope for this test, as the peers loaded from state are not known to 98 // the simulation; the test only verifies that the peers are known to the node 99 func TestHiveStatePersistance(t *testing.T) { 100 101 dir, err := ioutil.TempDir("", "hive_test_store") 102 if err != nil { 103 panic(err) 104 } 105 defer os.RemoveAll(dir) 106 107 store, err := state.NewDBStore(dir) //start the hive with an empty dbstore 108 if err != nil { 109 t.Fatal(err) 110 } 111 112 params := NewHiveParams() 113 s, pp := newHiveTester(t, params, 5, store) 114 115 peers := make(map[string]bool) 116 for _, node := range s.Nodes { 117 raddr := NewAddr(node) 118 pp.Register(raddr) 119 peers[raddr.String()] = true 120 } 121 122 // start and stop the hive 123 // the known peers should be saved upon stopping 124 err = pp.Start(s.Server) 125 if err != nil { 126 t.Fatal(err) 127 } 128 pp.Stop() 129 store.Close() 130 131 // start the hive with an empty dbstore 132 persistedStore, err := state.NewDBStore(dir) 133 if err != nil { 134 t.Fatal(err) 135 } 136 137 s1, pp := newHiveTester(t, params, 0, persistedStore) 138 139 // start the hive and check that we know of all expected peers 140 pp.Start(s1.Server) 141 i := 0 142 pp.Kademlia.EachAddr(nil, 256, func(addr *BzzAddr, po int) bool { 143 delete(peers, addr.String()) 144 i++ 145 return true 146 }) 147 // TODO remove this line when verified that test passes 148 time.Sleep(time.Second) 149 if i != 5 { 150 t.Fatalf("invalid number of entries: got %v, want %v", i, 5) 151 } 152 if len(peers) != 0 { 153 t.Fatalf("%d peers left over: %v", len(peers), peers) 154 } 155 156 }