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 <derek@mcg.dev>
This commit is contained in:
Derek McGowan 2023-04-05 18:14:28 -07:00
parent 3c6ddee128
commit a7fddb40bc
No known key found for this signature in database
GPG Key ID: F58C5D0A4405ACDB

View File

@ -148,10 +148,11 @@ func TestTrigger(t *testing.T) {
func TestStartupDelay(t *testing.T) { func TestStartupDelay(t *testing.T) {
var ( var (
startupDelay = time.Millisecond * 20
cfg = &config{ cfg = &config{
// Prevent GC from scheduling again before check // Prevent GC from scheduling again before check
PauseThreshold: 0.001, PauseThreshold: 0.001,
StartupDelay: duration(time.Millisecond), StartupDelay: duration(startupDelay),
} }
tc = &testCollector{ tc = &testCollector{
d: time.Second, d: time.Second,
@ -160,13 +161,23 @@ func TestStartupDelay(t *testing.T) {
scheduler = newScheduler(tc, cfg) scheduler = newScheduler(tc, cfg)
) )
defer cancel() 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 { if c := tc.runCount(); c != 1 {
t.Fatalf("unexpected gc run count %d, expected 1", c) 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 { type testCollector struct {