clocktest

package
v0.24.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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 New

func New(start time.Time) *Fake

New returns a Fake reading start. It arms nothing.

func (*Fake) Advance

func (f *Fake) Advance(d time.Duration)

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

func (f *Fake) AdvanceToNext() (time.Duration, bool)

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

func (f *Fake) AfterFunc(d time.Duration, fn func()) clock.Timer

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

func (f *Fake) Armed() []time.Duration

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

func (f *Fake) Monotonic() time.Duration

Monotonic is the time the fake has advanced since New — the reading Fake.StepWall does not move (MockTime.nowUs).

func (*Fake) NewTicker

func (f *Fake) NewTicker(d time.Duration) clock.Ticker

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

func (f *Fake) NewTimer(d time.Duration) clock.Timer

NewTimer arms a timer that sends the fake's time on C once the fake has advanced by d.

func (*Fake) Next

func (f *Fake) Next() (time.Time, bool)

Next is the deadline of the earliest armed timer or ticker; false when nothing is armed.

func (*Fake) Now

func (f *Fake) Now() time.Time

Now is the fake's current wall-clock time: the advanced reading plus the offset of every Fake.StepWall.

func (*Fake) Pending

func (f *Fake) Pending() int

Pending is the number of armed timers and running tickers.

func (*Fake) Resolve

func (f *Fake) Resolve(ctx context.Context, wait func() error) error

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

func (f *Fake) Set(t time.Time)

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

func (f *Fake) StepWall(d time.Duration)

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).

func (*Fake) Yield

func (f *Fake) Yield()

Yield lets the goroutines the fired callbacks and ticks started run before the test continues — MockTime's yield. It is a scheduler hint and a short real pause: enough for work that is runnable, no promise for work that waits on I/O.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL