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

autorelay: fix race condition in TestBackoff #1731

Merged
merged 1 commit into from
Sep 6, 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
6 changes: 3 additions & 3 deletions p2p/host/autorelay/autorelay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,11 @@ func TestBackoff(t *testing.T) {
str.Reset()
})

var counter int
var counter int32 // to be used atomically
h := newPrivateNode(t,
autorelay.WithPeerSource(func(int) <-chan peer.AddrInfo {
// always return the same node, and make sure we don't try to connect to it too frequently
counter++
atomic.AddInt32(&counter, 1)
peerChan := make(chan peer.AddrInfo, 1)
peerChan <- peer.AddrInfo{ID: r.ID(), Addrs: r.Addrs()}
close(peerChan)
Expand All @@ -264,7 +264,7 @@ func TestBackoff(t *testing.T) {
}
cl.Add(backoff / 2)
require.Eventually(t, func() bool { return atomic.LoadInt32(&reservations) == 2 }, 3*time.Second, 20*time.Millisecond)
require.Less(t, counter, 100) // just make sure we're not busy-looping
require.Less(t, int(atomic.LoadInt32(&counter)), 100) // just make sure we're not busy-looping
require.Equal(t, 2, int(atomic.LoadInt32(&reservations)))
}

Expand Down