clock

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2021 License: MIT Imports: 1 Imported by: 3

README

Build Status Code Coverage Go Report Card GoDoc

Documentation

Overview

Package clock is a near drop-in replacement for time.Now().UTC() and time.Sleep(). The structs defined here are intended to be used as pointer fields on struct types. When nil, these references forward to the corresponding functions in the standard time package. When not nil they perform behavior that facilitates unit testing when accessing the current time or sleeping is involved. The main advantage to this approach is that it is not necessary to provide a non-nil instance in 'contructor' functions or wireup for production code. It is also still trivial to set a non-nil reference in test code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func UTCNow

func UTCNow() time.Time

UTCNow is analogous to time.Now().UTC() // (unless frozen)

Types

type Clock

type Clock struct {
	// contains filtered or unexported fields
}

Clock is meant be included as a pointer field on a struct. Leaving the instance as a nil reference will cause any calls on the *Clock to forward to the corresponding functions in the standard time package. This is meant to be the behavior in production. In testing, set the field to a non-nil instance of a *Clock to provide a frozen time instant whenever UTCNow() is called.

func Freeze

func Freeze(instants ...time.Time) *Clock

Freeze creates a new *Clock instance with an internal time instant. This function is meant to be called from test code. See the godoc for the Clock struct for details.

func (*Clock) TimeSince

func (it *Clock) TimeSince(instant time.Time) time.Duration

TimeSince is analogous to time.Since(instant) // (unless frozen)

func (*Clock) UTCNow

func (it *Clock) UTCNow() time.Time

UTCNow is analogous to time.Now().UTC() // (unless frozen)

type Sleeper

type Sleeper struct {
	Naps []time.Duration
}

Sleeper is meant be included as a pointer field on a struct. Leaving the instance as a nil reference will cause any calls on the *Sleeper to forward to the corresponding functions in the standard time package. This is meant to be the behavior in production. In testing, set the field to a non-nil instance of a *Sleeper to record sleep durations for later inspection.

func StayAwake

func StayAwake() *Sleeper

StayAwake creates a new *Sleeper instance with an internal duration slice. This function is meant to be called from test code. See the godoc for the Sleeper struct for details.

func (*Sleeper) Sleep

func (it *Sleeper) Sleep(duration time.Duration)

Sleep -> time.Sleep

Jump to

Keyboard shortcuts

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