Help us shape the future of Go! Take the 2026 Go Developer Survey

clock

package
v0.8.12 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package clock provides the Clock and IDGen interfaces, injected wherever time/ids are needed.

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
	// Sleep blocks for d, returning early with ctx.Err() if ctx is cancelled
	// or its deadline expires. d <= 0 returns immediately with nil (or
	// ctx.Err() if already cancelled).
	Sleep(ctx context.Context, d time.Duration) error
}

Clock supplies the current time and a deterministic sleep. Injected so tests can control both — engine logic NEVER reaches time.Now / time.Sleep directly (CLAUDE.md determinism invariant).

type CryptoIDGen

type CryptoIDGen struct{}

CryptoIDGen is the production IDGen (128-bit crypto/rand, hex-encoded).

func (CryptoIDGen) NewRunID

func (CryptoIDGen) NewRunID() string

type Fake

type Fake struct {
	T   time.Time
	IDs []string
	// contains filtered or unexported fields
}

Fake is a deterministic Clock+IDGen for tests. NewRunID returns IDs in order.

Fake is safe for concurrent use. The production System clock's Now/Sleep are goroutine-safe (time.Now / time.NewTimer), so the Clock contract permits concurrent callers — e.g. engine/agent_event_sink.go drives Sleep from per-delta-timer goroutines. mu guards the mutable T and i fields; construct Fake by named fields only (the zero-value mu is ready to use), and always use it via *Fake so the mutex is never copied.

func (*Fake) Advance

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

Advance steps the fake clock forward by d. Slice 2.4 (retry / timeout tests) uses this to simulate elapsed time without real-time sleeps. Independent of testing/synctest — Fake.Now() returns f.T regardless of any synctest bubble's faked time.Now(). Whether retry tests use this method or synctest's time-fake is slice 2.4's decision; 2.1 ships only the explicit-control primitive.

func (*Fake) NewRunID

func (f *Fake) NewRunID() string

func (*Fake) Now

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

func (*Fake) Sleep

func (f *Fake) Sleep(ctx context.Context, d time.Duration) error

Sleep is the Fake's non-blocking deterministic sleeper — advances T by d (if the context is live) and returns immediately. Tests that drive retry orchestration use this; the post-loop f.Now() reads as if d had elapsed. A pre-cancelled or expired ctx returns ctx.Err() WITHOUT advancing T.

type IDGen

type IDGen interface {
	NewRunID() string
}

IDGen mints the run id — the only nondeterministic id in the runtime.

type System

type System struct{}

System is the production Clock (UTC wall-clock + real timer-based Sleep).

func (System) Now

func (System) Now() time.Time

func (System) Sleep

func (System) Sleep(ctx context.Context, d time.Duration) error

Sleep blocks for d via time.NewTimer + ctx.Done() select. Under testing/synctest, time.NewTimer is faked, so this method is the integration point for synctest-based timing tests.

Jump to

Keyboard shortcuts

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