github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/swarm/network/simulation/connect_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:47</date>
    10  //</624342673389522944>
    11  
    12  
    13  package simulation
    14  
    15  import (
    16  	"testing"
    17  
    18  	"github.com/ethereum/go-ethereum/p2p/discover"
    19  )
    20  
    21  func TestConnectToPivotNode(t *testing.T) {
    22  	sim := New(noopServiceFuncMap)
    23  	defer sim.Close()
    24  
    25  	pid, err := sim.AddNode()
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  
    30  	sim.SetPivotNode(pid)
    31  
    32  	id, err := sim.AddNode()
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	if len(sim.Net.Conns) > 0 {
    38  		t.Fatal("no connections should exist after just adding nodes")
    39  	}
    40  
    41  	err = sim.ConnectToPivotNode(id)
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  
    46  	if sim.Net.GetConn(id, pid) == nil {
    47  		t.Error("node did not connect to pivot node")
    48  	}
    49  }
    50  
    51  func TestConnectToLastNode(t *testing.T) {
    52  	sim := New(noopServiceFuncMap)
    53  	defer sim.Close()
    54  
    55  	n := 10
    56  
    57  	ids, err := sim.AddNodes(n)
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  
    62  	id, err := sim.AddNode()
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  
    67  	if len(sim.Net.Conns) > 0 {
    68  		t.Fatal("no connections should exist after just adding nodes")
    69  	}
    70  
    71  	err = sim.ConnectToLastNode(id)
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  
    76  	for _, i := range ids[:n-2] {
    77  		if sim.Net.GetConn(id, i) != nil {
    78  			t.Error("node connected to the node that is not the last")
    79  		}
    80  	}
    81  
    82  	if sim.Net.GetConn(id, ids[n-1]) == nil {
    83  		t.Error("node did not connect to the last node")
    84  	}
    85  }
    86  
    87  func TestConnectToRandomNode(t *testing.T) {
    88  	sim := New(noopServiceFuncMap)
    89  	defer sim.Close()
    90  
    91  	n := 10
    92  
    93  	ids, err := sim.AddNodes(n)
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  
    98  	if len(sim.Net.Conns) > 0 {
    99  		t.Fatal("no connections should exist after just adding nodes")
   100  	}
   101  
   102  	err = sim.ConnectToRandomNode(ids[0])
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  
   107  	var cc int
   108  	for i := 0; i < n; i++ {
   109  		for j := i + 1; j < n; j++ {
   110  			if sim.Net.GetConn(ids[i], ids[j]) != nil {
   111  				cc++
   112  			}
   113  		}
   114  	}
   115  
   116  	if cc != 1 {
   117  		t.Errorf("expected one connection, got %v", cc)
   118  	}
   119  }
   120  
   121  func TestConnectNodesFull(t *testing.T) {
   122  	sim := New(noopServiceFuncMap)
   123  	defer sim.Close()
   124  
   125  	ids, err := sim.AddNodes(12)
   126  	if err != nil {
   127  		t.Fatal(err)
   128  	}
   129  
   130  	if len(sim.Net.Conns) > 0 {
   131  		t.Fatal("no connections should exist after just adding nodes")
   132  	}
   133  
   134  	err = sim.ConnectNodesFull(ids)
   135  	if err != nil {
   136  		t.Fatal(err)
   137  	}
   138  
   139  	testFull(t, sim, ids)
   140  }
   141  
   142  func testFull(t *testing.T, sim *Simulation, ids []discover.NodeID) {
   143  	n := len(ids)
   144  	var cc int
   145  	for i := 0; i < n; i++ {
   146  		for j := i + 1; j < n; j++ {
   147  			if sim.Net.GetConn(ids[i], ids[j]) != nil {
   148  				cc++
   149  			}
   150  		}
   151  	}
   152  
   153  	want := n * (n - 1) / 2
   154  
   155  	if cc != want {
   156  		t.Errorf("expected %v connection, got %v", want, cc)
   157  	}
   158  }
   159  
   160  func TestConnectNodesChain(t *testing.T) {
   161  	sim := New(noopServiceFuncMap)
   162  	defer sim.Close()
   163  
   164  	ids, err := sim.AddNodes(10)
   165  	if err != nil {
   166  		t.Fatal(err)
   167  	}
   168  
   169  	if len(sim.Net.Conns) > 0 {
   170  		t.Fatal("no connections should exist after just adding nodes")
   171  	}
   172  
   173  	err = sim.ConnectNodesChain(ids)
   174  	if err != nil {
   175  		t.Fatal(err)
   176  	}
   177  
   178  	testChain(t, sim, ids)
   179  }
   180  
   181  func testChain(t *testing.T, sim *Simulation, ids []discover.NodeID) {
   182  	n := len(ids)
   183  	for i := 0; i < n; i++ {
   184  		for j := i + 1; j < n; j++ {
   185  			c := sim.Net.GetConn(ids[i], ids[j])
   186  			if i == j-1 {
   187  				if c == nil {
   188  					t.Errorf("nodes %v and %v are not connected, but they should be", i, j)
   189  				}
   190  			} else {
   191  				if c != nil {
   192  					t.Errorf("nodes %v and %v are connected, but they should not be", i, j)
   193  				}
   194  			}
   195  		}
   196  	}
   197  }
   198  
   199  func TestConnectNodesRing(t *testing.T) {
   200  	sim := New(noopServiceFuncMap)
   201  	defer sim.Close()
   202  
   203  	ids, err := sim.AddNodes(10)
   204  	if err != nil {
   205  		t.Fatal(err)
   206  	}
   207  
   208  	if len(sim.Net.Conns) > 0 {
   209  		t.Fatal("no connections should exist after just adding nodes")
   210  	}
   211  
   212  	err = sim.ConnectNodesRing(ids)
   213  	if err != nil {
   214  		t.Fatal(err)
   215  	}
   216  
   217  	testRing(t, sim, ids)
   218  }
   219  
   220  func testRing(t *testing.T, sim *Simulation, ids []discover.NodeID) {
   221  	n := len(ids)
   222  	for i := 0; i < n; i++ {
   223  		for j := i + 1; j < n; j++ {
   224  			c := sim.Net.GetConn(ids[i], ids[j])
   225  			if i == j-1 || (i == 0 && j == n-1) {
   226  				if c == nil {
   227  					t.Errorf("nodes %v and %v are not connected, but they should be", i, j)
   228  				}
   229  			} else {
   230  				if c != nil {
   231  					t.Errorf("nodes %v and %v are connected, but they should not be", i, j)
   232  				}
   233  			}
   234  		}
   235  	}
   236  }
   237  
   238  func TestConnectToNodesStar(t *testing.T) {
   239  	sim := New(noopServiceFuncMap)
   240  	defer sim.Close()
   241  
   242  	ids, err := sim.AddNodes(10)
   243  	if err != nil {
   244  		t.Fatal(err)
   245  	}
   246  
   247  	if len(sim.Net.Conns) > 0 {
   248  		t.Fatal("no connections should exist after just adding nodes")
   249  	}
   250  
   251  	centerIndex := 2
   252  
   253  	err = sim.ConnectNodesStar(ids[centerIndex], ids)
   254  	if err != nil {
   255  		t.Fatal(err)
   256  	}
   257  
   258  	testStar(t, sim, ids, centerIndex)
   259  }
   260  
   261  func testStar(t *testing.T, sim *Simulation, ids []discover.NodeID, centerIndex int) {
   262  	n := len(ids)
   263  	for i := 0; i < n; i++ {
   264  		for j := i + 1; j < n; j++ {
   265  			c := sim.Net.GetConn(ids[i], ids[j])
   266  			if i == centerIndex || j == centerIndex {
   267  				if c == nil {
   268  					t.Errorf("nodes %v and %v are not connected, but they should be", i, j)
   269  				}
   270  			} else {
   271  				if c != nil {
   272  					t.Errorf("nodes %v and %v are connected, but they should not be", i, j)
   273  				}
   274  			}
   275  		}
   276  	}
   277  }
   278  
   279  func TestConnectToNodesStarPivot(t *testing.T) {
   280  	sim := New(noopServiceFuncMap)
   281  	defer sim.Close()
   282  
   283  	ids, err := sim.AddNodes(10)
   284  	if err != nil {
   285  		t.Fatal(err)
   286  	}
   287  
   288  	if len(sim.Net.Conns) > 0 {
   289  		t.Fatal("no connections should exist after just adding nodes")
   290  	}
   291  
   292  	pivotIndex := 4
   293  
   294  	sim.SetPivotNode(ids[pivotIndex])
   295  
   296  	err = sim.ConnectNodesStarPivot(ids)
   297  	if err != nil {
   298  		t.Fatal(err)
   299  	}
   300  
   301  	testStar(t, sim, ids, pivotIndex)
   302  }
   303