clock

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func After

func After(ctx context.Context, d time.Duration) <-chan time.Time

func Now

func Now(ctx context.Context) time.Time

func Onto

func Onto(ctx context.Context, clock Clock) context.Context

Onto sets the context clock. Pass nil to revert to the default system clock.

func Since

func Since(ctx context.Context, t time.Time) time.Duration

func Sleep

func Sleep(ctx context.Context, d time.Duration)

func Until

func Until(ctx context.Context, t time.Time) time.Duration

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.

func From

func From(ctx context.Context) Clock

From gets the Clock from the Context.

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.

func NewMock

func NewMock() *Mock

NewMock creates a Mock Clock.

func (*Mock) AddTicks

func (m *Mock) AddTicks(t ...time.Time)

func (*Mock) After

func (*Mock) After(d time.Duration) <-chan time.Time

func (*Mock) AfterFunc

func (*Mock) AfterFunc(d time.Duration, f func()) Timer

func (*Mock) NewTicker

func (*Mock) NewTicker(d time.Duration) Ticker

func (*Mock) NewTimer

func (*Mock) NewTimer(d time.Duration) Timer

func (*Mock) Now

func (m *Mock) Now() time.Time

type Ticker

type Ticker interface {
	C() <-chan time.Time
	Stop()
}

Ticker models the semantics of time.Ticker.

func NewTicker

func NewTicker(ctx context.Context, d time.Duration) Ticker

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) AfterFunc

func (*TimeTravel) AfterFunc(d time.Duration, f func()) Timer

func (*TimeTravel) Close added in v0.0.27

func (t *TimeTravel) Close()

func (*TimeTravel) NewTicker

func (*TimeTravel) NewTicker(d time.Duration) Ticker

func (*TimeTravel) NewTimer

func (*TimeTravel) NewTimer(d time.Duration) Timer

func (*TimeTravel) Now

func (t *TimeTravel) Now() time.Time

Now returns the current time adjusted by the time-travel offset.

type Timer

type Timer interface {
	C() <-chan time.Time
	Reset(d time.Duration) bool
	Stop() bool
}

Ticker models the semantics of time.Timer.

func AfterFunc

func AfterFunc(ctx context.Context, d time.Duration, f func()) Timer

func NewTimer

func NewTimer(ctx context.Context, d time.Duration) Timer

Jump to

Keyboard shortcuts

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