Documentation
¶
Overview ¶
Package timeshim provides injectable time for deterministic tests: code takes a Clock instead of calling time.Now and time.AfterFunc, production passes Real, and tests pass a Fake that moves only when told to. The package uses only the standard library.
Index ¶
- Constants
- func After(c Clock, d time.Duration) <-chan time.Time
- func Since(c Clock, t time.Time) time.Duration
- func Sleep(ctx context.Context, c Clock, d time.Duration) error
- func Until(c Clock, t time.Time) time.Duration
- func WithDeadline(ctx context.Context, c Clock, deadline time.Time) (context.Context, context.CancelFunc)
- func WithDeadlineCause(ctx context.Context, c Clock, deadline time.Time, cause error) (context.Context, context.CancelFunc)
- func WithTimeout(ctx context.Context, c Clock, d time.Duration) (context.Context, context.CancelFunc)
- func WithTimeoutCause(ctx context.Context, c Clock, d time.Duration, cause error) (context.Context, context.CancelFunc)
- type ChanTimer
- type Clock
- type Fake
- func (c *Fake) Advance(d time.Duration)
- func (c *Fake) AdvanceTo(t time.Time)
- func (c *Fake) AdvanceToNext() (time.Time, bool)
- func (c *Fake) AfterFunc(d time.Duration, f func()) Timer
- func (c *Fake) Deadlines() []time.Time
- func (c *Fake) Next() (time.Time, bool)
- func (c *Fake) Now() time.Time
- func (c *Fake) Pending() int
- func (c *Fake) WaitDeadline(ctx context.Context, t time.Time) error
- func (c *Fake) WaitPending(ctx context.Context, n int) error
- type Ticker
- type Timer
Constants ¶
const Real realClock = 0
Real is the system clock.
Variables ¶
This section is empty.
Functions ¶
func Sleep ¶
Sleep waits until d has passed on c and returns nil, or ctx's error if ctx ends first. A d of zero or less returns at once, with ctx's error if any.
func WithDeadline ¶
func WithDeadline(ctx context.Context, c Clock, deadline time.Time) (context.Context, context.CancelFunc)
WithDeadline returns a copy of ctx that ends with context.DeadlineExceeded once c reaches deadline, as context.WithDeadline on the system clock. On a Fake the context ends within the Advance that reaches deadline. A ctx that has ended, or a deadline that has passed, gives a context that has ended already, with ctx's error first. The supplied clock drives the requested deadline even when ctx uses another clock. The context also ends when ctx does, or when the returned cancel is called. A parent with a private Done channel may require a waiting goroutine unless it implements AfterFunc.
func WithDeadlineCause ¶
func WithDeadlineCause(ctx context.Context, c Clock, deadline time.Time, cause error) (context.Context, context.CancelFunc)
WithDeadlineCause is like context.WithDeadlineCause on c: when the deadline passes, Err reports context.DeadlineExceeded and context.Cause reports cause.
Types ¶
type ChanTimer ¶
ChanTimer is a timer that sends c's time on C once its time has passed, as a *time.Timer made by time.NewTimer. As with time.Timer since Go 1.23, C is unbuffered in effect: once Stop or Reset returns, no stale time is received from C.
func NewTimer ¶
NewTimer returns a timer that sends on its channel once d has passed on c, as time.NewTimer. On Real it is a *time.Timer underneath. On other clocks a d of zero or less has the value ready on C at once, as does Reset with such a d. A positive d with an unrepresentable target panics.
type Clock ¶
type Clock interface {
// Now returns the current time.
Now() time.Time
// AfterFunc calls f in its own goroutine, or for Fake in Advance,
// once d has passed, as time.AfterFunc does.
AfterFunc(d time.Duration, f func()) Timer
}
Clock tells the time and runs functions after a delay.
type Fake ¶
type Fake struct {
// contains filtered or unexported fields
}
Fake is a clock that moves only by Advance. Its zero value starts at the zero time. It is safe for concurrent use and must not be copied after first use.
func (*Fake) Advance ¶
Advance moves the clock d forward and calls, in order of their times and outside the clock's lock, the functions that are due; a function may set new timers, which are called too when due. The channels of a ChanTimer or Ticker created directly on c receive the scheduled time when Advance ends, so a goroutine woken by one finds the clock at its new time, and no timer it sets fires in the same Advance. Functions due at the same time are called in the order they were armed by AfterFunc or Reset. Time never goes back. A negative d or an unrepresentable target panics. Of concurrent calls, each leaves the clock at least where it took it.
The order holds within one call. Concurrent calls take due functions from the same list, so their functions may run at the same time in different goroutines; drive a Fake from one goroutine to keep a single global order.
func (*Fake) AdvanceTo ¶
AdvanceTo moves the clock forward to t as Advance does. A t that is not after the clock's time only calls the functions already due.
func (*Fake) AdvanceToNext ¶
AdvanceToNext moves the clock to the earliest pending time, calls the functions due then as Advance does, and returns that time. It reports false, and leaves the clock alone, when no function is pending.
func (*Fake) AfterFunc ¶
AfterFunc arranges for f to be called by the Advance that reaches d from now, in the goroutine of Advance. A d of zero or less is due at once: f is called by the next Advance, AdvanceTo or AdvanceToNext, Advance(0) included, rather than in a goroutine of its own as time.AfterFunc does, so that tests stay deterministic and f never runs under the Fake's lock.
Timer.Reset re-arms in the same way. A negative d counts as zero, so Deadlines never reports a time before Now. An unrepresentable target panics before the registration changes.
func (*Fake) WaitDeadline ¶
WaitDeadline waits until a function is pending at exactly t, such as once code under test has armed the one timer a test is about to pass, and returns nil, or ctx's error if ctx ends first. Unlike WaitPending it does not depend on how many other timers are pending. It does not move the clock.
func (*Fake) WaitPending ¶
WaitPending waits until at least n functions wait for their time, such as once code under test running in another goroutine has armed its timers, and returns nil, or ctx's error if ctx ends first. It does not move the clock.
type Ticker ¶
Ticker sends c's time on C every period, as a *time.Ticker: a tick that a slow receiver misses is dropped, and once Stop or Reset returns no stale tick is received.
func NewTicker ¶
NewTicker returns a ticker of period d on c, as time.NewTicker; d must be positive. On clocks other than Real an unrepresentable first tick panics. A Fake ticker retires after its last representable tick. Stop tickers on clocks other than Real to release the clock registration.