clock

package
v0.38.0 Latest Latest
Warning

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

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

Documentation

Overview

Package clock provides a minimal Clock interface for time-dependent code. Production code uses Real (which delegates to the standard library), while tests use Mock to advance time programmatically without sleeping.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	// NewTicker returns a ticker that fires at the given interval.
	NewTicker(d time.Duration) Ticker

	// After returns a channel that receives after duration d.
	After(d time.Duration) <-chan time.Time
}

Clock abstracts time operations so that tests can control time progression without sleeping. Only operations actually used in the codebase are included.

type Mock

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

Mock is a deterministic Clock for tests. Time only advances when Add is called, making tests that depend on tickers or timers run instantly.

func NewMock

func NewMock() *Mock

NewMock returns a Mock clock initialized to the Unix epoch.

func (*Mock) Add

func (m *Mock) Add(d time.Duration)

Add advances the mock clock by d, firing any tickers or After channels whose deadlines have been reached.

func (*Mock) After

func (m *Mock) After(d time.Duration) <-chan time.Time

After returns a channel that receives when the clock advances past d.

func (*Mock) NewTicker

func (m *Mock) NewTicker(d time.Duration) Ticker

NewTicker creates a mock ticker that fires when the clock is advanced.

func (*Mock) Now

func (m *Mock) Now() time.Time

Now returns the mock's current time.

type Real

type Real struct{}

Real is a Clock backed by the standard library's time package.

func (Real) After

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

After returns a channel that receives after duration d.

WARNING: The returned timer cannot be stopped by the caller. If the channel is never consumed (e.g., due to context cancellation), the timer will not be garbage collected until it fires. Only use this in select statements where the channel is guaranteed to be drained or the goroutine exits promptly.

func (Real) NewTicker

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

NewTicker creates a real time.Ticker.

type Ticker

type Ticker interface {
	// C returns the ticker's channel.
	C() <-chan time.Time

	// Stop stops the ticker.
	Stop()

	// Reset changes the ticker's interval.
	Reset(d time.Duration)
}

Ticker abstracts *time.Ticker so both real and mock implementations can be used interchangeably.

Jump to

Keyboard shortcuts

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