From 978ffe3f202c8e901dc8c1385a3faf7cae118221 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Mon, 17 May 2021 09:01:10 -0700 Subject: [PATCH] test: deflake DontHaveTimeout tests --- .../messagequeue/donthavetimeoutmgr_test.go | 92 +++++++++++-------- 1 file changed, 56 insertions(+), 36 deletions(-) diff --git a/internal/messagequeue/donthavetimeoutmgr_test.go b/internal/messagequeue/donthavetimeoutmgr_test.go index cc0ebb30..540518f1 100644 --- a/internal/messagequeue/donthavetimeoutmgr_test.go +++ b/internal/messagequeue/donthavetimeoutmgr_test.go @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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 {