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 flaky TestBasicDialSync #1486

Closed
wants to merge 2 commits into from
Closed
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
41 changes: 29 additions & 12 deletions p2p/net/swarm/dial_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,23 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this a pointer

requests := make(chan dialRequest, 2)
done := make(chan struct{})
dsync := newDialSync(func(id peer.ID, reqs <-chan dialRequest) {
fmt.Println("worker func")
require.Equal(t, id, p)
atomic.AddInt32(&counter, 1)
for req := range reqs {
requests <- req
go func(req dialRequest) {
<-done
fmt.Println("sending response")
req.resch <- dialResponse{conn: new(Conn)}
}(req)
}
})

finished := make(chan struct{}, 2)
go func() {
Expand All @@ -52,16 +66,19 @@ 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()

<-finished
<-finished

if len(callsch) > 1 {
t.Fatal("should only have called dial func once!")
}
// 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
now := time.Now()
require.Eventually(t, func() bool {
l := len(finished)
fmt.Printf("waited %s: %d\n", time.Since(now), l)
return l == 2
}, 100*time.Millisecond, 10*time.Millisecond, "dial functions should have returned")

require.Equal(t, 1, int(atomic.LoadInt32(&counter)), "should only have called dial func once!")
}

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