clock

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package clock is the injectable time source for logic packages: they take a Clock instead of calling time.Now or time.NewTimer, and tests drive a Fake.

rules.Clock has the same Now() time.Time method, so a clock.Clock satisfies it; the two should converge on this package.

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
	// NewTimer returns a timer that fires once, d from now (at once when d <= 0).
	NewTimer(d time.Duration) Timer
	// NewTimerAt returns a timer that fires once the clock reaches deadline (at once if it
	// already has). Code that computed a deadline from an earlier Now uses it, so the time
	// that passed since isn't added to the wait.
	NewTimerAt(deadline time.Time) Timer
}

Clock reads the time and arms timers.

type Fake

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

Fake is a clock that only moves when told to. Its timers fire during Advance or Set, in deadline order. It is safe for concurrent use.

func NewFake

func NewFake(t time.Time) *Fake

NewFake returns a Fake set to t.

func (*Fake) Advance

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

Advance moves the clock forward by d, fires the timers that are due, and returns the new time.

func (*Fake) BlockUntil

func (f *Fake) BlockUntil(n int)

BlockUntil waits until at least n timers are waiting to fire, so a test can Advance only once the code under test is waiting on the clock.

func (*Fake) NewTimer

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

NewTimer returns a timer that fires once the clock reaches now+d (at once when d <= 0).

func (*Fake) NewTimerAt

func (f *Fake) NewTimerAt(deadline time.Time) Timer

NewTimerAt returns a timer that fires once the clock reaches deadline.

func (*Fake) Now

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

Now returns the fake time.

func (*Fake) Set

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

Set moves the clock to t and fires the timers that are due.

func (*Fake) Timers

func (f *Fake) Timers() int

Timers returns the number of timers waiting to fire.

type Real

type Real struct{}

Real is the wall clock.

func (Real) NewTimer

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

NewTimer wraps time.NewTimer.

func (Real) NewTimerAt

func (Real) NewTimerAt(deadline time.Time) Timer

NewTimerAt is NewTimer(time.Until(deadline)).

func (Real) Now

func (Real) Now() time.Time

Now returns time.Now().

type Timer

type Timer interface {
	// C receives the time once the timer fires.
	C() <-chan time.Time
	// Stop prevents the timer from firing; false when it already fired or was stopped.
	Stop() bool
}

Timer is a one-shot timer from a Clock.

Jump to

Keyboard shortcuts

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