github.com/letterj/go-ethereum@v1.8.22-0.20190204142846-520024dfd689/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  			config := &network.BzzConfig{
    36  				OverlayAddr:  addr.Over(),
    37  				UnderlayAddr: addr.Under(),
    38  				HiveParams:   hp,
    39  			}
    40  			kad := network.NewKademlia(addr.Over(), network.NewKadParams())
    41  			// store kademlia in node's bucket under BucketKeyKademlia
    42  			// so that it can be found by WaitTillHealthy method.
    43  			b.Store(BucketKeyKademlia, kad)
    44  			return network.NewBzz(config, kad, nil, nil, nil), nil, nil
    45  		},
    46  	})
    47  	defer sim.Close()
    48  
    49  	_, err := sim.AddNodesAndConnectRing(10)
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  
    54  	ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
    55  	defer cancel()
    56  	ill, err := sim.WaitTillHealthy(ctx)
    57  	if err != nil {
    58  		for id, kad := range ill {
    59  			t.Log("Node", id)
    60  			t.Log(kad.String())
    61  		}
    62  		if err != nil {
    63  			t.Fatal(err)
    64  		}
    65  	}
    66  }