clock

package
v0.111.6 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 2 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package clock provides a Clock interface with a real implementation (backed by time.Now) and a controllable mock implementation for deterministic tests.

Any package that calls time.Now() or time.Since() directly cannot be tested deterministically. By accepting a Clock the package becomes trivially testable: inject clock.Real() in production, clock.NewMock(t0) in tests, advance with mock.Advance(d).

Usage — production:

type Worker struct { clk clock.Clock }
func New() *Worker { return &Worker{clk: clock.Real()} }
func (w *Worker) Age(t time.Time) time.Duration { return w.clk.Since(t) }

Usage — test:

mc := clock.NewMock(time.Unix(0, 0))
w  := &Worker{clk: mc}
mc.Advance(5 * time.Minute)
// w.Age(...) now returns a deterministic value

Index

Examples

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 (or mocked) time.
	Now() time.Time
	// Since returns the time elapsed since t (equivalent to Now().Sub(t)).
	Since(t time.Time) time.Duration
	// After returns a channel that fires after duration d. On the real
	// clock this is time.After; on the mock it fires when the mock
	// time is advanced past the deadline.
	After(d time.Duration) <-chan time.Time
}

Clock is the interface satisfied by both Real and Mock. Any code that needs testable time handling should accept a Clock.

func Real

func Real() Clock

Real returns the singleton real-time Clock backed by time.Now. This is what production code should use.

type Mock

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

Mock is a controllable Clock for use in tests. All methods are safe for concurrent use. Advance moves the clock forward, firing any timers whose deadline has been reached.

func NewMock

func NewMock(start time.Time) *Mock

NewMock returns a Mock clock initialised to start. Use time.Unix(0,0) or a known UTC instant for fully deterministic tests.

Example

Code that takes a clock.Clock can be driven by a Mock in tests.

package main

import (
	"fmt"
	"time"

	"github.com/baditaflorin/go-common/clock"
)

func main() {
	m := clock.NewMock(time.Date(2026, 10, 17, 12, 0, 0, 0, time.UTC))
	start := m.Now()
	m.Advance(90 * time.Second)
	fmt.Println(m.Since(start))
}
Output:
1m30s

func (*Mock) Advance

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

Advance moves the mock clock forward by d and fires any timers whose deadline has been reached. d must be positive; negative values are a no-op.

func (*Mock) After

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

After returns a channel that will receive the mock time once m.Advance moves the clock past m.Now().Add(d).

func (*Mock) Now

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

Now returns the mock's current time.

func (*Mock) Set

func (m *Mock) Set(t time.Time)

Set moves the mock clock to an absolute time. Fires any timers whose deadline ≤ t.

func (*Mock) Since

func (m *Mock) Since(t time.Time) time.Duration

Since returns m.Now().Sub(t).

Jump to

Keyboard shortcuts

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