Skip to content

Commit

Permalink
fix flaky test
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed May 23, 2023
1 parent 5e3e8e2 commit 212cd19
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions p2p/net/swarm/dial_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func makeUpgrader(t *testing.T, n *Swarm) transport.Upgrader {
// channel ch will close an accepted connection
func makeTCPListener(t *testing.T, a ma.Multiaddr, recvCh chan struct{}) (list manet.Listener, ch chan struct{}) {
t.Helper()
log.Errorf("listening on %s", a)
list, err := manet.Listen(a)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -523,7 +524,7 @@ func (s schedulingTestCase) Generate(rand *mrand.Rand, size int) reflect.Value {
delays := make(map[time.Duration]struct{})
for i := 0; i < size; i++ {
input[i] = timedDial{
addr: ma.StringCast(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", i+10000)),
addr: ma.StringCast(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", i+10550)),
delay: time.Duration(mrand.Intn(100)) * 10 * time.Millisecond, // max 1 second
success: false,
failAfter: time.Duration(mrand.Intn(100)) * 10 * time.Millisecond, // max 1 second
Expand Down Expand Up @@ -596,7 +597,6 @@ func checkDialWorkerLoopScheduling(t *testing.T, s1, s2 *Swarm, tc schedulingTes
f := func() {
err := l.Close()
if err != nil {
log.Error(err)
t.Error(err)
}
}
Expand Down Expand Up @@ -668,7 +668,9 @@ loop:
if r.conn == nil {
return errors.New("expected connection to succeed")
}
case <-time.After(1 * time.Second):
// High timeout here is okay. We will exit whenever the other branch
// is triggered
case <-time.After(10 * time.Second):
return errors.New("expected to receive a response")
}
connected = true
Expand All @@ -679,7 +681,9 @@ loop:
case <-recvCh:
case <-resch:
return errors.New("didn't expect a response")
case <-time.After(100 * time.Millisecond):
// High timeout here is okay. We will exit whenever the other branch
// is triggered
case <-time.After(10 * time.Second):
return errors.New("didn't receive a dial attempt notification")
}
failDials[a] = dialState{
Expand Down Expand Up @@ -899,6 +903,9 @@ func TestDialWorkerLoopSchedulingProperty(t *testing.T) {
// setup the ranker to trigger dials according to the test case
s1.dialRanker = makeRanker(tc.input)
err := checkDialWorkerLoopScheduling(t, s1, s2, tc)
if err != nil {
log.Error(err)
}
return err == nil
}

Expand Down

0 comments on commit 212cd19

Please sign in to comment.