Skip to content

Commit

Permalink
swarm: fix flaky TestBasicDialSync
Browse files Browse the repository at this point in the history
marten-seemann committed May 18, 2022
1 parent c51c1b6 commit 2a83f6d
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions p2p/net/swarm/dial_sync_test.go
Original file line number Diff line number Diff line change
@@ -33,9 +33,21 @@ func getMockDialFunc() (dialWorkerFunc, func(), context.Context, <-chan struct{}
}

func TestBasicDialSync(t *testing.T) {
df, done, _, callsch := getMockDialFunc()
dsync := newDialSync(df)
p := peer.ID("testpeer")
var counter int32
requests := make(chan dialRequest, 2)
done := make(chan struct{})
dsync := newDialSync(func(id peer.ID, reqs <-chan dialRequest) {
require.Equal(t, id, p)
atomic.AddInt32(&counter, 1)
for req := range reqs {
requests <- req
go func(req dialRequest) {
<-done
req.resch <- dialResponse{conn: new(Conn)}
}(req)
}
})

finished := make(chan struct{}, 2)
go func() {
@@ -52,16 +64,14 @@ func TestBasicDialSync(t *testing.T) {
finished <- struct{}{}
}()

// short sleep just to make sure we've moved around in the scheduler
time.Sleep(time.Millisecond * 20)
done()
// wait until both requests are registered
require.Eventually(t, func() bool { return len(requests) == 2 }, 100*time.Millisecond, 10*time.Millisecond, "expected both requests to be processed")
// make the dials return
close(done)
// make sure the Dial functions return
require.Eventually(t, func() bool { return len(finished) == 2 }, 100*time.Millisecond, 10*time.Millisecond, "dial functions should have returned")

<-finished
<-finished

if len(callsch) > 1 {
t.Fatal("should only have called dial func once!")
}
require.Equal(t, 1, int(atomic.LoadInt32(&counter)), "should only have called dial func once!")
}

func TestDialSyncCancel(t *testing.T) {

0 comments on commit 2a83f6d

Please sign in to comment.