@@ -339,16 +339,7 @@ func TestStartStopNode(t *testing.T) {
339339 t .Error ("node not stopped" )
340340 }
341341
342- // Sleep here to ensure that Network.watchPeerEvents defer function
343- // has set the `node.Up = false` before we start the node again.
344- // p2p/simulations/network.go:215
345- //
346- // The same node is stopped and started again, and upon start
347- // watchPeerEvents is started in a goroutine. If the node is stopped
348- // and then very quickly started, that goroutine may be scheduled later
349- // then start and force `node.Up = false` in its defer function.
350- // This will make this test unreliable.
351- time .Sleep (time .Second )
342+ waitForPeerEventPropagation ()
352343
353344 err = sim .StartNode (id )
354345 if err != nil {
@@ -386,16 +377,7 @@ func TestStartStopRandomNode(t *testing.T) {
386377 t .Fatal (err )
387378 }
388379
389- // Sleep here to ensure that Network.watchPeerEvents defer function
390- // has set the `node.Up = false` before we start the node again.
391- // p2p/simulations/network.go:215
392- //
393- // The same node is stopped and started again, and upon start
394- // watchPeerEvents is started in a goroutine. If the node is stopped
395- // and then very quickly started, that goroutine may be scheduled later
396- // then start and force `node.Up = false` in its defer function.
397- // This will make this test unreliable.
398- time .Sleep (time .Second )
380+ waitForPeerEventPropagation ()
399381
400382 idStarted , err := sim .StartRandomNode ()
401383 if err != nil {
@@ -431,16 +413,7 @@ func TestStartStopRandomNodes(t *testing.T) {
431413 }
432414 }
433415
434- // Sleep here to ensure that Network.watchPeerEvents defer function
435- // has set the `node.Up = false` before we start the node again.
436- // p2p/simulations/network.go:215
437- //
438- // The same node is stopped and started again, and upon start
439- // watchPeerEvents is started in a goroutine. If the node is stopped
440- // and then very quickly started, that goroutine may be scheduled later
441- // then start and force `node.Up = false` in its defer function.
442- // This will make this test unreliable.
443- time .Sleep (time .Second )
416+ waitForPeerEventPropagation ()
444417
445418 ids , err = sim .StartRandomNodes (2 )
446419 if err != nil {
@@ -457,3 +430,15 @@ func TestStartStopRandomNodes(t *testing.T) {
457430 }
458431 }
459432}
433+
434+ func waitForPeerEventPropagation () {
435+ // Sleep here to ensure that Network.watchPeerEvents defer function
436+ // has set the `node.Up() = false` before we start the node again.
437+ //
438+ // The same node is stopped and started again, and upon start
439+ // watchPeerEvents is started in a goroutine. If the node is stopped
440+ // and then very quickly started, that goroutine may be scheduled later
441+ // then start and force `node.Up() = false` in its defer function.
442+ // This will make this test unreliable.
443+ time .Sleep (1 * time .Second )
444+ }
0 commit comments