clock

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package clock provides the injectable time source used by app and adapter code: Real for production and Fake for tests and simulations driven by a manually advanced clock.

fc := clock.NewFake(time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC))
ch := fc.After(time.Second)
fc.Advance(time.Second) // ch now has a value

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
	// After returns a channel that receives the time once d has elapsed.
	After(d time.Duration) <-chan time.Time
	// NewTicker returns a ticker that fires every d.
	NewTicker(d time.Duration) Ticker
}

Clock is the source of time for every client-side time value and every time comparison Foreman sends to Postgres.

type Fake

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

Fake is a manually advanced clock for tests and simulations. Timers and tickers created from it fire only when Advance or Set moves time past their deadline. It is safe for concurrent use.

func NewFake

func NewFake(start time.Time) *Fake

NewFake returns a fake clock set to start.

func (*Fake) Advance

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

Advance moves the fake time forward by d and fires due timers and tickers.

func (*Fake) After

func (f *Fake) After(d time.Duration) <-chan time.Time

After returns a channel that fires once the fake time reaches now+d.

func (*Fake) NewTicker

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

NewTicker returns a ticker driven by the fake clock.

func (*Fake) Now

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

Now returns the fake current time.

func (*Fake) Set

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

Set moves the fake time to t (never backwards) and fires due timers and tickers.

func (*Fake) Waiters

func (f *Fake) Waiters() int

Waiters returns the number of pending After timers plus active tickers. Tests use it to wait until goroutines are blocked on the clock.

type Real

type Real struct{}

Real is the wall clock.

func New

func New() Real

New returns the wall clock.

func (Real) After

func (Real) After(d time.Duration) <-chan time.Time

After wraps time.After.

func (Real) NewTicker

func (Real) NewTicker(d time.Duration) Ticker

NewTicker wraps time.NewTicker.

func (Real) Now

func (Real) Now() time.Time

Now returns time.Now().

type Ticker

type Ticker interface {
	// C returns the channel on which ticks are delivered.
	C() <-chan time.Time
	// Stop turns the ticker off.
	Stop()
	// Reset changes the ticker period and restarts it.
	Reset(d time.Duration)
}

Ticker delivers ticks at a fixed interval until stopped.

Jump to

Keyboard shortcuts

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