github.com/zjj1991/quorum@v0.0.0-20190524123704-ae4b0a1e1a19/swarm/network/simulation/node_test.go (about) 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 17 package simulation 18 19 import ( 20 "context" 21 "fmt" 22 "sync" 23 "testing" 24 "time" 25 26 "github.com/ethereum/go-ethereum/log" 27 "github.com/ethereum/go-ethereum/node" 28 "github.com/ethereum/go-ethereum/p2p/enode" 29 "github.com/ethereum/go-ethereum/p2p/simulations/adapters" 30 "github.com/ethereum/go-ethereum/swarm/network" 31 ) 32 33 func TestUpDownNodeIDs(t *testing.T) { 34 sim := New(noopServiceFuncMap) 35 defer sim.Close() 36 37 ids, err := sim.AddNodes(10) 38 if err != nil { 39 t.Fatal(err) 40 } 41 42 gotIDs := sim.NodeIDs() 43 44 if !equalNodeIDs(ids, gotIDs) { 45 t.Error("returned nodes are not equal to added ones") 46 } 47 48 stoppedIDs, err := sim.StopRandomNodes(3) 49 if err != nil { 50 t.Fatal(err) 51 } 52 53 gotIDs = sim.UpNodeIDs() 54 55 for _, id := range gotIDs { 56 if !sim.Net.GetNode(id).Up { 57 t.Errorf("node %s should not be down", id) 58 } 59 } 60 61 if !equalNodeIDs(ids, append(gotIDs, stoppedIDs...)) { 62 t.Error("returned nodes are not equal to added ones") 63 } 64 65 gotIDs = sim.DownNodeIDs() 66 67 for _, id := range gotIDs { 68 if sim.Net.GetNode(id).Up { 69 t.Errorf("node %s should not be up", id) 70 } 71 } 72 73 if !equalNodeIDs(stoppedIDs, gotIDs) { 74 t.Error("returned nodes are not equal to the stopped ones") 75 } 76 } 77 78 func equalNodeIDs(one, other []enode.ID) bool { 79 if len(one) != len(other) { 80 return false 81 } 82 var count int 83 for _, a := range one { 84 var found bool 85 for _, b := range other { 86 if a == b { 87 found = true 88 break 89 } 90 } 91 if found { 92 count++ 93 } else { 94 return false 95 } 96 } 97 return count == len(one) 98 } 99 100 func TestAddNode(t *testing.T) { 101 sim := New(noopServiceFuncMap) 102 defer sim.Close() 103 104 id, err := sim.AddNode() 105 if err != nil { 106 t.Fatal(err) 107 } 108 109 n := sim.Net.GetNode(id) 110 if n == nil { 111 t.Fatal("node not found") 112 } 113 114 if !n.Up { 115 t.Error("node not started") 116 } 117 } 118 119 func TestAddNodeWithMsgEvents(t *testing.T) { 120 sim := New(noopServiceFuncMap) 121 defer sim.Close() 122 123 id, err := sim.AddNode(AddNodeWithMsgEvents(true)) 124 if err != nil { 125 t.Fatal(err) 126 } 127 128 if !sim.Net.GetNode(id).Config.EnableMsgEvents { 129 t.Error("EnableMsgEvents is false") 130 } 131 132 id, err = sim.AddNode(AddNodeWithMsgEvents(false)) 133 if err != nil { 134 t.Fatal(err) 135 } 136 137 if sim.Net.GetNode(id).Config.EnableMsgEvents { 138 t.Error("EnableMsgEvents is true") 139 } 140 } 141 142 func TestAddNodeWithService(t *testing.T) { 143 sim := New(map[string]ServiceFunc{ 144 "noop1": noopServiceFunc, 145 "noop2": noopServiceFunc, 146 }) 147 defer sim.Close() 148 149 id, err := sim.AddNode(AddNodeWithService("noop1")) 150 if err != nil { 151 t.Fatal(err) 152 } 153 154 n := sim.Net.GetNode(id).Node.(*adapters.SimNode) 155 if n.Service("noop1") == nil { 156 t.Error("service noop1 not found on node") 157 } 158 if n.Service("noop2") != nil { 159 t.Error("service noop2 should not be found on node") 160 } 161 } 162 163 func TestAddNodes(t *testing.T) { 164 sim := New(noopServiceFuncMap) 165 defer sim.Close() 166 167 nodesCount := 12 168 169 ids, err := sim.AddNodes(nodesCount) 170 if err != nil { 171 t.Fatal(err) 172 } 173 174 count := len(ids) 175 if count != nodesCount { 176 t.Errorf("expected %v nodes, got %v", nodesCount, count) 177 } 178 179 count = len(sim.Net.GetNodes()) 180 if count != nodesCount { 181 t.Errorf("expected %v nodes, got %v", nodesCount, count) 182 } 183 } 184 185 func TestAddNodesAndConnectFull(t *testing.T) { 186 sim := New(noopServiceFuncMap) 187 defer sim.Close() 188 189 n := 12 190 191 ids, err := sim.AddNodesAndConnectFull(n) 192 if err != nil { 193 t.Fatal(err) 194 } 195 196 testFull(t, sim, ids) 197 } 198 199 func TestAddNodesAndConnectChain(t *testing.T) { 200 sim := New(noopServiceFuncMap) 201 defer sim.Close() 202 203 _, err := sim.AddNodesAndConnectChain(12) 204 if err != nil { 205 t.Fatal(err) 206 } 207 208 // add another set of nodes to test 209 // if two chains are connected 210 _, err = sim.AddNodesAndConnectChain(7) 211 if err != nil { 212 t.Fatal(err) 213 } 214 215 testChain(t, sim, sim.UpNodeIDs()) 216 } 217 218 func TestAddNodesAndConnectRing(t *testing.T) { 219 sim := New(noopServiceFuncMap) 220 defer sim.Close() 221 222 ids, err := sim.AddNodesAndConnectRing(12) 223 if err != nil { 224 t.Fatal(err) 225 } 226 227 testRing(t, sim, ids) 228 } 229 230 func TestAddNodesAndConnectStar(t *testing.T) { 231 sim := New(noopServiceFuncMap) 232 defer sim.Close() 233 234 ids, err := sim.AddNodesAndConnectStar(12) 235 if err != nil { 236 t.Fatal(err) 237 } 238 239 testStar(t, sim, ids, 0) 240 } 241 242 //To test that uploading a snapshot works 243 func TestUploadSnapshot(t *testing.T) { 244 log.Debug("Creating simulation") 245 s := New(map[string]ServiceFunc{ 246 "bzz": func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) { 247 addr := network.NewAddr(ctx.Config.Node()) 248 hp := network.NewHiveParams() 249 hp.Discovery = false 250 config := &network.BzzConfig{ 251 OverlayAddr: addr.Over(), 252 UnderlayAddr: addr.Under(), 253 HiveParams: hp, 254 } 255 kad := network.NewKademlia(addr.Over(), network.NewKadParams()) 256 return network.NewBzz(config, kad, nil, nil, nil), nil, nil 257 }, 258 }) 259 defer s.Close() 260 261 nodeCount := 16 262 log.Debug("Uploading snapshot") 263 err := s.UploadSnapshot(fmt.Sprintf("../stream/testing/snapshot_%d.json", nodeCount)) 264 if err != nil { 265 t.Fatalf("Error uploading snapshot to simulation network: %v", err) 266 } 267 268 ctx := context.Background() 269 log.Debug("Starting simulation...") 270 s.Run(ctx, func(ctx context.Context, sim *Simulation) error { 271 log.Debug("Checking") 272 nodes := sim.UpNodeIDs() 273 if len(nodes) != nodeCount { 274 t.Fatal("Simulation network node number doesn't match snapshot node number") 275 } 276 return nil 277 }) 278 log.Debug("Done.") 279 } 280 281 func TestPivotNode(t *testing.T) { 282 sim := New(noopServiceFuncMap) 283 defer sim.Close() 284 285 id, err := sim.AddNode() 286 if err != nil { 287 t.Fatal(err) 288 } 289 290 id2, err := sim.AddNode() 291 if err != nil { 292 t.Fatal(err) 293 } 294 295 if sim.PivotNodeID() != nil { 296 t.Error("expected no pivot node") 297 } 298 299 sim.SetPivotNode(id) 300 301 pid := sim.PivotNodeID() 302 303 if pid == nil { 304 t.Error("pivot node not set") 305 } else if *pid != id { 306 t.Errorf("expected pivot node %s, got %s", id, *pid) 307 } 308 309 sim.SetPivotNode(id2) 310 311 pid = sim.PivotNodeID() 312 313 if pid == nil { 314 t.Error("pivot node not set") 315 } else if *pid != id2 { 316 t.Errorf("expected pivot node %s, got %s", id2, *pid) 317 } 318 } 319 320 func TestStartStopNode(t *testing.T) { 321 sim := New(noopServiceFuncMap) 322 defer sim.Close() 323 324 id, err := sim.AddNode() 325 if err != nil { 326 t.Fatal(err) 327 } 328 329 n := sim.Net.GetNode(id) 330 if n == nil { 331 t.Fatal("node not found") 332 } 333 if !n.Up { 334 t.Error("node not started") 335 } 336 337 err = sim.StopNode(id) 338 if err != nil { 339 t.Fatal(err) 340 } 341 if n.Up { 342 t.Error("node not stopped") 343 } 344 345 // Sleep here to ensure that Network.watchPeerEvents defer function 346 // has set the `node.Up = false` before we start the node again. 347 // p2p/simulations/network.go:215 348 // 349 // The same node is stopped and started again, and upon start 350 // watchPeerEvents is started in a goroutine. If the node is stopped 351 // and then very quickly started, that goroutine may be scheduled later 352 // then start and force `node.Up = false` in its defer function. 353 // This will make this test unreliable. 354 time.Sleep(time.Second) 355 356 err = sim.StartNode(id) 357 if err != nil { 358 t.Fatal(err) 359 } 360 if !n.Up { 361 t.Error("node not started") 362 } 363 } 364 365 func TestStartStopRandomNode(t *testing.T) { 366 sim := New(noopServiceFuncMap) 367 defer sim.Close() 368 369 _, err := sim.AddNodes(3) 370 if err != nil { 371 t.Fatal(err) 372 } 373 374 id, err := sim.StopRandomNode() 375 if err != nil { 376 t.Fatal(err) 377 } 378 379 n := sim.Net.GetNode(id) 380 if n == nil { 381 t.Fatal("node not found") 382 } 383 if n.Up { 384 t.Error("node not stopped") 385 } 386 387 id2, err := sim.StopRandomNode() 388 if err != nil { 389 t.Fatal(err) 390 } 391 392 // Sleep here to ensure that Network.watchPeerEvents defer function 393 // has set the `node.Up = false` before we start the node again. 394 // p2p/simulations/network.go:215 395 // 396 // The same node is stopped and started again, and upon start 397 // watchPeerEvents is started in a goroutine. If the node is stopped 398 // and then very quickly started, that goroutine may be scheduled later 399 // then start and force `node.Up = false` in its defer function. 400 // This will make this test unreliable. 401 time.Sleep(time.Second) 402 403 idStarted, err := sim.StartRandomNode() 404 if err != nil { 405 t.Fatal(err) 406 } 407 408 if idStarted != id && idStarted != id2 { 409 t.Error("unexpected started node ID") 410 } 411 } 412 413 func TestStartStopRandomNodes(t *testing.T) { 414 sim := New(noopServiceFuncMap) 415 defer sim.Close() 416 417 _, err := sim.AddNodes(10) 418 if err != nil { 419 t.Fatal(err) 420 } 421 422 ids, err := sim.StopRandomNodes(3) 423 if err != nil { 424 t.Fatal(err) 425 } 426 427 for _, id := range ids { 428 n := sim.Net.GetNode(id) 429 if n == nil { 430 t.Fatal("node not found") 431 } 432 if n.Up { 433 t.Error("node not stopped") 434 } 435 } 436 437 // Sleep here to ensure that Network.watchPeerEvents defer function 438 // has set the `node.Up = false` before we start the node again. 439 // p2p/simulations/network.go:215 440 // 441 // The same node is stopped and started again, and upon start 442 // watchPeerEvents is started in a goroutine. If the node is stopped 443 // and then very quickly started, that goroutine may be scheduled later 444 // then start and force `node.Up = false` in its defer function. 445 // This will make this test unreliable. 446 time.Sleep(time.Second) 447 448 ids, err = sim.StartRandomNodes(2) 449 if err != nil { 450 t.Fatal(err) 451 } 452 453 for _, id := range ids { 454 n := sim.Net.GetNode(id) 455 if n == nil { 456 t.Fatal("node not found") 457 } 458 if !n.Up { 459 t.Error("node not started") 460 } 461 } 462 }