From e6e8ef36d2800b4537d5bb72c00af37a223c9cc1 Mon Sep 17 00:00:00 2001 From: Philip O'Toole Date: Wed, 22 Jul 2020 19:58:48 -0400 Subject: [PATCH] Fix Snapshot test If the snapshot count didn't change, the test didn't actually fail. --- store/store_test.go | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/store/store_test.go b/store/store_test.go index 5096fec8..648ff4bd 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -870,13 +870,10 @@ func Test_StoreLogTruncationMultinode(t *testing.T) { } // Wait for the snapshot to happen and log to be truncated. - for { - time.Sleep(1000 * time.Millisecond) - if stats.Get(numSnaphots).String() != nSnaps { - // It's changed, so a snap and truncate has happened. - break - } + f := func() bool { + return stats.Get(numSnaphots).String() != nSnaps } + testPoll(t, f, 100*time.Millisecond, 2*time.Second) // Fire up new node and ensure it picks up all changes. This will // involve getting a snapshot and truncated log. @@ -1203,3 +1200,21 @@ func asJSON(v interface{}) string { } return string(b) } + +func testPoll(t *testing.T, f func() bool, p time.Duration, d time.Duration) { + tck := time.NewTicker(p) + defer tck.Stop() + tmr := time.NewTimer(d) + defer tmr.Stop() + + for { + select { + case <-tck.C: + if f() { + return + } + case <-tmr.C: + t.Fatalf("timeout expired: %s", t.Name()) + } + } +}