github.com/FUSIONFoundation/efsn@v3.6.2-0.20200916075423-dbb5dd5d2cc7+incompatible/swarm/network/hive_test.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package network 18 19 import ( 20 "io/ioutil" 21 "log" 22 "os" 23 "testing" 24 25 p2ptest "github.com/FusionFoundation/efsn/p2p/testing" 26 "github.com/FusionFoundation/efsn/swarm/state" 27 ) 28 29 func newHiveTester(t *testing.T, params *HiveParams, n int, store state.Store) (*bzzTester, *Hive) { 30 // setup 31 addr := RandomAddr() // tested peers peer address 32 to := NewKademlia(addr.OAddr, NewKadParams()) 33 pp := NewHive(params, to, store) // hive 34 35 return newBzzBaseTester(t, n, addr, DiscoverySpec, pp.Run), pp 36 } 37 38 func TestRegisterAndConnect(t *testing.T) { 39 params := NewHiveParams() 40 s, pp := newHiveTester(t, params, 1, nil) 41 42 id := s.IDs[0] 43 raddr := NewAddrFromNodeID(id) 44 pp.Register(raddr) 45 46 // start the hive and wait for the connection 47 err := pp.Start(s.Server) 48 if err != nil { 49 t.Fatal(err) 50 } 51 defer pp.Stop() 52 // retrieve and broadcast 53 err = s.TestDisconnected(&p2ptest.Disconnect{ 54 Peer: s.IDs[0], 55 Error: nil, 56 }) 57 58 if err == nil || err.Error() != "timed out waiting for peers to disconnect" { 59 t.Fatalf("expected peer to connect") 60 } 61 } 62 63 func TestHiveStatePersistance(t *testing.T) { 64 log.SetOutput(os.Stdout) 65 66 dir, err := ioutil.TempDir("", "hive_test_store") 67 if err != nil { 68 panic(err) 69 } 70 defer os.RemoveAll(dir) 71 72 store, err := state.NewDBStore(dir) //start the hive with an empty dbstore 73 74 params := NewHiveParams() 75 s, pp := newHiveTester(t, params, 5, store) 76 77 peers := make(map[string]bool) 78 for _, id := range s.IDs { 79 raddr := NewAddrFromNodeID(id) 80 pp.Register(raddr) 81 peers[raddr.String()] = true 82 } 83 84 // start the hive and wait for the connection 85 err = pp.Start(s.Server) 86 if err != nil { 87 t.Fatal(err) 88 } 89 pp.Stop() 90 store.Close() 91 92 persistedStore, err := state.NewDBStore(dir) //start the hive with an empty dbstore 93 94 s1, pp := newHiveTester(t, params, 1, persistedStore) 95 96 //start the hive and wait for the connection 97 98 pp.Start(s1.Server) 99 i := 0 100 pp.Kademlia.EachAddr(nil, 256, func(addr *BzzAddr, po int, nn bool) bool { 101 delete(peers, addr.String()) 102 i++ 103 return true 104 }) 105 if len(peers) != 0 || i != 5 { 106 t.Fatalf("invalid peers loaded") 107 } 108 }