github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/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/ethereum/go-ethereum/p2p/testing"
    26  	"github.com/ethereum/go-ethereum/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  
    74  	params := NewHiveParams()
    75  	s, pp := newHiveTester(t, params, 5, store)
    76  
    77  	peers := make(map[string]bool)
    78  	for _, node := range s.Nodes {
    79  		raddr := NewAddr(node)
    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 i != 5 {
   106  		t.Errorf("invalid number of entries: got %v, want %v", i, 5)
   107  	}
   108  	if len(peers) != 0 {
   109  		t.Fatalf("%d peers left over: %v", len(peers), peers)
   110  	}
   111  }