clock

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package clock is the time source every scheduling decision reads. Logic in this module never calls time.Now or time.NewTimer: it takes a Clock, so production passes Real and tests pass a Fake that moves only when told to, which makes "sleep three days" run in microseconds and makes every run repeatable.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	// Now returns the current time.
	Now() time.Time
	// NewTimer returns a timer that delivers on C once d has passed. A d
	// of zero or less fires at once.
	NewTimer(d time.Duration) Timer
}

Clock is a source of the current time and of timers.

type Fake

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

Fake is a Clock that moves only on Advance or Set. Timers due at or before the new time fire in deadline order, each receiving its deadline. It is safe for concurrent use. Use NewFake; the zero value is not ready.

func NewFake

func NewFake(start time.Time) *Fake

NewFake returns a Fake set to start.

func (*Fake) Advance

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

Advance moves the clock forward by d and fires every timer now due. A negative d is ignored: the fake clock never goes back.

func (*Fake) BlockUntil

func (f *Fake) BlockUntil(ctx context.Context, n int) error

BlockUntil waits until at least n timers are pending, so a test can Advance only after the goroutine under test has armed its timer. It returns ctx.Err() if ctx ends first.

func (*Fake) NewTimer

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

NewTimer returns a timer due at Now()+d. A d of zero or less fires at once, as time.NewTimer does.

func (*Fake) Now

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

Now returns the fake time.

func (*Fake) Pending

func (f *Fake) Pending() int

Pending returns the number of timers that have not fired or been stopped.

func (*Fake) Set

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

Set moves the clock to t, if t is later, and fires every timer now due.

type Real

type Real struct{}

Real is the wall clock. It is the one place in this module that reads time.Now.

func (Real) NewTimer

func (Real) NewTimer(d time.Duration) Timer

NewTimer wraps time.NewTimer.

func (Real) Now

func (Real) Now() time.Time

Now returns time.Now.

type Timer

type Timer interface {
	// C delivers the fire time once. Its buffer holds one value, so a
	// timer never blocks the clock that fires it.
	C() <-chan time.Time
	// Stop prevents the timer from firing. It reports whether it did so,
	// false when the timer had already fired or been stopped.
	Stop() bool
}

Timer is a one-shot timer.

Jump to

Keyboard shortcuts

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