Skip to content

Commit

Permalink
Merge pull request #1318 from libp2p/fix/flaky-rcmgr-test
Browse files Browse the repository at this point in the history
deflake resource manager tests, take 2
  • Loading branch information
vyzo committed Feb 6, 2022
2 parents 807e6b0 + 55ec721 commit 392cf96
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions itest/rcmgr_test.go
Expand Up @@ -12,7 +12,10 @@ import (
rcmgr "github.com/libp2p/go-libp2p-resource-manager"

"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"

"github.com/stretchr/testify/require"
)

func makeRcmgrOption(t *testing.T, limiter *rcmgr.BasicLimiter, test string) func(int) libp2p.Option {
Expand All @@ -37,12 +40,19 @@ func closeRcmgrs(echos []*Echo) {
}
}

func waitForConnection(t *testing.T, src, dest *Echo) {
require.Eventually(t, func() bool {
return src.Host.Network().Connectedness(dest.Host.ID()) == network.Connected &&
dest.Host.Network().Connectedness(src.Host.ID()) == network.Connected
}, time.Second, time.Millisecond)
}

func TestResourceManagerConnInbound(t *testing.T) {
// this test checks that we can not exceed the inbound conn limit at system level
// we specify: 1 conn per peer, 3 conns total, and we try to create 4 conns
limiter := rcmgr.NewDefaultLimiter()
limiter.SystemLimits = limiter.SystemLimits.WithConnLimit(3, 1024, 1024)
limiter.DefaultPeerLimits = limiter.DefaultPeerLimits.WithConnLimit(1, 16, 16)
limiter.DefaultPeerLimits = limiter.DefaultPeerLimits.WithConnLimit(1, 1, 1)

echos := createEchos(t, 5, makeRcmgrOption(t, limiter, "TestResourceManagerConnInbound"))
defer closeEchos(echos)
Expand All @@ -53,7 +63,7 @@ func TestResourceManagerConnInbound(t *testing.T) {
if err != nil {
t.Fatal(err)
}
time.Sleep(10 * time.Millisecond)
waitForConnection(t, echos[i], echos[0])
}

for i := 1; i < 4; i++ {
Expand All @@ -74,7 +84,7 @@ func TestResourceManagerConnOutbound(t *testing.T) {
// we specify: 1 conn per peer, 3 conns total, and we try to create 4 conns
limiter := rcmgr.NewDefaultLimiter()
limiter.SystemLimits = limiter.SystemLimits.WithConnLimit(1024, 3, 1024)
limiter.DefaultPeerLimits = limiter.DefaultPeerLimits.WithConnLimit(16, 1, 16)
limiter.DefaultPeerLimits = limiter.DefaultPeerLimits.WithConnLimit(1, 1, 1)
echos := createEchos(t, 5, makeRcmgrOption(t, limiter, "TestResourceManagerConnOutbound"))
defer closeEchos(echos)
defer closeRcmgrs(echos)
Expand All @@ -84,7 +94,7 @@ func TestResourceManagerConnOutbound(t *testing.T) {
if err != nil {
t.Fatal(err)
}
time.Sleep(10 * time.Millisecond)
waitForConnection(t, echos[0], echos[i])
}

for i := 1; i < 4; i++ {
Expand Down Expand Up @@ -114,7 +124,7 @@ func TestResourceManagerServiceInbound(t *testing.T) {
if err != nil {
t.Fatal(err)
}
time.Sleep(10 * time.Millisecond)
waitForConnection(t, echos[i], echos[0])
}

ready := make(chan struct{})
Expand Down Expand Up @@ -167,7 +177,7 @@ func TestResourceManagerServicePeerInbound(t *testing.T) {
if err != nil {
t.Fatal(err)
}
time.Sleep(10 * time.Millisecond)
waitForConnection(t, echos[i], echos[0])
}

echos[0].BeforeDone(waitForBarrier(4, time.Minute))
Expand Down

0 comments on commit 392cf96

Please sign in to comment.