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

swarm: fix race condition in TestFailFirst #1490

Merged
merged 1 commit into from
May 16, 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
23 changes: 10 additions & 13 deletions p2p/net/swarm/dial_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"time"

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

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

func getMockDialFunc() (dialWorkerFunc, func(), context.Context, <-chan struct{}) {
Expand Down Expand Up @@ -161,6 +163,7 @@ func TestDialSyncAllCancel(t *testing.T) {

func TestFailFirst(t *testing.T) {
var count int32
dialErr := fmt.Errorf("gophers ate the modem")
f := func(p peer.ID, reqch <-chan dialRequest) {
go func() {
for {
Expand All @@ -169,12 +172,11 @@ func TestFailFirst(t *testing.T) {
return
}

if atomic.LoadInt32(&count) > 0 {
req.resch <- dialResponse{conn: new(Conn)}
if atomic.CompareAndSwapInt32(&count, 0, 1) {
req.resch <- dialResponse{err: dialErr}
} else {
req.resch <- dialResponse{err: fmt.Errorf("gophers ate the modem")}
req.resch <- dialResponse{conn: new(Conn)}
}
atomic.AddInt32(&count, 1)
}
}()
}
Expand All @@ -185,17 +187,12 @@ func TestFailFirst(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

if _, err := ds.Dial(ctx, p); err == nil {
t.Fatal("expected gophers to have eaten the modem")
}
_, err := ds.Dial(ctx, p)
require.ErrorIs(t, err, dialErr, "expected gophers to have eaten the modem")

c, err := ds.Dial(ctx, p)
if err != nil {
t.Fatal(err)
}
if c == nil {
t.Fatal("should have gotten a 'real' conn back")
}
require.NoError(t, err)
require.NotNil(t, c, "should have gotten a 'real' conn back")
}

func TestStressActiveDial(t *testing.T) {
Expand Down