Skip to content

Commit

Permalink
Use wait instead of a sleep to check for startup delay
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
dmcgowan committed Apr 6, 2023
1 parent 3c6ddee commit a7fddb4
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions gc/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down

0 comments on commit a7fddb4

Please sign in to comment.