clock

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

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

Go to latest
Published: Dec 24, 2016 License: MIT Imports: 2 Imported by: 1

README

clock Build Status Coverage Status GoDoc

This Go package provides a Clock that returns the time; and can be mocked.

Install

go get github.com/stephanos/clock

Documentation

godoc.org

License

MIT (see LICENSE).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func After

func After(d time.Duration) <-chan time.Time

After waits for the duration to elapse and then sends the current time on the returned channel. It is equivalent to NewTimer(d).C.

func Now

func Now() time.Time

Now returns the current local time.

func Sleep

func 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 Tick

func Tick(d time.Duration) <-chan time.Time

Tick is a convenience wrapper for NewTicker providing access to the ticking channel only.

func Ticker

func Ticker(d time.Duration) *time.Ticker

Ticker returns a new Ticker containing a channel that will send the time with a period specified by the duration argument. It adjusts the intervals or drops ticks to make up for slow receivers. The duration d must be greater than zero; if not, Ticker will panic.

Types

type Clock

type Clock interface {

	// Now returns the current local time.
	Now() time.Time

	// Sleep pauses the current goroutine for at least the duration d.
	// A negative or zero duration causes Sleep to return immediately.
	Sleep(d time.Duration)

	// After waits for the duration to elapse and then sends the current time
	// on the returned channel. It is equivalent to NewTimer(d).C.
	After(d time.Duration) <-chan time.Time

	// Tick is a convenience wrapper for NewTicker providing access to the
	// ticking channel only.
	Tick(d time.Duration) <-chan time.Time

	// Ticker returns a new Ticker containing a channel that will send the
	// time with a period specified by the duration argument. It adjusts
	// the intervals or drops ticks to make up for slow receivers.
	// The duration d must be greater than zero; if not, Ticker will panic.
	Ticker(d time.Duration) *time.Ticker
}

Clock provides the functions from the time package.

var Work Clock

Work mirrors the behaviour of Go's time package.

func New

func New() Clock

New returns a new Clock that mirrors the behaviour of the time package.

type Mock

type Mock interface {
	Clock

	// Set applies the passed-in time to the Clock's time.
	Set(t time.Time) Mock

	// Add changes the Clock's time by the passed-in duration.
	Add(d time.Duration) Mock

	// Freeze stops the clock's time.
	Freeze() Mock

	// Freeze stops the clock's time at the passed-in moment.
	FreezeAt(t time.Time) Mock

	// IsFrozen is whether the clock's time is stopped.
	IsFrozen() bool

	// Unfreeze starts the clock's time again.
	Unfreeze() Mock

	// SetSleep overrides the passed-in argument to the Sleep method.
	SetSleep(d time.Duration) Mock

	// NoSleep disables the Sleep method.
	NoSleep() Mock

	// ResetSleep re-enables the default Sleep behaviour.
	ResetSleep() Mock
}

Mock represents a manipulable Work. It is concurrent-friendly.

func NewMock

func NewMock() Mock

NewMock returns a new manipulable Clock.

Jump to

Keyboard shortcuts

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