crock

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2018 License: BSD-2-Clause Imports: 5 Imported by: 0

README

crock

Crock is a fake time library for testing Go programs that use time.

Documentation

Overview

Package crock provides mock implementations of functions from the stdlib time package.

crock deliberately does not specify any interfaces or shims for "real" time. Consumers should create their own interfaces, and make a trivial shim to integrate functions from package time into their code.

See the example for some stuff you can lift for this purpose.

Index

Constants

View Source
const (
	// DefaultResolution is the resolution at which crock time updates.
	DefaultResolution = time.Second

	// DefaultMultiplier is the relative speed at which crock time progresses.
	DefaultMultiplier = 1.0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Duration

type Duration = time.Duration

type Time

type Time struct {

	// Resolution is the frequency events will be processed at once crock time
	// is started.
	Resolution time.Duration

	// Multiplier controls the relative speed of crock time. A multiplier of
	// 1.0 means crock time will proceed at nearly the same speed as real time.
	Multiplier float64
	// contains filtered or unexported fields
}

Time is a crock implementation of time. New Times are halted by default - they do not advance. Time proceeds according to the resolution and multiplier settings. By default, the Resolution is DefaultResolution and Multiplier is DefaultMultiplier.

Don't change the Resolution and Multiplier fields when time is in motion.

func NewTime

func NewTime(now time.Time) *Time

NewTime creates a new time, which is now. Resolution and Multiplier are set to their defaults.

func (*Time) After

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

After works like time.After, except it only sends on the channel it returns if crock time progresses enough.

func (*Time) AfterFunc

func (t *Time) AfterFunc(d Duration, f func()) *timeproxy.Timer

func (*Time) NewTicker

func (t *Time) NewTicker(d time.Duration) *timeproxy.Ticker

NewTicker creates a new crock ticker. It works like time.Ticker, except that it only ticks when time is progressing.

func (*Time) NewTimer

func (t *Time) NewTimer(d Duration) *timeproxy.Timer

NewTimer creates a new crock timer. It works like time.NewTimer except the timer only sends

func (*Time) Now

func (t *Time) Now() time.Time

Now returns t's current time.

func (*Time) Set

func (t *Time) Set(to time.Time)

Set sets crock time to a particular time. Can be invoked whether or not time is currently progressing. If there are timer or ticker events that would have occurred before the set crock time, they will fire.

func (*Time) Since

func (t *Time) Since(s time.Time) time.Duration

Since works like time.Since, performing t.Now().Sub(s)

func (*Time) Sleep

func (t *Time) Sleep(d time.Duration)

Sleep sleeps for real duration d / t.Multiplier.

func (*Time) Start

func (t *Time) Start()

Start causes crock time to progress at the rate determined by t.Multiplier. For example, if t.Multiplier is 0.5, crock time will progress twice as slowly as real time.

Don't call Start concurrently with other methods.

func (*Time) Stop

func (t *Time) Stop()

Stop stops crock time.

Don't call Stop concurrently with other methods.

func (*Time) Tick

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

Tick works like time.Tick, except it only ticks if crock time progresses enough.

func (*Time) Until

func (t *Time) Until(s time.Time) time.Duration

Until works like time.Until, performing s.Sub(t.Now())

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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