Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
test: deflake DontHaveTimeout tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed May 17, 2021
1 parent ca847df commit 978ffe3
Showing 1 changed file with 56 additions and 36 deletions.
92 changes: 56 additions & 36 deletions internal/messagequeue/donthavetimeoutmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,16 @@ func TestDontHaveTimeoutMgrCancel(t *testing.T) {
cancelCount := 1
dhtm.CancelPending(ks[:cancelCount])

// Wait for the expected timeout
time.Sleep(expectedTimeout)
for i := 0; i < 5; i++ {
// Wait for the expected timeout
time.Sleep(expectedTimeout)

// At this stage all non-cancelled keys should have timed out
if tr.timedOutCount() != len(ks)-cancelCount {
t.Fatal("expected timeout")
// At this stage all non-cancelled keys should have timed out
if tr.timedOutCount() == len(ks)-cancelCount {
return
}
}
t.Fatal("expected timeout")
}

func TestDontHaveTimeoutWantCancelWant(t *testing.T) {
Expand Down Expand Up @@ -186,13 +189,16 @@ func TestDontHaveTimeoutWantCancelWant(t *testing.T) {
t.Fatal("expected one key to timeout")
}

// Wait till after added back key should time out
time.Sleep(latency)
for i := 0; i < 5; i++ {
// Wait till after added back key should time out
time.Sleep(latency)

// At this stage the key that was added back should also have timed out
if tr.timedOutCount() != 2 {
t.Fatal("expected added back key to timeout")
// At this stage the key that was added back should also have timed out
if tr.timedOutCount() == 2 {
return
}
}
t.Fatal("expected added back key to timeout")
}

func TestDontHaveTimeoutRepeatedAddPending(t *testing.T) {
Expand All @@ -213,13 +219,16 @@ func TestDontHaveTimeoutRepeatedAddPending(t *testing.T) {
dhtm.AddPending([]cid.Cid{c})
}

// Wait for the expected timeout
time.Sleep(latency + 5*time.Millisecond)
for i := 0; i < 5; i++ {
// Wait for the expected timeout
time.Sleep(latency + 5*time.Millisecond)

// At this stage all keys should have timed out
if tr.timedOutCount() != len(ks) {
t.Fatal("expected timeout")
// At this stage all keys should have timed out
if tr.timedOutCount() == len(ks) {
return
}
}
t.Fatal("expected timeout")
}

func TestDontHaveTimeoutMgrMessageLatency(t *testing.T) {
Expand Down Expand Up @@ -258,12 +267,15 @@ func TestDontHaveTimeoutMgrMessageLatency(t *testing.T) {
// We've already slept for 25ms so with the new 15ms timeout
// the keys should have timed out

// Give the queue some time to process the updates
time.Sleep(5 * time.Millisecond)
for i := 0; i < 5; i++ {
// Give the queue some time to process the updates
time.Sleep(5 * time.Millisecond)

if tr.timedOutCount() != len(ks) {
t.Fatal("expected keys to timeout")
if tr.timedOutCount() == len(ks) {
return
}
}
t.Fatal("expected keys to timeout")
}

func TestDontHaveTimeoutMgrMessageLatencyMax(t *testing.T) {
Expand All @@ -285,13 +297,17 @@ func TestDontHaveTimeoutMgrMessageLatencyMax(t *testing.T) {
// than the maximum timeout
dhtm.UpdateMessageLatency(testMaxTimeout * 4)

// Sleep until just after the maximum timeout
time.Sleep(testMaxTimeout + 5*time.Millisecond)
// Sleep until the max timeout.
time.Sleep(testMaxTimeout)

// Keys should have timed out
if tr.timedOutCount() != len(ks) {
t.Fatal("expected keys to timeout")
for i := 0; i < 5; i++ {
// Keys should have timed out
if tr.timedOutCount() == len(ks) {
return
}
time.Sleep(5 * time.Millisecond)
}
t.Fatal("expected keys to timeout")
}

func TestDontHaveTimeoutMgrUsesDefaultTimeoutIfPingError(t *testing.T) {
Expand Down Expand Up @@ -320,13 +336,16 @@ func TestDontHaveTimeoutMgrUsesDefaultTimeoutIfPingError(t *testing.T) {
t.Fatal("expected timeout not to have happened yet")
}

// Sleep until after the expected timeout
time.Sleep(10 * time.Millisecond)
for i := 0; i < 5; i++ {
// Sleep until after the expected timeout
time.Sleep(5 * time.Millisecond)

// Now the keys should have timed out
if tr.timedOutCount() != len(ks) {
t.Fatal("expected timeout")
// Now the keys should have timed out
if tr.timedOutCount() == len(ks) {
return
}
}
t.Fatal("expected timeout")
}

func TestDontHaveTimeoutMgrUsesDefaultTimeoutIfLatencyLonger(t *testing.T) {
Expand Down Expand Up @@ -354,13 +373,15 @@ func TestDontHaveTimeoutMgrUsesDefaultTimeoutIfLatencyLonger(t *testing.T) {
t.Fatal("expected timeout not to have happened yet")
}

// Sleep until after the default timeout
time.Sleep(defaultTimeout * 2)
for i := 0; i < 5; i++ {
time.Sleep(5 * time.Millisecond)

// Now the keys should have timed out
if tr.timedOutCount() != len(ks) {
t.Fatal("expected timeout")
// Now the keys should have timed out
if tr.timedOutCount() == len(ks) {
return
}
}
t.Fatal("expected timeout")
}

func TestDontHaveTimeoutNoTimeoutAfterShutdown(t *testing.T) {
Expand All @@ -385,8 +406,7 @@ func TestDontHaveTimeoutNoTimeoutAfterShutdown(t *testing.T) {
// Shutdown the manager
dhtm.Shutdown()

// Wait for the expected timeout
time.Sleep(10 * time.Millisecond)
time.Sleep(20 * time.Millisecond)

// Manager was shut down so timeout should not have fired
if tr.timedOutCount() != 0 {
Expand Down

0 comments on commit 978ffe3

Please sign in to comment.