Browse Source

p2p/test: fix Switch test race condition (#4893)

pull/4899/head
Erik Grinaker 4 years ago
committed by GitHub
parent
commit
f4978466b9
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 4 deletions
  1. +3
    -0
      p2p/switch.go
  2. +8
    -4
      p2p/switch_test.go

+ 3
- 0
p2p/switch.go View File

@ -320,6 +320,9 @@ func (sw *Switch) Peers() IPeerSet {
// TODO: make record depending on reason.
func (sw *Switch) StopPeerForError(peer Peer, reason interface{}) {
sw.Logger.Error("Stopping peer for error", "peer", peer, "err", reason)
if peer == nil {
return
}
sw.stopAndRemovePeer(peer, reason)
if peer.IsPersistent() {


+ 8
- 4
p2p/switch_test.go View File

@ -706,11 +706,15 @@ func TestSwitchInitPeerIsNotCalledBeforeRemovePeer(t *testing.T) {
defer rp.Stop()
_, err = rp.Dial(sw.NetAddress())
require.NoError(t, err)
// wait till the switch adds rp to the peer set
time.Sleep(50 * time.Millisecond)
// stop peer asynchronously
go sw.StopPeerForError(sw.Peers().Get(rp.ID()), "test")
// wait till the switch adds rp to the peer set, then stop the peer asynchronously
for {
time.Sleep(20 * time.Millisecond)
if peer := sw.Peers().Get(rp.ID()); peer != nil {
go sw.StopPeerForError(peer, "test")
break
}
}
// simulate peer reconnecting to us
_, err = rp.Dial(sw.NetAddress())


Loading…
Cancel
Save