From a7fddb40bca9bc06f3ac3199df3e861dd00f1bcc Mon Sep 17 00:00:00 2001 From: Derek McGowan Date: Wed, 5 Apr 2023 18:14:28 -0700 Subject: [PATCH] Use wait instead of a sleep to check for startup delay Avoid using sleep as a way to measure whether gc has occurred. Some systems may pause execution of the test and cause a failure if the gc thread has not yet run after the sleep in the main thread. Signed-off-by: Derek McGowan --- gc/scheduler/scheduler_test.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/gc/scheduler/scheduler_test.go b/gc/scheduler/scheduler_test.go index 42c8b2ee0..d6d9a61f7 100644 --- a/gc/scheduler/scheduler_test.go +++ b/gc/scheduler/scheduler_test.go @@ -148,10 +148,11 @@ func TestTrigger(t *testing.T) { func TestStartupDelay(t *testing.T) { var ( - cfg = &config{ + startupDelay = time.Millisecond * 20 + cfg = &config{ // Prevent GC from scheduling again before check PauseThreshold: 0.001, - StartupDelay: duration(time.Millisecond), + StartupDelay: duration(startupDelay), } tc = &testCollector{ d: time.Second, @@ -160,13 +161,23 @@ func TestStartupDelay(t *testing.T) { scheduler = newScheduler(tc, cfg) ) defer cancel() - go scheduler.run(ctx) - time.Sleep(time.Millisecond * 30) + t1 := time.Now() + go scheduler.run(ctx) + _, err := scheduler.wait(ctx, false) + if err != nil { + t.Fatalf("gc failed with error: %s", err) + } + d := time.Since(t1) if c := tc.runCount(); c != 1 { t.Fatalf("unexpected gc run count %d, expected 1", c) } + + if d < startupDelay { + t.Fatalf("expected startup delay to be longer than %s, actual %s", startupDelay, d) + } + } type testCollector struct {