Documentation
¶
Index ¶
- func ContextWithDeadline(ctx context.Context, clock Clock, deadline time.Time) (context.Context, context.CancelFunc)
- func ContextWithTimeout(ctx context.Context, clock Clock, timeout time.Duration) (context.Context, context.CancelFunc)
- func WithContext(ctx context.Context, clock Clock) context.Context
- type Advanceable
- type Clock
- type MockClock
- func (a MockClock) Advance(duration time.Duration)
- func (c *MockClock) After(duration time.Duration) <-chan time.Time
- func (c *MockClock) BlockedOnAfter() int
- func (c *MockClock) BlockingAdvance(duration time.Duration)
- func (c *MockClock) GetAfterArgs() []time.Duration
- func (c *MockClock) GetTickerArgs() []time.Duration
- func (c *MockClock) NewTicker(duration time.Duration) Ticker
- func (c *MockClock) Now() time.Time
- func (a MockClock) SetCurrent(now time.Time)
- func (c *MockClock) Since(t time.Time) time.Duration
- func (c *MockClock) Sleep(duration time.Duration)
- func (c *MockClock) Until(t time.Time) time.Duration
- type MockTicker
- type Ticker
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextWithDeadline ¶
func ContextWithDeadline(ctx context.Context, clock Clock, deadline time.Time) (context.Context, context.CancelFunc)
ContextWithDeadline mimmics context.WithDeadline, but uses the given clock instance instead of the using standard time.After function directly.
Types ¶
type Advanceable ¶
type Clock ¶
type Clock interface {
// Now returns the current time.
Now() time.Time
// After returns a channel which receives the current time after
// the given duration elapses.
After(duration time.Duration) <-chan time.Time
// Sleep blocks until the given duration elapses.
Sleep(duration time.Duration)
// Since returns the time elapsed since t.
Since(t time.Time) time.Duration
// Until returns the duration until t.
Until(t time.Time) time.Duration
// NewTicker will construct a ticker which will continually fire,
// pausing for the given duration in between invocations.
NewTicker(duration time.Duration) Ticker
}
Clock is a wrapper around common functions in the time package. This interface is designed to allow easy mocking of time functions.
func FromContext ¶
FromContext retrieves the Clock value from the provided context. If a Clock is not set on the context a new real clock will be returned.
func NewRealClock ¶
func NewRealClock() Clock
NewRealClock returns a Clock whose implementation falls back to the methods available in the time package.
type MockClock ¶
type MockClock struct {
// contains filtered or unexported fields
}
MockClock is an implementation of Clock that can be moved forward in time in increments for testing code that relies on timeouts or other time-sensitive constructs.
func NewMockClock ¶
func NewMockClock() *MockClock
NewMockClock creates a new MockClock with the internal time set to time.Now().
func NewMockClockAt ¶
NewMockClockAt creates a new MockClick with the internal time set to the given time.
func (*MockClock) After ¶
After returns a channel that will be sent the clock's internal time once the clock's internal time is at or past the supplied duration.
func (*MockClock) BlockedOnAfter ¶
BlockedOnAfter returns the number of calls to After that are blocked waiting for a call to Advance to trigger them.
func (*MockClock) BlockingAdvance ¶
BlockingAdvance will call Advance but only after there is another goroutine with a reference to a new channel returned by the After method.
func (*MockClock) GetAfterArgs ¶
GetAfterArgs returns the duration of each call to After in the same order as they were called. The list is cleared each time GetAfterArgs is called.
func (*MockClock) GetTickerArgs ¶
GetTickerArgs returns the duration of each call to create a new ticker in the same order as they were called. The list is cleared each time GetTickerArgs is called.
func (*MockClock) NewTicker ¶
NewTicker creates a new Ticker tied to the internal MockClock time that ticks at intervals similar to time.NewTicker(). It will also skip or drop ticks for slow readers similar to time.NewTicker() as well.
func (MockClock) SetCurrent ¶
SetCurrent sets the clock's internal time to the given time.
type MockTicker ¶
type MockTicker struct {
// contains filtered or unexported fields
}
MockTicker is an implementation of Ticker that can be moved forward in time in increments for testing code that relies on timeouts or other time-sensitive constructs.
func NewMockTicker ¶
func NewMockTicker(duration time.Duration) *MockTicker
NewMockTicker creates a new MockTicker with the internal time set to time.Now().
func NewMockTickerAt ¶
func NewMockTickerAt(now time.Time, duration time.Duration) *MockTicker
NewMockTickerAt creates a new MockTicker with the internal time set to the given time.
func (*MockTicker) BlockingAdvance ¶
func (t *MockTicker) BlockingAdvance(duration time.Duration)
BlockingAdvance will bump the ticker's internal time by the given duration. If If the new internal time passes the next tick threshold, a signal will be sent. This method will not return until the signal is read by a consumer of the ticker.
func (*MockTicker) Chan ¶
func (t *MockTicker) Chan() <-chan time.Time
Chan returns a channel which will receive the tickers's internal time at the interval given when creating the ticker.
func (MockTicker) SetCurrent ¶
SetCurrent sets the clock's internal time to the given time.
type Ticker ¶
type Ticker interface {
// Chan returns the underlying ticker channel.
Chan() <-chan time.Time
// Stop stops the ticker.
Stop()
}
Ticker is a wrapper around a time.Ticker, which allows interface access to the underlying channel (instead of bare access like the time.Ticker struct allows).
