faketime

package
v0.0.0-...-5c79d48 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2024 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	// NewTicker returns a new Ticker containing a channel that will send
	// the current time on the channel after each tick. The typical period of the
	// ticks is specified by the duration argument, but in the case of a fake
	// ticker ticks may be manually sent. Include a tag so that tests can
	// intercept tickers and handle appropriately.
	NewTicker(d time.Duration, tag any) Ticker
}

Clock is a simplified clock interface which facilitates testing of delays and timeouts by allowing all waits to be tagged so that test code can react to specific waits and respond accordingly.

func NewRealClock

func NewRealClock() Clock

NewRealClock returns a clock which just calls out to real time functions.

type FakeClock

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

FakeClock is a type which exposes "faked" time functions which can be useful for determinstically (and quickly) testing time-dependent code.

func NewFakeClock

func NewFakeClock(tickerListener chan *FakeTicker) *FakeClock

NewFakeClock return a new fake clock, and must be passed a tickerListener Channel where all new tickers will be sent.

func (*FakeClock) NewTicker

func (c *FakeClock) NewTicker(d time.Duration, tag any) Ticker

NewTicker returns a new FakeTicker. Every "tick" of the ticker must be manually sent by calling the "Tick" method.

type FakeTicker

type FakeTicker struct {
	// Tag can be used to identify tickers for tests.
	Tag any
	// contains filtered or unexported fields
}

FakeTicker is a Ticker implementation which will only send ticks when Tick is manually called.

func (*FakeTicker) C

func (t *FakeTicker) C() <-chan time.Time

C returns the ticker channel.

func (*FakeTicker) Stop

func (t *FakeTicker) Stop()

Stop prevents future Tick calls from succeeding, concurrent Tick calls may or may not be successful.

func (*FakeTicker) Tick

func (t *FakeTicker) Tick(ctx context.Context) error

Tick blocks until: - the ticker is stopped, - ctx expires, or - successfully sending on the tick channel.

type Ticker

type Ticker interface {
	// C returns the ticker channel.
	C() <-chan time.Time

	// Stop releases any resources associated with the ticker and makes sure no
	// future ticks are fired.
	Stop()
}

Ticker is an interface to wrap time.Ticker.

Jump to

Keyboard shortcuts

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