timeutil

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: AGPL-3.0 Imports: 3 Imported by: 1 module, 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 timeutil provides shared time abstractions for deterministic testing.

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
	After(d time.Duration) <-chan time.Time
}

Clock abstracts time operations so production code uses real time and tests can inject controllable fakes.

type FakeClock

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

FakeClock implements Clock for deterministic testing. Use Advance() to manually progress time and fire pending timers, or use NewInstantFakeClock() for tests that just need zero-delay After().

func NewFakeClock

func NewFakeClock() *FakeClock

NewFakeClock creates a FakeClock anchored to a fixed time. After() calls register timers that fire when Advance() is called.

func NewInstantFakeClock

func NewInstantFakeClock() *FakeClock

NewInstantFakeClock creates a FakeClock where After() returns immediately. Useful for tests that just need to eliminate delays without controlling timing.

func (*FakeClock) Advance

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

Advance progresses time by d, firing any timers that expire.

func (*FakeClock) After

func (c *FakeClock) After(d time.Duration) <-chan time.Time

After returns a channel that fires when Advance() progresses past the duration. In instant mode (NewInstantFakeClock), the channel fires immediately.

func (*FakeClock) Now

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

Now returns the fake clock's current time.

func (*FakeClock) PendingTimers

func (c *FakeClock) PendingTimers() int

PendingTimers returns the number of timers waiting to fire.

func (*FakeClock) WaitForTimers

func (c *FakeClock) WaitForTimers(ctx context.Context, n int) error

WaitForTimers blocks until at least n timers are pending, or ctx is cancelled.

type RealClock

type RealClock struct{}

RealClock delegates to the standard time package.

func (RealClock) After

func (RealClock) After(d time.Duration) <-chan time.Time

After returns a channel that receives once after d; nonpositive durations fire immediately.

func (RealClock) Now

func (RealClock) Now() time.Time

Now returns the current wall-clock time, including the standard library's monotonic reading.

Jump to

Keyboard shortcuts

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