Skip to content

chore: change mock clock to allow Advance() within timer/tick functions #13500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clock/clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type Clock interface {
Now(tags ...string) time.Time
// Since returns the time elapsed since t. It is shorthand for Clock.Now().Sub(t).
Since(t time.Time, tags ...string) time.Duration
// Until returns the duration until t. It is shorthand for t.Sub(Clock.Now()).
Until(t time.Time, tags ...string) time.Duration
}

// Waiter can be waited on for an error.
Expand Down
69 changes: 67 additions & 2 deletions clock/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestExampleTickerFunc(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

mClock := clock.NewMock()
mClock := clock.NewMock(t)

// Because the ticker is started on a goroutine, we can't immediately start
// advancing the clock, or we will race with the start of the ticker. If we
Expand Down Expand Up @@ -76,9 +76,74 @@ func TestExampleTickerFunc(t *testing.T) {
}

// Now that we know the ticker is started, we can advance the time.
mClock.Advance(time.Hour).MustWait(ctx, t)
mClock.Advance(time.Hour).MustWait(ctx)

if tks := tc.Ticks(); tks != 1 {
t.Fatalf("expected 1 got %d ticks", tks)
}
}

type exampleLatencyMeasurer struct {
mu sync.Mutex
lastLatency time.Duration
}

func newExampleLatencyMeasurer(ctx context.Context, clk clock.Clock) *exampleLatencyMeasurer {
m := &exampleLatencyMeasurer{}
clk.TickerFunc(ctx, 10*time.Second, func() error {
start := clk.Now()
// m.doSomething()
latency := clk.Since(start)
m.mu.Lock()
defer m.mu.Unlock()
m.lastLatency = latency
return nil
})
return m
}

func (m *exampleLatencyMeasurer) LastLatency() time.Duration {
m.mu.Lock()
defer m.mu.Unlock()
return m.lastLatency
}

func TestExampleLatencyMeasurer(t *testing.T) {
t.Parallel()

// nolint:gocritic // trying to avoid Coder-specific stuff with an eye toward spinning this out
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

mClock := clock.NewMock(t)
trap := mClock.Trap().Since()
defer trap.Close()

lm := newExampleLatencyMeasurer(ctx, mClock)

w := mClock.Advance(10 * time.Second) // triggers first tick
c := trap.MustWait(ctx) // call to Since()
mClock.Advance(33 * time.Millisecond)
c.Release()
w.MustWait(ctx)

if l := lm.LastLatency(); l != 33*time.Millisecond {
t.Fatalf("expected 33ms got %s", l.String())
}

// Next tick is in 10s - 33ms, but if we don't want to calculate, we can use:
d, w2 := mClock.AdvanceNext()
c = trap.MustWait(ctx)
mClock.Advance(17 * time.Millisecond)
c.Release()
w2.MustWait(ctx)

expectedD := 10*time.Second - 33*time.Millisecond
if d != expectedD {
t.Fatalf("expected %s got %s", expectedD.String(), d.String())
}

if l := lm.LastLatency(); l != 17*time.Millisecond {
t.Fatalf("expected 17ms got %s", l.String())
}
}
Loading
Loading