Documentation ¶
Overview ¶
Package clock provides swappable real and fake clocks. The real clock is a stateless wrapper around the "time" module. The fake clock provides manual control over progress. They share a common Clock and Timer interface.
Index ¶
- type Clock
- type FakeClock
- func (fc *FakeClock) Add(d time.Duration)
- func (fc *FakeClock) After(d time.Duration) <-chan time.Time
- func (fc *FakeClock) AfterFunc(d time.Duration, f func()) Timer
- func (fc *FakeClock) FakeAfterFunc(d time.Duration, f func()) *FakeTimer
- func (fc *FakeClock) FakeTimer(d time.Duration) *FakeTimer
- func (fc *FakeClock) Now() time.Time
- func (fc *FakeClock) Set(end time.Time)
- func (fc *FakeClock) Sleep(d time.Duration)
- func (fc *FakeClock) Timer(d time.Duration) Timer
- type FakeTimer
- type RealClock
- type Timer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Clock ¶
type Clock interface { AfterFunc(d time.Duration, f func()) Timer After(d time.Duration) <-chan time.Time Now() time.Time Sleep(d time.Duration) }
Clock represents an interface to the functions in the standard library time package. Two implementations are available in the clock package. The first is a real-time clock which simply wraps the time package's functions. The second is a mock clock which will only make forward progress when programmatically adjusted.
type FakeClock ¶
FakeClock represents a fake clock that only moves forward programmically. It can be preferable to a real-time clock when testing time-based functionality.
func NewFake ¶
func NewFake() *FakeClock
NewFake returns an instance of a fake clock. The current time of the fake clock on initialization is the Unix epoch.
func (*FakeClock) Add ¶
Add moves the current time of the fake clock forward by the duration. This should only be called from a single goroutine at a time.
func (*FakeClock) AfterFunc ¶
AfterFunc waits for the duration to elapse and then executes a function. A Timer is returned that can be stopped.
func (*FakeClock) FakeAfterFunc ¶
FakeAfterFunc waits for the duration to elapse and then executes a function. A Timer is returned that can be stopped.
func (*FakeClock) FakeTimer ¶
FakeTimer produces a timer that will emit a time some duration after now, exposing the fake timer internals and type.
type FakeTimer ¶
type FakeTimer struct {
// contains filtered or unexported fields
}
FakeTimer represents a single event.
type RealClock ¶
type RealClock struct{}
RealClock implements a real-time clock by simply wrapping the time package functions.
func NewReal ¶
func NewReal() RealClock
NewReal returns an instance of a real clock (changing in the very real fourth dimension).
func (RealClock) AfterFunc ¶
AfterFunc waits for the duration to elapse and then executes a function. A Timer is returned that can be stopped.