From fef54527c7d84e12972ed4ed35180695eef1b75b Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 13 Dec 2024 11:55:16 +0000 Subject: [PATCH] fix(p2p/pex): fix TestConnectionSpeedForPeerReceivedFromSeed (backport #4660) (#4661)
This is an automatic backport of pull request #4660 done by [Mergify](https://mergify.com). --------- Co-authored-by: Anton Kaliaev --- p2p/pex/pex_reactor_test.go | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/p2p/pex/pex_reactor_test.go b/p2p/pex/pex_reactor_test.go index 216c461912a..cf3e491dbff 100644 --- a/p2p/pex/pex_reactor_test.go +++ b/p2p/pex/pex_reactor_test.go @@ -271,14 +271,11 @@ func TestConnectionSpeedForPeerReceivedFromSeed(t *testing.T) { require.NoError(t, err) defer os.RemoveAll(dir) - // Default is 10, we need one connection for the seed node. - cfg.MaxNumOutboundPeers = 2 - var id int var knownAddrs []*p2p.NetAddress // 1. Create some peers - for id = 0; id < cfg.MaxNumOutboundPeers+1; id++ { + for id = 0; id < 3; id++ { peer := testCreateDefaultPeer(dir, id) require.NoError(t, peer.Start()) addr := peer.NetAddress() @@ -305,14 +302,14 @@ func TestConnectionSpeedForPeerReceivedFromSeed(t *testing.T) { assertPeersWithTimeout(t, []*p2p.Switch{node}, 3*time.Second, 1) // 5. Check that the node connects to the peers reported by the seed node - assertPeersWithTimeout(t, []*p2p.Switch{node}, 10*time.Second, cfg.MaxNumOutboundPeers) + assertPeersWithTimeout(t, []*p2p.Switch{node}, 10*time.Second, 2) // 6. Assert that the configured maximum number of inbound/outbound peers // are respected, see https://github.com/cometbft/cometbft/issues/486 outbound, inbound, dialing := node.NumPeers() assert.LessOrEqual(t, inbound, cfg.MaxNumInboundPeers) assert.LessOrEqual(t, outbound, cfg.MaxNumOutboundPeers) - assert.Zero(t, dialing) + assert.LessOrEqual(t, dialing, cfg.MaxNumOutboundPeers+cfg.MaxNumInboundPeers-outbound-inbound) } func TestPEXReactorSeedMode(t *testing.T) { @@ -567,8 +564,9 @@ func assertPeersWithTimeout( var ( ticker = time.NewTicker(checkPeriod) - remaining = timeout + timeoutCh = time.After(timeout) ) + defer ticker.Stop() for { select { @@ -582,14 +580,10 @@ func assertPeersWithTimeout( break } } - remaining -= checkPeriod - if remaining < 0 { - remaining = 0 - } if allGood { return } - case <-time.After(remaining): + case <-timeoutCh: numPeersStr := "" for i, s := range switches { outbound, inbound, _ := s.NumPeers()