github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/p2p/simulations/connect_test.go (about)

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