Documentation
¶
Overview ¶
Package clocktest is the test clock of the module: Fake, a clock.Clock whose time moves only when the test moves it.
It follows matter.js's MockTime (packages/testing/src/mocks/time.ts): timers are callbacks at a virtual instant, Fake.Advance runs the ones due in the interval in time order and awaits each, Fake.Yield lets the work they started proceed, and Fake.Resolve moves time forward until a pending operation completes. Go has no microtask queue, so Yield is a scheduler hint and a short real pause rather than a guarantee; a test that needs one specific goroutine to have run waits for its effect.
Index ¶
- type Fake
- func (f *Fake) Advance(d time.Duration)
- func (f *Fake) AdvanceToNext() (time.Duration, bool)
- func (f *Fake) AfterFunc(d time.Duration, fn func()) clock.Timer
- func (f *Fake) Armed() []time.Duration
- func (f *Fake) Monotonic() time.Duration
- func (f *Fake) NewTicker(d time.Duration) clock.Ticker
- func (f *Fake) NewTimer(d time.Duration) clock.Timer
- func (f *Fake) Next() (time.Time, bool)
- func (f *Fake) Now() time.Time
- func (f *Fake) Pending() int
- func (f *Fake) Resolve(ctx context.Context, wait func() error) error
- func (f *Fake) Set(t time.Time)
- func (f *Fake) StepWall(d time.Duration)
- func (f *Fake) Yield()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Fake ¶
type Fake struct {
// contains filtered or unexported fields
}
Fake is a clock.Clock driven by the test. Its zero value is not ready; use New. All methods are safe for concurrent use.
func (*Fake) Advance ¶
Advance moves the fake forward by d. Every timer and ticker tick due up to the new time fires in time order, the clock reading each one's deadline as it fires — a timer a callback arms inside the interval fires in the same call. An AfterFunc callback runs in its own goroutine and Advance waits for it to return before the next one fires (MockTime awaits each); a channel timer or ticker sends without blocking, as the standard library does, so a slow receiver misses ticks. Advance returns when every due callback has returned, the clock reading the new time.
A callback must not call Advance on the same fake: it would wait for itself.
func (*Fake) AdvanceToNext ¶
AdvanceToNext moves the fake to the earliest armed deadline and no further: what is due at that instant fires as Fake.Advance fires it, a timer a callback arms for the same instant included; a timer armed for later waits for the next call. It reports the step and true, or zero and false when nothing is armed — the clock then stays where it is. An overdue deadline (after Fake.Set moved the reading past it) is a step of zero.
It is the step for a test that waits on timers: a fixed step, a minute say, also fires every timer the woken work arms inside that minute while Advance still runs, and whether the work armed it in time is up to the scheduler — the replacement subscription of controller/client's TestDefaultSubscription timed out inside one such step before the test could answer it. Stepping deadline by deadline, with the test's own wait for the effect in between, fires one instant at a time.
func (*Fake) AfterFunc ¶
AfterFunc arms f to run in its own goroutine once the fake has advanced by d. A d of zero or less is due at once and runs on the next Fake.Advance, as MockTime's timers do.
func (*Fake) Armed ¶
Armed is the time left until each armed timer and ticker fires, the earliest first — what a test reads to see which timeouts the code under test started (MockTime's timerCountFor, by duration instead of name).
func (*Fake) Monotonic ¶
Monotonic is the time the fake has advanced since New — the reading Fake.StepWall does not move (MockTime.nowUs).
func (*Fake) NewTicker ¶
NewTicker arms a ticker that sends the fake's time on C every d of advanced time. It panics on a d of zero or less, as time.NewTicker does.
func (*Fake) NewTimer ¶
NewTimer arms a timer that sends the fake's time on C once the fake has advanced by d.
func (*Fake) Next ¶
Next is the deadline of the earliest armed timer or ticker; false when nothing is armed.
func (*Fake) Now ¶
Now is the fake's current wall-clock time: the advanced reading plus the offset of every Fake.StepWall.
func (*Fake) Resolve ¶
Resolve runs wait in its own goroutine and moves the fake forward, deadline by deadline with a Fake.Yield before each step, until wait returns; its error is Resolve's. When nothing is armed it stops moving time and keeps waiting — the work may still arm a timer — until wait returns or ctx is done, in which case Resolve returns ctx.Err(). This is MockTime's resolve, which moves time until a promise settles.
func (*Fake) Set ¶
Set moves the reading to t, forwards or backwards, without firing anything — a jump of the whole clock, the monotonic reading included. Armed timers keep their deadlines; one that is overdue after the step fires on the next Fake.Advance, which may be Advance(0). For a step of the wall clock alone, see Fake.StepWall.
func (*Fake) StepWall ¶
StepWall moves the wall clock by d, forwards or backwards, as an NTP step or a manual clock set does: Fake.Now changes, Fake.Monotonic and the armed timers do not — MockTime.stepWallClock (packages/testing/src/mocks/time.ts:472-478 at b25ed960).