Documentation
¶
Overview ¶
Package clock makes the functionality of the time package injectable in unit tests and other code. It also provides a few convenient goodies, that reduce the work needed to write unit tests. To benefit from this package, instead of calling time.Now directly, pass clock.Real (type: clock.Clock) to your functions or structs, and call its Now methods: Now, Sleep etc. Then in unit tests, pass a clock that you control, created with clock.NewController.
Index ¶
- Variables
- func MustLoadLocation(name string) *time.Location
- func MustParse(layout, value string) time.Time
- func MustParseDuration(s string) time.Duration
- func MustParseInLocation(layout, value string, loc *time.Location) time.Time
- type Clock
- type Controller
- func (c *Controller) After(d time.Duration) <-chan time.Time
- func (c *Controller) NewTimer(d time.Duration) *Timer
- func (c *Controller) Now() time.Time
- func (c *Controller) Since(t time.Time) time.Duration
- func (c *Controller) Sleep(d time.Duration)
- func (c *Controller) Until(t time.Time) time.Duration
- type ControllerOpts
- type Timer
Constants ¶
This section is empty.
Variables ¶
var Real = Clock(realClock{})
Real is a Clock whose methods call the functions of the time package with the same name.
Functions ¶
func MustLoadLocation ¶
MustLoadLocation is a wrapper around LoadLocation that panics on error.
func MustParseDuration ¶
MustParseDuration is a wrapper around time.ParseDuration that panics on parse error.
Types ¶
type Clock ¶
type Clock interface { After(time.Duration) <-chan time.Time NewTimer(d time.Duration) *Timer Now() time.Time Since(time.Time) time.Duration Sleep(time.Duration) Until(time.Time) time.Duration }
Clock is the top level interface that encapsulates the time package functionality.
type Controller ¶
type Controller struct { Opts ControllerOpts // contains filtered or unexported fields }
Controller is a Clock implementation that gives you control over time!
func NewController ¶
func NewController(options *ControllerOpts) *Controller
NewController creates a new controller. The options argument can be nil.
func (*Controller) After ¶
func (c *Controller) After(d time.Duration) <-chan time.Time
After is equivalent to NewTimer(d).C.
func (*Controller) NewTimer ¶
func (c *Controller) NewTimer(d time.Duration) *Timer
NewTimer creates a new Timer that will send the current simulated time on its channel after it becomes >= c.Now().Add(d).
func (*Controller) Now ¶
func (c *Controller) Now() time.Time
Now returns the simulated current time.
func (*Controller) Sleep ¶
func (c *Controller) Sleep(d time.Duration)
Sleep advances the simulated current time by the passed duration, it it's >0.
type ControllerOpts ¶
type ControllerOpts struct {
InitialTime time.Time // optional; if zero, the initial time is time.Now()
}
ControllerOpts are options for Controller configuration.