github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/swarm/network/simulation/kademlia_test.go (about)

     1  // Copyright 2018 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 simulation
    18  
    19  import (
    20  	"context"
    21  	"sync"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/ethereum/go-ethereum/node"
    26  	"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
    27  	"github.com/ethereum/go-ethereum/swarm/network"
    28  )
    29  
    30  func TestWaitTillHealthy(t *testing.T) {
    31  	sim := New(map[string]ServiceFunc{
    32  		"bzz": func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) {
    33  			addr := network.NewAddr(ctx.Config.Node())
    34  			hp := network.NewHiveParams()
    35  			hp.Discovery = false
    36  			config := &network.BzzConfig{
    37  				OverlayAddr:  addr.Over(),
    38  				UnderlayAddr: addr.Under(),
    39  				HiveParams:   hp,
    40  			}
    41  			kad := network.NewKademlia(addr.Over(), network.NewKadParams())
    42  			// store kademlia in node's bucket under BucketKeyKademlia
    43  			// so that it can be found by WaitTillHealthy method.
    44  			b.Store(BucketKeyKademlia, kad)
    45  			return network.NewBzz(config, kad, nil, nil, nil), nil, nil
    46  		},
    47  	})
    48  	defer sim.Close()
    49  
    50  	_, err := sim.AddNodesAndConnectRing(10)
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  
    55  	ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
    56  	defer cancel()
    57  	ill, err := sim.WaitTillHealthy(ctx, 2)
    58  	if err != nil {
    59  		for id, kad := range ill {
    60  			t.Log("Node", id)
    61  			t.Log(kad.String())
    62  		}
    63  		if err != nil {
    64  			t.Fatal(err)
    65  		}
    66  	}
    67  }