github.com/shyftnetwork/go-empyrean@v1.8.3-0.20191127201940-fbfca9338f04/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/ShyftNetwork/go-empyrean/p2p/testing"
    26  	"github.com/ShyftNetwork/go-empyrean/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  	node := s.Nodes[0]
    43  	raddr := NewAddr(node)
    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.Nodes[0].ID(),
    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  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  
    77  	params := NewHiveParams()
    78  	s, pp := newHiveTester(t, params, 5, store)
    79  
    80  	peers := make(map[string]bool)
    81  	for _, node := range s.Nodes {
    82  		raddr := NewAddr(node)
    83  		pp.Register(raddr)
    84  		peers[raddr.String()] = true
    85  	}
    86  
    87  	// start the hive and wait for the connection
    88  	err = pp.Start(s.Server)
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  	pp.Stop()
    93  	store.Close()
    94  
    95  	persistedStore, err := state.NewDBStore(dir) //start the hive with an empty dbstore
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  
   100  	s1, pp := newHiveTester(t, params, 1, persistedStore)
   101  
   102  	//start the hive and wait for the connection
   103  
   104  	pp.Start(s1.Server)
   105  	i := 0
   106  	pp.Kademlia.EachAddr(nil, 256, func(addr *BzzAddr, po int) bool {
   107  		delete(peers, addr.String())
   108  		i++
   109  		return true
   110  	})
   111  	if i != 5 {
   112  		t.Errorf("invalid number of entries: got %v, want %v", i, 5)
   113  	}
   114  	if len(peers) != 0 {
   115  		t.Fatalf("%d peers left over: %v", len(peers), peers)
   116  	}
   117  }