clock

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package clock is the only place permitted to read wall-clock time. Everything else takes a Clock so tests can drive time deterministically.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	Now() time.Time

	// AfterFunc runs f once, d in the future; a non-positive d is due now.
	// f runs on an unspecified goroutine, or synchronously inside Advance.
	AfterFunc(d time.Duration, f func()) Timer
}

Clock is the emulator's sole source of time.

func Real

func Real() Clock

Real returns a Clock backed by the time package.

type FakeClock

type FakeClock struct {
	// contains filtered or unexported fields
}

FakeClock is a Clock whose time only moves when a test moves it. It never sleeps and never polls.

func NewFake

func NewFake(now time.Time) *FakeClock

NewFake returns a FakeClock reading now.

func NewFakeNow

func NewFakeNow() *FakeClock

NewFakeNow returns a FakeClock seeded at the real wall-clock time. A manual-mode server uses it so timestamps start realistic, then hold still until a client advances the clock. Reading time.Now here is the point: this is the one hand-off from the wall clock into a clock the emulator controls.

func (*FakeClock) Advance

func (c *FakeClock) Advance(d time.Duration)

Advance moves time forward by d, running every callback that comes due in timestamp order and draining past d. Reentrant calls panic.

func (*FakeClock) AdvanceTo

func (c *FakeClock) AdvanceTo(target time.Time) (reachable bool)

AdvanceTo moves time forward to target, running every callback that comes due, and reports whether the target was reachable. It returns false only when target is strictly before now, since time does not move backward; target equal to now is a no-op that still reports true. The comparison and the move happen under one lock, so a caller cannot be told the target is in the future and then have a concurrent jump move past it — concurrent absolute travel converges on the latest target instead of summing deltas or falsely succeeding on a stale check.

func (*FakeClock) AfterFunc

func (c *FakeClock) AfterFunc(d time.Duration, f func()) Timer

func (*FakeClock) Now

func (c *FakeClock) Now() time.Time

func (*FakeClock) Pending

func (c *FakeClock) Pending() int

Pending reports how many timers are still scheduled, so a test can assert nothing was left dangling.

type Timer

type Timer interface {
	// Stop cancels the timer, reporting whether it beat the callback.
	Stop() bool
}

Timer is a scheduled callback that has not necessarily run yet.

Jump to

Keyboard shortcuts

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