clock

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package clock provides a small time abstraction so that time-stamped state (a sprite's creation time, and any future timed behavior) can be driven deterministically in tests. Production code uses Real(); tests use a Fake whose Advance method fires scheduled callbacks without any real sleeping.

Index

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 time as seen by this clock.
	Now() time.Time
	// AfterFunc schedules fn to run after d has elapsed and returns a Timer
	// that can cancel it.
	AfterFunc(d time.Duration, fn func()) Timer
}

Clock is the subset of the time package that spritzer depends on.

func Real

func Real() Clock

Real returns a Clock backed by the standard library time package.

type Fake

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

Fake is a deterministic Clock. Time only moves when Advance is called, at which point any callbacks whose deadline has passed run synchronously on the calling goroutine. Callbacks may schedule further callbacks, which are also fired if they fall within the advanced window.

func NewFake

func NewFake(start time.Time) *Fake

NewFake returns a Fake clock started at the given time. If start is the zero value a fixed, readable epoch is used.

func (*Fake) Advance

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

Advance moves the clock forward by d, firing every due callback in deadline order. Callbacks run without the internal lock held so they may schedule more work on the same clock.

func (*Fake) AfterFunc

func (f *Fake) AfterFunc(d time.Duration, fn func()) Timer

AfterFunc registers fn to fire once the fake clock has advanced past d.

func (*Fake) Now

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

Now returns the fake clock's current time.

type Timer

type Timer interface {
	// Stop cancels the timer, reporting whether it did so before it fired.
	Stop() bool
}

Timer can cancel a scheduled callback.

Jump to

Keyboard shortcuts

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