manual

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package manual provides deterministic fixed and manually advanced clocks.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrActiveLimit reports exhaustion of the configured active-object budget.
	ErrActiveLimit = errors.New("manual clock: active object limit exceeded")
	// ErrWorkLimit reports exhaustion of one advancement's work budget.
	ErrWorkLimit = errors.New("manual clock: advancement work limit exceeded")
	// ErrClosed reports an operation attempted after shutdown.
	ErrClosed = errors.New("manual clock: closed")
	// ErrBackwardAdvance reports an AdvanceTo target before the current wall time.
	ErrBackwardAdvance = errors.New("manual clock: backward advance")
	// ErrInvalidLimits reports a zero or negative resource budget.
	ErrInvalidLimits = errors.New("manual clock: invalid limits")
)

Functions

This section is empty.

Types

type Callback

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

Callback is a manually advanced timer callback.

func (*Callback) Reset

func (callback *Callback) Reset(duration time.Duration) (bool, error)

Reset reschedules the callback and reports whether it was active beforehand.

func (*Callback) Stop

func (callback *Callback) Stop() bool

Stop prevents an active callback from starting.

type Clock

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

Clock is a concurrency-safe deterministic clock. Time changes only through Advance, AdvanceTo, or Jump. It starts no hidden goroutine.

func New

func New(start time.Time, options ...Option) (*Clock, error)

New constructs a clock at an explicit wall timestamp, including time.Time{}. It preserves the location and strips any process-local monotonic reading so Jump can model wall movement independently from elapsed progress.

func (*Clock) Advance

func (clock *Clock) Advance(duration time.Duration) (*Waiter, error)

Advance moves monotonic time forward and synchronously processes every event due through the target. Callbacks run outside internal locks in deterministic timestamp and registration order.

func (*Clock) AdvanceTo

func (clock *Clock) AdvanceTo(target time.Time) (*Waiter, error)

AdvanceTo moves forward until target wall time. Backward wall movement must use Jump so it cannot accidentally reverse monotonic progress.

func (*Clock) AfterFunc

func (clock *Clock) AfterFunc(duration time.Duration, function func()) (clockpkg.Callback, error)

AfterFunc schedules an owned callback. A panic is recovered and counted in the advancement Result; panic payloads are never retained.

func (*Clock) Jump

func (clock *Clock) Jump(delta time.Duration) error

Jump changes wall time without changing monotonic progress or event deadlines.

func (*Clock) Mark

func (clock *Clock) Mark() time.Duration

Mark captures the current monotonic progress as a time.Duration token.

func (*Clock) Measure

func (clock *Clock) Measure() func() time.Duration

Measure captures current monotonic progress and returns a concurrency-safe closure whose result is unaffected by Jump.

func (*Clock) NewTicker

func (clock *Clock) NewTicker(duration time.Duration) (clockpkg.Ticker, error)

NewTicker creates an owned ticker. Its one-element channel drops ticks while the receiver is backpressured, matching the standard library policy.

func (*Clock) NewTimer

func (clock *Clock) NewTimer(duration time.Duration) (clockpkg.Timer, error)

NewTimer creates an owned one-shot timer.

func (*Clock) Now

func (clock *Clock) Now() time.Time

Now returns the current wall time. The starting location is preserved.

func (*Clock) Shutdown

func (clock *Clock) Shutdown() error

Shutdown idempotently releases every active object owned by the clock.

func (*Clock) Since

func (clock *Clock) Since(start time.Time) time.Duration

Since returns wall-clock subtraction against Now. Use Mark and SinceMark for elapsed measurement that must remain correct across Jump.

func (*Clock) SinceMark

func (clock *Clock) SinceMark(mark time.Duration) time.Duration

SinceMark returns monotonic progress since mark, independent of wall jumps.

func (*Clock) Sleep

func (clock *Clock) Sleep(ctx context.Context, duration time.Duration) error

Sleep blocks until manual advancement reaches the deadline or ctx is done.

func (*Clock) Snapshot

func (clock *Clock) Snapshot() Snapshot

Snapshot returns bounded diagnostic counters without callback payloads.

type Fixed

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

Fixed is an immutable wall clock.

func NewFixed

func NewFixed(now time.Time) Fixed

NewFixed returns an immutable clock fixed at now.

func (Fixed) Measure

func (Fixed) Measure() func() time.Duration

Measure returns an elapsed measurement that remains zero for a fixed clock.

func (Fixed) Now

func (fixed Fixed) Now() time.Time

Now returns the fixed timestamp without changing its location.

func (Fixed) Since

func (fixed Fixed) Since(start time.Time) time.Duration

Since returns the duration from start to the fixed timestamp. If both values contain compatible monotonic readings, time.Time.Sub uses them.

type Limits

type Limits struct {
	MaxActive         int
	MaxWorkPerAdvance int
}

Limits bounds scheduled objects, outstanding advancement waiters, and work processed by a Clock. MaxActive applies independently to scheduled objects and advancement waiters.

type Option

type Option func(*config) error

Option configures a Clock during construction.

func WithLimits

func WithLimits(limits Limits) Option

WithLimits replaces the default active-object and advancement budgets.

type Result

type Result struct {
	StartedAt time.Duration
	EndedAt   time.Duration
	Triggered int
	Callbacks int
	Panics    int
}

Result summarizes bounded work performed by one advancement.

type Snapshot

type Snapshot struct {
	Now     time.Time
	Elapsed time.Duration
	Active  int
	Closed  bool
}

Snapshot is a bounded view of manual-clock state.

type Ticker

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

Ticker is a manually advanced periodic timer.

func (*Ticker) C

func (ticker *Ticker) C() <-chan time.Time

C returns the ticker's receive-only event channel.

func (*Ticker) Reset

func (ticker *Ticker) Reset(duration time.Duration) error

Reset changes the period and schedules the next tick from the current time.

func (*Ticker) Stop

func (ticker *Ticker) Stop()

Stop prevents future ticks. It is idempotent.

type Timer

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

Timer is a manually advanced one-shot timer.

func (*Timer) C

func (timer *Timer) C() <-chan time.Time

C returns the timer's receive-only event channel.

func (*Timer) Reset

func (timer *Timer) Reset(duration time.Duration) (bool, error)

Reset reschedules the timer and reports whether it was active beforehand.

func (*Timer) Stop

func (timer *Timer) Stop() bool

Stop prevents an active timer from firing.

type Waiter

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

Waiter synchronizes completion of work triggered by an advancement.

func (*Waiter) Wait

func (waiter *Waiter) Wait(ctx context.Context) (Result, error)

Wait returns the completed result or context cancellation.

Jump to

Keyboard shortcuts

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