clock

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 23, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package clock provides a Clock interface and implementations for production and deterministic test use.

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
	NewTicker(d time.Duration) Ticker
}

Clock provides time operations. Inject RealClock in production and FakeClock in tests that need deterministic timing.

func New

func New() Clock

New returns a Clock backed by the system clock.

func Wall

func Wall() Clock

Wall returns a Clock backed by the system clock. Alias for New.

type FakeClock

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

FakeClock is a Clock with controllable time for deterministic tests. Time advances only when Advance is called explicitly.

func NewFake

func NewFake(initial time.Time) *FakeClock

NewFake returns a FakeClock initialized to initial.

func NewMock

func NewMock(initial time.Time) *FakeClock

NewMock is an alias for NewFake for convenience.

func (*FakeClock) Advance

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

Advance moves the clock forward by d and fires any tickers whose interval has elapsed. Tickers with a full channel drop the excess tick (same behavior as the real time.Ticker under load).

func (*FakeClock) NewTicker

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

NewTicker returns a Ticker that fires when Advance moves time past multiples of d relative to the time NewTicker was called.

func (*FakeClock) Now

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

Now returns the current fake time.

type Mock

type Mock = FakeClock

Mock is an alias for FakeClock for convenience.

type RealClock

type RealClock struct{}

RealClock is a Clock backed by the system clock. Its zero value is ready to use.

func (RealClock) NewTicker

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

NewTicker returns a Ticker that fires at intervals of d using the system clock.

func (RealClock) Now

func (RealClock) Now() time.Time

Now returns the current system time.

type Ticker

type Ticker interface {
	// C returns the channel on which ticks are delivered.
	C() <-chan time.Time
	// Stop prevents the ticker from firing. It does not drain C.
	Stop()
}

Ticker is a time source that fires at regular intervals.

Jump to

Keyboard shortcuts

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