Documentation ¶
Index ¶
- Variables
- type Clock
- type DefaultClock
- func (dc DefaultClock) After(d time.Duration) <-chan time.Time
- func (dc DefaultClock) AfterFunc(d time.Duration, f func()) Timer
- func (dc DefaultClock) NewTicker(d time.Duration) Ticker
- func (dc DefaultClock) NewTimer(d time.Duration) Timer
- func (dc DefaultClock) Now() time.Time
- func (dc DefaultClock) Since(t time.Time) time.Duration
- func (dc DefaultClock) Sleep(d time.Duration)
- func (dc DefaultClock) Tick(d time.Duration) <-chan time.Time
- type MockClock
- func (m *MockClock) AddTime(d time.Duration)
- func (m *MockClock) After(d time.Duration) <-chan time.Time
- func (m *MockClock) AfterFunc(d time.Duration, f func()) Timer
- func (m *MockClock) NewTicker(d time.Duration) Ticker
- func (m *MockClock) NewTimer(d time.Duration) Timer
- func (m *MockClock) Now() time.Time
- func (m *MockClock) SetTime(t time.Time)
- func (m *MockClock) Since(t time.Time) time.Duration
- func (m *MockClock) Sleep(d time.Duration)
- func (m *MockClock) Tick(d time.Duration) <-chan time.Time
- type Ticker
- type Timer
Constants ¶
This section is empty.
Variables ¶
var C = DefaultClock{}
C holds a default clock that uses time.Now as its time source. This is what you should normally use in your code.
Functions ¶
This section is empty.
Types ¶
type Clock ¶
type Clock interface { Now() time.Time After(d time.Duration) <-chan time.Time Sleep(d time.Duration) Tick(d time.Duration) <-chan time.Time AfterFunc(d time.Duration, f func()) Timer NewTimer(d time.Duration) Timer NewTicker(d time.Duration) Ticker Since(t time.Time) time.Duration }
The Clock interface provides time-based functionality. It should be used rather than the `time` package in situations where you want to mock things.
type DefaultClock ¶
type DefaultClock struct{}
DefaultClock is an implementation of the Clock interface that uses standard time methods.
func (DefaultClock) After ¶
func (dc DefaultClock) After(d time.Duration) <-chan time.Time
After waits for the duration to elapse and then sends the current time on the returned channel.
func (DefaultClock) AfterFunc ¶
func (dc DefaultClock) AfterFunc(d time.Duration, f func()) Timer
AfterFunc waits for the duration to elapse and then calls f in its own goroutine. It returns a Timer that can be used to cancel the call using its Stop method.
func (DefaultClock) NewTicker ¶
func (dc DefaultClock) NewTicker(d time.Duration) Ticker
NewTicker returns a new Ticker containing a channel that will send the time with a period specified by the duration argument.
func (DefaultClock) NewTimer ¶
func (dc DefaultClock) NewTimer(d time.Duration) Timer
NewTimer creates a new Timer that will send the current time on its channel after at least duration d.
func (DefaultClock) Now ¶
func (dc DefaultClock) Now() time.Time
Now returns the current local time.
func (DefaultClock) Since ¶
func (dc DefaultClock) Since(t time.Time) time.Duration
Since returns the time elapsed since t.
func (DefaultClock) Sleep ¶
func (dc DefaultClock) Sleep(d time.Duration)
Sleep pauses the current goroutine for at least the duration d. A negative or zero duration causes Sleep to return immediately.
func (DefaultClock) Tick ¶
func (dc DefaultClock) Tick(d time.Duration) <-chan time.Time
Tick is a convenience wrapper for NewTicker providing access to the ticking channel only. While Tick is useful for clients that have no need to shut down the Ticker, be aware that without a way to shut it down the underlying Ticker cannot be recovered by the garbage collector; it "leaks".
type MockClock ¶
type MockClock struct {
// contains filtered or unexported fields
}
MockClock provides a Clock whose time only changes or advances when manually specified to do so. This is useful for unit tests.
func NewMockClock ¶
NewMockClock creates a new mock clock, with its current time set to the provided optional start time.
func (*MockClock) After ¶
After waits for the duration to elapse and then sends the current time on the returned channel.
func (*MockClock) AfterFunc ¶
AfterFunc waits for the duration to elapse and then calls f in its own goroutine. It returns a Timer that can be used to cancel the call using its Stop method.
func (*MockClock) NewTicker ¶
NewTicker returns a new mock Ticker containing a channel that will send the time with a period specified by the duration argument. Note: unlike the default ticker included in Go, the mock ticker will *never* skip ticks as time advances.
func (*MockClock) NewTimer ¶
NewTimer creates a new mock Timer that will send the current time on its channel after at least duration d.
func (*MockClock) Sleep ¶
Sleep pauses the current goroutine for at least the duration d. A negative or zero duration causes Sleep to return immediately.
func (*MockClock) Tick ¶
Tick is a convenience wrapper for NewTicker providing access to the ticking channel only. While Tick is useful for clients that have no need to shut down the Ticker, be aware that without a way to shut it down the underlying Ticker cannot be recovered by the garbage collector; it "leaks".
type Ticker ¶
The Ticker is an interface for time.Ticker, and can also be swapped in mocks. This *does* change its API so that it can fit into an interface -- rather than using the channel at .C, you should call Chan() and use the returned channel just as you would .C.
type Timer ¶
The Timer is an interface for time.Timer, and can also be swapped in mocks. This *does* change its API so that it can fit into an interface -- rather than using the channel at .C, you should call Chan() and use the returned channel just as you would .C.
func NewMockTimer ¶
NewMockTimer creates a new Timer using the provided Clock. You should not use this directly outside of unit tests; use Clock.NewTimer().