Skip to content

Commit

Permalink
swarm: fix race condition in TestFailFirst (#1490)
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann authored May 16, 2022
1 parent 5c218cf commit c51c1b6
Showing 1 changed file with 10 additions and 13 deletions.
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

0 comments on commit c51c1b6

Please sign in to comment.