Skip to content

Commit

Permalink
fix(p2p/pex): fix TestConnectionSpeedForPeerReceivedFromSeed (backport
Browse files Browse the repository at this point in the history
…cometbft#4660) (cometbft#4661)

<hr>This is an automatic backport of pull request cometbft#4660 done by
[Mergify](https://mergify.com).

---------

Co-authored-by: Anton Kaliaev <[email protected]>
  • Loading branch information
mergify[bot] and melekes authored Dec 13, 2024
1 parent 6ecf6d1 commit fef5452
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions p2p/pex/pex_reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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) {
Expand Down Expand Up @@ -567,8 +564,9 @@ func assertPeersWithTimeout(

var (
ticker = time.NewTicker(checkPeriod)
remaining = timeout
timeoutCh = time.After(timeout)
)
defer ticker.Stop()

for {
select {
Expand All @@ -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()
Expand Down

0 comments on commit fef5452

Please sign in to comment.