Skip to content

Commit 047e0ca

Browse files
rootulpclaude
andauthored
fix: replace infinite loops with bounded polling in p2p switch tests (#2800)
## Summary - Replace unbounded `for` loops in `TestPeerRemovedFromReactorOnStopWithNilReason` and `TestSwitchInitPeerIsNotCalledBeforeRemovePeer` with `require.Eventually` calls that timeout after 5 seconds - The infinite loops could hang forever if the peer was never added to the switch, causing the test to hit the 15-minute CI timeout Closes #2791 ## Test plan - [x] `go test -v -run "TestSwitchInitPeerIsNotCalledBeforeRemovePeer|TestPeerRemovedFromReactorOnStopWithNilReason" -count=1 -timeout=30s ./p2p/` passes locally 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 60fc8c8 commit 047e0ca

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

p2p/switch_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -823,13 +823,13 @@ func TestSwitchInitPeerIsNotCalledBeforeRemovePeer(t *testing.T) {
823823
require.NoError(t, err)
824824

825825
// wait till the switch adds rp to the peer set, then stop the peer asynchronously
826-
for {
827-
time.Sleep(20 * time.Millisecond)
826+
require.Eventually(t, func() bool {
828827
if peer := sw.Peers().Get(rp.ID()); peer != nil {
829828
go sw.StopPeerForError(peer, "test", "mockReactor")
830-
break
829+
return true
831830
}
832-
}
831+
return false
832+
}, 5*time.Second, 20*time.Millisecond, "peer was never added to switch")
833833

834834
// simulate peer reconnecting to us
835835
_, err = rp.Dial(sw.NetAddress())
@@ -872,12 +872,10 @@ func TestPeerRemovedFromReactorOnStopWithNilReason(t *testing.T) {
872872

873873
// wait till the switch adds rp to the peer set
874874
var peer Peer
875-
for {
876-
time.Sleep(20 * time.Millisecond)
877-
if peer = sw.Peers().Get(rp.ID()); peer != nil {
878-
break
879-
}
880-
}
875+
require.Eventually(t, func() bool {
876+
peer = sw.Peers().Get(rp.ID())
877+
return peer != nil
878+
}, 5*time.Second, 20*time.Millisecond, "peer was never added to switch")
881879

882880
// verify peer is in reactor's peer set
883881
reactorPeerSet := sw.peerSetForReactor(reactor)

0 commit comments

Comments
 (0)