Skip to content

Commit

Permalink
testClock/Sleep: Guard against fast advance
Browse files Browse the repository at this point in the history
The same check we have elsewhere with AwaitScheduled
is needed for the end-to-end Clock test.
  • Loading branch information
abhinav committed Feb 13, 2024
1 parent 5ed3bec commit 400c025
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion internal/fxclock/clock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,21 @@ func testClock(t *testing.T, clock Clock, advance func(d time.Duration)) {

t.Run("Sleep", func(t *testing.T) {
start := clock.Now()
go advance(1 * time.Millisecond)

go func() {
// For the mock clock, there's a chance that advance will be
// too fast and the Sleep will block forever, waiting for
// another advance. The mock clock provides
// AwaitScheduled to help with this.
//
// Since that function is not available on the system clock,
// we'll use upcasting to check for it.
if awaiter, ok := clock.(interface{ AwaitScheduled(int) }); ok {
awaiter.AwaitScheduled(1)
}

advance(1 * time.Millisecond)
}()
clock.Sleep(1 * time.Millisecond)

assert.NotZero(t, clock.Since(start), "time must have advanced")
Expand Down

0 comments on commit 400c025

Please sign in to comment.