github.com/core-coin/go-core/v2@v2.1.9/p2p/simulations/connect_test.go (about)

     1  // Copyright 2018 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package simulations
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/core-coin/go-core/v2/node"
    23  	"github.com/core-coin/go-core/v2/p2p/enode"
    24  	"github.com/core-coin/go-core/v2/p2p/simulations/adapters"
    25  )
    26  
    27  func newTestNetwork(t *testing.T, nodeCount int) (*Network, []enode.ID) {
    28  	t.Helper()
    29  	adapter := adapters.NewSimAdapter(adapters.LifecycleConstructors{
    30  		"noopwoop": func(ctx *adapters.ServiceContext, stack *node.Node) (node.Lifecycle, error) {
    31  			return NewNoopService(nil), nil
    32  		},
    33  	})
    34  
    35  	// create network
    36  	network := NewNetwork(adapter, &NetworkConfig{
    37  		DefaultService: "noopwoop",
    38  	})
    39  
    40  	// create and start nodes
    41  	ids := make([]enode.ID, nodeCount)
    42  	for i := range ids {
    43  		conf := adapters.RandomNodeConfig()
    44  		node, err := network.NewNodeWithConfig(conf)
    45  		if err != nil {
    46  			t.Fatalf("error creating node: %s", err)
    47  		}
    48  		if err := network.Start(node.ID()); err != nil {
    49  			t.Fatalf("error starting node: %s", err)
    50  		}
    51  		ids[i] = node.ID()
    52  	}
    53  
    54  	if len(network.Conns) > 0 {
    55  		t.Fatal("no connections should exist after just adding nodes")
    56  	}
    57  
    58  	return network, ids
    59  }
    60  
    61  func TestConnectToLastNode(t *testing.T) {
    62  	net, ids := newTestNetwork(t, 10)
    63  	defer net.Shutdown()
    64  
    65  	first := ids[0]
    66  	if err := net.ConnectToLastNode(first); err != nil {
    67  		t.Fatal(err)
    68  	}
    69  
    70  	last := ids[len(ids)-1]
    71  	for i, id := range ids {
    72  		if id == first || id == last {
    73  			continue
    74  		}
    75  
    76  		if net.GetConn(first, id) != nil {
    77  			t.Errorf("connection must not exist with node(ind: %v, id: %v)", i, id)
    78  		}
    79  	}
    80  
    81  	if net.GetConn(first, last) == nil {
    82  		t.Error("first and last node must be connected")
    83  	}
    84  }
    85  
    86  func TestConnectToRandomNode(t *testing.T) {
    87  	net, ids := newTestNetwork(t, 10)
    88  	defer net.Shutdown()
    89  
    90  	err := net.ConnectToRandomNode(ids[0])
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  
    95  	var cc int
    96  	for i, a := range ids {
    97  		for _, b := range ids[i:] {
    98  			if net.GetConn(a, b) != nil {
    99  				cc++
   100  			}
   101  		}
   102  	}
   103  
   104  	if cc != 1 {
   105  		t.Errorf("expected one connection, got %v", cc)
   106  	}
   107  }
   108  
   109  func TestConnectNodesFull(t *testing.T) {
   110  	tests := []struct {
   111  		name      string
   112  		nodeCount int
   113  	}{
   114  		{name: "no node", nodeCount: 0},
   115  		{name: "single node", nodeCount: 1},
   116  		{name: "2 nodes", nodeCount: 2},
   117  		{name: "3 nodes", nodeCount: 3},
   118  		{name: "even number of nodes", nodeCount: 12},
   119  		{name: "odd number of nodes", nodeCount: 13},
   120  	}
   121  	for _, test := range tests {
   122  		t.Run(test.name, func(t *testing.T) {
   123  			net, ids := newTestNetwork(t, test.nodeCount)
   124  			defer net.Shutdown()
   125  
   126  			err := net.ConnectNodesFull(ids)
   127  			if err != nil {
   128  				t.Fatal(err)
   129  			}
   130  
   131  			VerifyFull(t, net, ids)
   132  		})
   133  	}
   134  }
   135  
   136  func TestConnectNodesChain(t *testing.T) {
   137  	net, ids := newTestNetwork(t, 10)
   138  	defer net.Shutdown()
   139  
   140  	err := net.ConnectNodesChain(ids)
   141  	if err != nil {
   142  		t.Fatal(err)
   143  	}
   144  
   145  	VerifyChain(t, net, ids)
   146  }
   147  
   148  func TestConnectNodesRing(t *testing.T) {
   149  	net, ids := newTestNetwork(t, 10)
   150  	defer net.Shutdown()
   151  
   152  	err := net.ConnectNodesRing(ids)
   153  	if err != nil {
   154  		t.Fatal(err)
   155  	}
   156  
   157  	VerifyRing(t, net, ids)
   158  }
   159  
   160  func TestConnectNodesStar(t *testing.T) {
   161  	net, ids := newTestNetwork(t, 10)
   162  	defer net.Shutdown()
   163  
   164  	pivotIndex := 2
   165  
   166  	err := net.ConnectNodesStar(ids, ids[pivotIndex])
   167  	if err != nil {
   168  		t.Fatal(err)
   169  	}
   170  
   171  	VerifyStar(t, net, ids, pivotIndex)
   172  }