Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce flakiness of backoff cache tests #1415

Merged
merged 1 commit into from
Apr 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions p2p/discovery/backoff/backoffcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ import (
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
)

func scaleDuration(t time.Duration) time.Duration {
if os.Getenv("CI") != "" {
return 3 * t
}
return t
}

type delayedDiscovery struct {
disc discovery.Discovery
delay time.Duration
Expand Down Expand Up @@ -78,8 +85,15 @@ func TestBackoffDiscoverySingleBackoff(t *testing.T) {
d1 := mocks.NewDiscoveryClient(h1, discServer)
d2 := mocks.NewDiscoveryClient(h2, discServer)

bkf := NewExponentialBackoff(time.Millisecond*100, time.Second*10, NoJitter,
time.Millisecond*100, 2.5, 0, rand.NewSource(0))
bkf := NewExponentialBackoff(
scaleDuration(time.Millisecond*100),
scaleDuration(time.Second*10),
NoJitter,
scaleDuration(time.Millisecond*100),
2.5,
0,
rand.NewSource(0),
)
dCache, err := NewBackoffDiscovery(d1, bkf)
if err != nil {
t.Fatal(err)
Expand All @@ -96,18 +110,11 @@ func TestBackoffDiscoverySingleBackoff(t *testing.T) {
assertNumPeers(t, ctx, dCache, ns, 1)

// wait for cache to expire and check for the new peer
time.Sleep(time.Millisecond * 110)
time.Sleep(scaleDuration(time.Millisecond * 110))
assertNumPeers(t, ctx, dCache, ns, 2)
}

func TestBackoffDiscoveryMultipleBackoff(t *testing.T) {
scaleDuration := func(t time.Duration) time.Duration {
if os.Getenv("CI") != "" {
return 3 * t
}
return t
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down Expand Up @@ -179,9 +186,9 @@ func TestBackoffDiscoverySimultaneousQuery(t *testing.T) {
advertisers[i] = mocks.NewDiscoveryClient(h, discServer)
}

d1 := &delayedDiscovery{advertisers[0], time.Millisecond * 10}
d1 := &delayedDiscovery{advertisers[0], scaleDuration(time.Millisecond * 10)}

bkf := NewFixedBackoff(time.Millisecond * 200)
bkf := NewFixedBackoff(scaleDuration(time.Millisecond * 200))
dCache, err := NewBackoffDiscovery(d1, bkf)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -241,7 +248,7 @@ func TestBackoffDiscoveryCacheCapacity(t *testing.T) {
h1 := bhost.NewBlankHost(swarmt.GenSwarm(t))
d1 := mocks.NewDiscoveryClient(h1, discServer)

const discoveryInterval = time.Millisecond * 100
discoveryInterval := scaleDuration(time.Millisecond * 10)

bkf := NewFixedBackoff(discoveryInterval)
dCache, err := NewBackoffDiscovery(d1, bkf)
Expand Down Expand Up @@ -271,7 +278,7 @@ func TestBackoffDiscoveryCacheCapacity(t *testing.T) {
assertNumPeersWithLimit(t, ctx, dCache, ns, n-1, n-1)

// Wait for next discovery so next request will bypass cache
time.Sleep(time.Millisecond * 100)
time.Sleep(scaleDuration(time.Millisecond * 100))

// Ask for all peers again
assertNumPeersWithLimit(t, ctx, dCache, ns, n, n)
Expand Down