testclock

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

LICENSE: clock

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

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

Clock provides facilities to manipulate time, timers, and tickers. It is designed for use in deterministic testing of time reliant code. Since deterministism is hard to achieve when influencing a clock from multiple goroutines, the FakeClock disallows concurrent access by-design.

func New

func New(opts ...Option) *Clock

func (*Clock) Advance

func (st *Clock) Advance(d time.Duration) int

Advance moves the clock forward and returns the number of pending timers that were triggered.

func (*Clock) AdvanceTo

func (st *Clock) AdvanceTo(t time.Time) int

AdvanceTo moves the clock forward to the specific time and returns the number of pending timers that were triggered.

func (*Clock) Block

func (st *Clock) Block(t *testing.T, pendingTimerCount int)

Block waits until there are at least the given count of tickers/timers pending.

func (*Clock) BlockThenAdvance

func (st *Clock) BlockThenAdvance(t *testing.T, pendingTimerCount int, d time.Duration) int

func (*Clock) BlockThenStep

func (st *Clock) BlockThenStep(t *testing.T, pendingTimerCount int) (time.Duration, bool)

func (*Clock) Clock

func (st *Clock) Clock() clock.Clock

Clock returns a clock controlled by the test clock.

func (*Clock) Next

func (st *Clock) Next() time.Duration

Next returns how much time needs to elapse to fire the next pending timer. It returns a negative duration if there are no pending timers.

func (*Clock) Now

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

Now provides functionality equivalent to time.Now according to the the test clock.

func (*Clock) Since

func (st *Clock) Since(t time.Time) time.Duration

Since provides functionality equivalent to time.Since according to the the test clock.

func (*Clock) Step

func (st *Clock) Step() (time.Duration, bool)

Step steps the clock far enough to trigger the the next pending timer/ticker. It returns (elapsed,true) if successful or (0, false) if there were no timers/tickers pending. Block can be called first to ensure there is one or more pending timers/tickers.

type Option

type Option func(*testBackend)

func WithLocation

func WithLocation(loc *time.Location) Option

func WithNow

func WithNow(now time.Time) Option

Jump to

Keyboard shortcuts

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