timeutil

package
v0.4.1 Latest Latest
Warning

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

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

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

func (RealClock) Now

func (RealClock) Now() time.Time

Jump to

Keyboard shortcuts

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