connect_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 simulation
  17. import (
  18. "testing"
  19. "github.com/ethereum/go-ethereum/p2p/enode"
  20. )
  21. func TestConnectToPivotNode(t *testing.T) {
  22. sim := New(noopServiceFuncMap)
  23. defer sim.Close()
  24. pid, err := sim.AddNode()
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. sim.SetPivotNode(pid)
  29. id, err := sim.AddNode()
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. if len(sim.Net.Conns) > 0 {
  34. t.Fatal("no connections should exist after just adding nodes")
  35. }
  36. err = sim.ConnectToPivotNode(id)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. if sim.Net.GetConn(id, pid) == nil {
  41. t.Error("node did not connect to pivot node")
  42. }
  43. }
  44. func TestConnectToLastNode(t *testing.T) {
  45. sim := New(noopServiceFuncMap)
  46. defer sim.Close()
  47. n := 10
  48. ids, err := sim.AddNodes(n)
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. id, err := sim.AddNode()
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. if len(sim.Net.Conns) > 0 {
  57. t.Fatal("no connections should exist after just adding nodes")
  58. }
  59. err = sim.ConnectToLastNode(id)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. for _, i := range ids[:n-2] {
  64. if sim.Net.GetConn(id, i) != nil {
  65. t.Error("node connected to the node that is not the last")
  66. }
  67. }
  68. if sim.Net.GetConn(id, ids[n-1]) == nil {
  69. t.Error("node did not connect to the last node")
  70. }
  71. }
  72. func TestConnectToRandomNode(t *testing.T) {
  73. sim := New(noopServiceFuncMap)
  74. defer sim.Close()
  75. n := 10
  76. ids, err := sim.AddNodes(n)
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. if len(sim.Net.Conns) > 0 {
  81. t.Fatal("no connections should exist after just adding nodes")
  82. }
  83. err = sim.ConnectToRandomNode(ids[0])
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. var cc int
  88. for i := 0; i < n; i++ {
  89. for j := i + 1; j < n; j++ {
  90. if sim.Net.GetConn(ids[i], ids[j]) != nil {
  91. cc++
  92. }
  93. }
  94. }
  95. if cc != 1 {
  96. t.Errorf("expected one connection, got %v", cc)
  97. }
  98. }
  99. func TestConnectNodesFull(t *testing.T) {
  100. sim := New(noopServiceFuncMap)
  101. defer sim.Close()
  102. ids, err := sim.AddNodes(12)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. if len(sim.Net.Conns) > 0 {
  107. t.Fatal("no connections should exist after just adding nodes")
  108. }
  109. err = sim.ConnectNodesFull(ids)
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. testFull(t, sim, ids)
  114. }
  115. func testFull(t *testing.T, sim *Simulation, ids []enode.ID) {
  116. n := len(ids)
  117. var cc int
  118. for i := 0; i < n; i++ {
  119. for j := i + 1; j < n; j++ {
  120. if sim.Net.GetConn(ids[i], ids[j]) != nil {
  121. cc++
  122. }
  123. }
  124. }
  125. want := n * (n - 1) / 2
  126. if cc != want {
  127. t.Errorf("expected %v connection, got %v", want, cc)
  128. }
  129. }
  130. func TestConnectNodesChain(t *testing.T) {
  131. sim := New(noopServiceFuncMap)
  132. defer sim.Close()
  133. ids, err := sim.AddNodes(10)
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. if len(sim.Net.Conns) > 0 {
  138. t.Fatal("no connections should exist after just adding nodes")
  139. }
  140. err = sim.ConnectNodesChain(ids)
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. testChain(t, sim, ids)
  145. }
  146. func testChain(t *testing.T, sim *Simulation, ids []enode.ID) {
  147. n := len(ids)
  148. for i := 0; i < n; i++ {
  149. for j := i + 1; j < n; j++ {
  150. c := sim.Net.GetConn(ids[i], ids[j])
  151. if i == j-1 {
  152. if c == nil {
  153. t.Errorf("nodes %v and %v are not connected, but they should be", i, j)
  154. }
  155. } else {
  156. if c != nil {
  157. t.Errorf("nodes %v and %v are connected, but they should not be", i, j)
  158. }
  159. }
  160. }
  161. }
  162. }
  163. func TestConnectNodesRing(t *testing.T) {
  164. sim := New(noopServiceFuncMap)
  165. defer sim.Close()
  166. ids, err := sim.AddNodes(10)
  167. if err != nil {
  168. t.Fatal(err)
  169. }
  170. if len(sim.Net.Conns) > 0 {
  171. t.Fatal("no connections should exist after just adding nodes")
  172. }
  173. err = sim.ConnectNodesRing(ids)
  174. if err != nil {
  175. t.Fatal(err)
  176. }
  177. testRing(t, sim, ids)
  178. }
  179. func testRing(t *testing.T, sim *Simulation, ids []enode.ID) {
  180. n := len(ids)
  181. for i := 0; i < n; i++ {
  182. for j := i + 1; j < n; j++ {
  183. c := sim.Net.GetConn(ids[i], ids[j])
  184. if i == j-1 || (i == 0 && j == n-1) {
  185. if c == nil {
  186. t.Errorf("nodes %v and %v are not connected, but they should be", i, j)
  187. }
  188. } else {
  189. if c != nil {
  190. t.Errorf("nodes %v and %v are connected, but they should not be", i, j)
  191. }
  192. }
  193. }
  194. }
  195. }
  196. func TestConnectToNodesStar(t *testing.T) {
  197. sim := New(noopServiceFuncMap)
  198. defer sim.Close()
  199. ids, err := sim.AddNodes(10)
  200. if err != nil {
  201. t.Fatal(err)
  202. }
  203. if len(sim.Net.Conns) > 0 {
  204. t.Fatal("no connections should exist after just adding nodes")
  205. }
  206. centerIndex := 2
  207. err = sim.ConnectNodesStar(ids[centerIndex], ids)
  208. if err != nil {
  209. t.Fatal(err)
  210. }
  211. testStar(t, sim, ids, centerIndex)
  212. }
  213. func testStar(t *testing.T, sim *Simulation, ids []enode.ID, centerIndex int) {
  214. n := len(ids)
  215. for i := 0; i < n; i++ {
  216. for j := i + 1; j < n; j++ {
  217. c := sim.Net.GetConn(ids[i], ids[j])
  218. if i == centerIndex || j == centerIndex {
  219. if c == nil {
  220. t.Errorf("nodes %v and %v are not connected, but they should be", i, j)
  221. }
  222. } else {
  223. if c != nil {
  224. t.Errorf("nodes %v and %v are connected, but they should not be", i, j)
  225. }
  226. }
  227. }
  228. }
  229. }
  230. func TestConnectToNodesStarPivot(t *testing.T) {
  231. sim := New(noopServiceFuncMap)
  232. defer sim.Close()
  233. ids, err := sim.AddNodes(10)
  234. if err != nil {
  235. t.Fatal(err)
  236. }
  237. if len(sim.Net.Conns) > 0 {
  238. t.Fatal("no connections should exist after just adding nodes")
  239. }
  240. pivotIndex := 4
  241. sim.SetPivotNode(ids[pivotIndex])
  242. err = sim.ConnectNodesStarPivot(ids)
  243. if err != nil {
  244. t.Fatal(err)
  245. }
  246. testStar(t, sim, ids, pivotIndex)
  247. }