Skip to content

Commit 42324b3

Browse files
authored
chore: add clock pkg for testing time (#13461)
Adds a package for testing time/timer/ticker functions. Implementation is limited to `NewTimer` and `NewContextTicker`, but will eventually be expanded to all `time` functions from the standard library as well as `context.WithTimeout()`, `context.WithDeadline()`. Replaces `benbjohnson/clock` for the pubsub watchdog, as a proof of concept. Eventually, as we expand functionality, we will replace most time-related functions with this library for testing.
1 parent a4bba52 commit 42324b3

File tree

6 files changed

+658
-45
lines changed

6 files changed

+658
-45
lines changed

clock/clock.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Package clock is a library for testing time related code. It exports an interface Clock that
2+
// mimics the standard library time package functions. In production, an implementation that calls
3+
// thru to the standard library is used. In testing, a Mock clock is used to precisely control and
4+
// intercept time functions.
5+
package clock
6+
7+
import (
8+
"context"
9+
"time"
10+
)
11+
12+
type Clock interface {
13+
// TickerFunc is a convenience function that calls f on the interval d until either the given
14+
// context expires or f returns an error. Callers may call Wait() on the returned Waiter to
15+
// wait until this happens and obtain the error.
16+
TickerFunc(ctx context.Context, d time.Duration, f func() error, tags ...string) Waiter
17+
// NewTimer creates a new Timer that will send the current time on its channel after at least
18+
// duration d.
19+
NewTimer(d time.Duration, tags ...string) *Timer
20+
}
21+
22+
// Waiter can be waited on for an error.
23+
type Waiter interface {
24+
Wait(tags ...string) error
25+
}

0 commit comments

Comments
 (0)