fakeclock

package
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: BSD-3-Clause Imports: 3 Imported by: 0 modules, 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 fakeclock is a clock that a test moves by hand, so that timers and frame times in tests need no 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.
	Now() time.Time
	// AfterFunc runs f in its own goroutine (the real clock) or inside Advance
	// (the fake one) after d. The returned Timer cancels it.
	AfterFunc(d time.Duration, f func()) Timer
}

Clock tells the time and runs functions later. The real clock and the fake one both implement it, and code that needs time takes a Clock.

Flutter: no counterpart (flutter_test's FakeAsync)

type Fake

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

Fake is a Clock that only moves when Advance or Set is called. It is safe for concurrent use; the functions run on the goroutine that advances it.

Flutter: no counterpart (flutter_test's FakeAsync)

func New

func New(start time.Time) *Fake

New returns a fake clock that reads start. A zero start is the Unix epoch, so that times print the same on every run.

func (*Fake) Advance

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

Advance moves the clock forward by d and runs the timers that come due, in order of their time (and of their creation for equal times). While a function runs the clock reads the time of its timer, so a function that reads the clock or sets another timer sees a consistent time; a timer it sets that falls within d runs in the same call. A negative d panics.

func (*Fake) AfterFunc

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

AfterFunc schedules f to run when the clock has advanced by d. A d of zero or less runs f at the next Advance (of any amount, including zero).

func (*Fake) Now

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

Now returns the time of the clock.

func (*Fake) Pending

func (c *Fake) Pending() int

Pending returns how many timers are waiting.

func (*Fake) Set

func (c *Fake) Set(t time.Time)

Set moves the clock to t, running the timers that come due on the way. A t before the current time panics.

func (*Fake) Since

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

Since returns the time elapsed from t, by this clock.

type Real

type Real struct{}

Real is the clock of the machine.

Flutter: no counterpart

func (Real) AfterFunc

func (Real) AfterFunc(d time.Duration, f func()) Timer

AfterFunc is time.AfterFunc.

func (Real) Now

func (Real) Now() time.Time

Now returns time.Now().

type Timer

type Timer interface {
	// Stop prevents the function from running. It reports whether it did: false
	// when the function already ran or was already stopped.
	Stop() bool
}

Timer is a function waiting to run.

Flutter: no counterpart

Jump to

Keyboard shortcuts

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