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

clock

package
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package clock is the injected time source every timeout in Agent GM is measured against (spec section 13.1). No test sleeps, and no test waits out a real 24 hours.

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
	// Since is Now().Sub(t).
	Since(t time.Time) time.Duration
	// Sleep blocks for d as this clock measures it, or until ctx ends, in
	// which case it returns ctx.Err(). Every wait in Agent GM goes through
	// it -- the [3s, 8s, 20s] send backoff, ticket expiry, the pending
	// timeout -- so no test sleeps and no test waits out a real 24 hours
	// (spec section 13.1).
	Sleep(ctx context.Context, d time.Duration) error
}

Clock is the time source.

type Fake

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

Fake advances only when a test tells it to. Two backends may share one Fake or hold two, and each test says which -- spec section 13.1's sign-out and re-pair tests advance one account's clock while the other's stands still, which is only expressible with separate clocks.

func NewFake

func NewFake() *Fake

NewFake starts a fake clock at a fixed, deterministic instant.

func NewFakeAt

func NewFakeAt(t time.Time) *Fake

NewFakeAt starts a fake clock at a chosen instant.

func (*Fake) Advance

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

Advance moves the clock forward.

func (*Fake) FreezeSleep

func (f *Fake) FreezeSleep(v bool)

FreezeSleep makes Sleep record without advancing.

func (*Fake) Now

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

func (*Fake) Since

func (f *Fake) Since(t time.Time) time.Duration

func (*Fake) Sleep

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

Sleep advances this clock by d and records the wait, rather than blocking.

A fake clock that blocked until another goroutine advanced it would make every single-goroutine test a deadlock waiting to happen, and would make the thing under test -- "it backed off for 3s and then 8s" -- unobservable. Advancing and recording makes the schedule itself the assertion: see Sleeps.

A frozen clock (FreezeSleep) does not advance, for the tests that need to hold time still across a wait.

func (*Fake) Sleeps

func (f *Fake) Sleeps() []time.Duration

Sleeps reports every duration Sleep was asked for, in order. It is how a test asserts a backoff schedule without waiting for it.

type Real

type Real struct{}

Real is the production clock.

func (Real) Now

func (Real) Now() time.Time

func (Real) Since

func (Real) Since(t time.Time) time.Duration

func (Real) Sleep

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

Jump to

Keyboard shortcuts

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