Documentation ¶
Overview ¶
Package time2 provides time related functionality that can be manipulated to facilite deterministic testing.
Index ¶
- func Now(ctx context.Context) time.Time
- func Since(ctx context.Context, t time.Time) time.Duration
- func Sleep(ctx context.Context, duration time.Duration) bool
- func WithClock(ctx context.Context, clk Clock) context.Context
- type Clock
- type Machine
- func (tm *Machine) Advance(d time.Duration)
- func (tm *Machine) Block(ctx context.Context, minimumTimerCount int) bool
- func (tm *Machine) BlockThenAdvance(ctx context.Context, minimumTimerCount int, d time.Duration) bool
- func (tm *Machine) Clock() Clock
- func (tm *Machine) Now() time.Time
- func (tm *Machine) Since(t time.Time) time.Duration
- type MachineOption
- type Ticker
- type Timer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Now ¶
Now invokes the method of the same name on the clock set on the context or the real clock when unset.
func Since ¶
Since invokes the method of the same name on the clock set on the context or the real clock when unset.
Types ¶
type Clock ¶
type Clock struct {
// contains filtered or unexported fields
}
Clock implements a clock. The zero value is valid and uses the real time functions.
func (Clock) NewTicker ¶
NewTicker provides functionality equivalent to time.NewTicker. It is safe to call on a zero value.
func (Clock) NewTimer ¶
NewTimer provides functionality equivalent to time.NewTimer. It is safe to call on a zero value.
func (Clock) Now ¶
Now provides functionality equivalent to time.Now. It is safe to call on a zero value.
type Machine ¶
type Machine struct {
// contains filtered or unexported fields
}
Machine 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 build when influencing a clock from multiple goroutines, the Machine disallows concurrent access by-design.
func NewMachine ¶
func NewMachine(opts ...MachineOption) *Machine
NewMachine returns a new time machine configured with the given options.
func WithNewMachine ¶
WithNewMachine provides a context that uses a clock controlled by the returned time machine.
func (*Machine) Advance ¶
Advance advances the clock forward, triggering all expired timers/tickers tracked by the time machine. It should not be called concurrently.
func (*Machine) Block ¶
Block blocks execution until the scheduled timer count reaches the provided minimum. Timers are scheduled until triggered or stopped. Tickers are always scheduled until stopped. It should not be called concurrently. Returns false if the context became done while blocking.
func (*Machine) BlockThenAdvance ¶
func (tm *Machine) BlockThenAdvance(ctx context.Context, minimumTimerCount int, d time.Duration) bool
BlockThenAdvance is a convenience method that blocks on the minimum timer count and then advances the clock, triggering any expired timers/tickers. It should not be called concurrently. Returns false if the context became done while blocking.
type MachineOption ¶
type MachineOption = func(*machineBackend)
MachineOption allows optional configuration of the Machine.
func WithTimeAt ¶
func WithTimeAt(t time.Time) MachineOption
WithTimeAt uses the provided time as the current time of the time machine.