Documentation ¶
Overview ¶
Package clock is a context-driven wrapper for the time library. It allows substitution of mock clocks via context.Context for testing and other purposes.
Index ¶
- func After(ctx context.Context, d time.Duration) <-chan time.Time
- func Now(ctx context.Context) time.Time
- func Onto(ctx context.Context, clock Clock) context.Context
- func Since(ctx context.Context, t time.Time) time.Duration
- func Sleep(ctx context.Context, d time.Duration)
- func Until(ctx context.Context, t time.Time) time.Duration
- type Clock
- type Mock
- type Ticker
- type TimeTravel
- type Timer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Clock ¶
type Clock interface { After(d time.Duration) <-chan time.Time AfterFunc(d time.Duration, f func()) Timer NewTicker(d time.Duration) Ticker NewTimer(d time.Duration) Timer Now() time.Time }
Clock models the system clock. The functions mirror their counterparts in the time package.
type Mock ¶
type Mock struct {
// contains filtered or unexported fields
}
Mock implements a mock Clock that reports a fixed sequence of times each time it is queried.
type TimeTravel ¶
type TimeTravel struct {
// contains filtered or unexported fields
}
TimeTravel is a mock Clock that offsets time by a given offset. Also, when a call is made that involves a time delay, it travels to that time and returns instantly.
func NewTimeTravel ¶
func NewTimeTravel(initialOffset time.Duration) *TimeTravel
NewTimeTravel creates a TimeTravel Clock.
func NewTimeTravelStartingAt ¶ added in v0.0.27
func NewTimeTravelStartingAt(when time.Time) *TimeTravel
func (*TimeTravel) After ¶
func (t *TimeTravel) After(d time.Duration) <-chan time.Time
Wait returns a channel that time-travels by the duration and immediately sends the new time on the returned channel immediately. To conform to the real-world time API and avoid paradoxes, this function won't time-travel backwards. Negative durations will be treated as 0.
func (*TimeTravel) Close ¶ added in v0.0.27
func (t *TimeTravel) Close()
func (*TimeTravel) Now ¶
func (t *TimeTravel) Now() time.Time
Now returns the current time adjusted by the time-travel offset.