connect_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. package simulations
  17. import (
  18. "testing"
  19. "github.com/ethereum/go-ethereum/node"
  20. "github.com/ethereum/go-ethereum/p2p/enode"
  21. "github.com/ethereum/go-ethereum/p2p/simulations/adapters"
  22. )
  23. func newTestNetwork(t *testing.T, nodeCount int) (*Network, []enode.ID) {
  24. adapter := adapters.NewSimAdapter(adapters.Services{
  25. "noopwoop": func(ctx *adapters.ServiceContext) (node.Service, error) {
  26. return NewNoopService(nil), nil
  27. },
  28. })
  29. // create network
  30. network := NewNetwork(adapter, &NetworkConfig{
  31. DefaultService: "noopwoop",
  32. })
  33. // create and start nodes
  34. ids := make([]enode.ID, nodeCount)
  35. for i := range ids {
  36. conf := adapters.RandomNodeConfig()
  37. node, err := network.NewNodeWithConfig(conf)
  38. if err != nil {
  39. t.Fatalf("error creating node: %s", err)
  40. }
  41. if err := network.Start(node.ID()); err != nil {
  42. t.Fatalf("error starting node: %s", err)
  43. }
  44. ids[i] = node.ID()
  45. }
  46. if len(network.Conns) > 0 {
  47. t.Fatal("no connections should exist after just adding nodes")
  48. }
  49. return network, ids
  50. }
  51. func TestConnectToPivotNode(t *testing.T) {
  52. net, ids := newTestNetwork(t, 2)
  53. defer net.Shutdown()
  54. pivot := ids[0]
  55. net.SetPivotNode(pivot)
  56. other := ids[1]
  57. err := net.ConnectToPivotNode(other)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. if net.GetConn(pivot, other) == nil {
  62. t.Error("pivot and the other node are not connected")
  63. }
  64. }
  65. func TestConnectToLastNode(t *testing.T) {
  66. net, ids := newTestNetwork(t, 10)
  67. defer net.Shutdown()
  68. first := ids[0]
  69. if err := net.ConnectToLastNode(first); err != nil {
  70. t.Fatal(err)
  71. }
  72. last := ids[len(ids)-1]
  73. for i, id := range ids {
  74. if id == first || id == last {
  75. continue
  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. if net.GetConn(first, last) == nil {
  82. t.Error("first and last node must be connected")
  83. }
  84. }
  85. func TestConnectToRandomNode(t *testing.T) {
  86. net, ids := newTestNetwork(t, 10)
  87. defer net.Shutdown()
  88. err := net.ConnectToRandomNode(ids[0])
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. var cc int
  93. for i, a := range ids {
  94. for _, b := range ids[i:] {
  95. if net.GetConn(a, b) != nil {
  96. cc++
  97. }
  98. }
  99. }
  100. if cc != 1 {
  101. t.Errorf("expected one connection, got %v", cc)
  102. }
  103. }
  104. func TestConnectNodesFull(t *testing.T) {
  105. net, ids := newTestNetwork(t, 12)
  106. defer net.Shutdown()
  107. err := net.ConnectNodesFull(ids)
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. VerifyFull(t, net, ids)
  112. }
  113. func TestConnectNodesChain(t *testing.T) {
  114. net, ids := newTestNetwork(t, 10)
  115. defer net.Shutdown()
  116. err := net.ConnectNodesChain(ids)
  117. if err != nil {
  118. t.Fatal(err)
  119. }
  120. VerifyChain(t, net, ids)
  121. }
  122. func TestConnectNodesRing(t *testing.T) {
  123. net, ids := newTestNetwork(t, 10)
  124. defer net.Shutdown()
  125. err := net.ConnectNodesRing(ids)
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. VerifyRing(t, net, ids)
  130. }
  131. func TestConnectNodesStar(t *testing.T) {
  132. net, ids := newTestNetwork(t, 10)
  133. defer net.Shutdown()
  134. pivotIndex := 2
  135. err := net.ConnectNodesStar(ids[pivotIndex], ids)
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. VerifyStar(t, net, ids, pivotIndex)
  140. }
  141. func TestConnectNodesStarPivot(t *testing.T) {
  142. net, ids := newTestNetwork(t, 10)
  143. defer net.Shutdown()
  144. pivotIndex := 4
  145. net.SetPivotNode(ids[pivotIndex])
  146. err := net.ConnectNodesStarPivot(ids)
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. VerifyStar(t, net, ids, pivotIndex)
  151. }