clock

package module
v0.0.0-...-1909248 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2022 License: BSD-3-Clause Imports: 3 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var Real = Clock(realClock{})

Real is a Clock whose methods call the functions of the time package with the same name.

Functions

func MustLoadLocation

func MustLoadLocation(name string) *time.Location

MustLoadLocation is a wrapper around LoadLocation that panics on error.

func MustParse

func MustParse(layout, value string) time.Time

MustParse is a wrapper around time.Parse that panics on parse error.

func MustParseDuration

func MustParseDuration(s string) time.Duration

MustParseDuration is a wrapper around time.ParseDuration that panics on parse error.

func MustParseInLocation

func MustParseInLocation(layout, value string, loc *time.Location) time.Time

MustParseInLocation is a wrapper around time.ParseInLocation 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) Since

func (c *Controller) Since(t time.Time) time.Duration

func (*Controller) Sleep

func (c *Controller) Sleep(d time.Duration)

Sleep advances the simulated current time by the passed duration, it it's >0.

func (*Controller) Until

func (c *Controller) Until(t time.Time) time.Duration

type ControllerOpts

type ControllerOpts struct {
	InitialTime time.Time // optional; if zero, the initial time is time.Now()
}

ControllerOpts are options for Controller configuration.

type Timer

type Timer struct {
	C <-chan time.Time
}

Timer is an equivalent of time.Timer with a reduced API.

Jump to

Keyboard shortcuts

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