timing

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2023 License: BSD-2-Clause Imports: 2 Imported by: 0

Documentation

Overview

Forked from github.com/StefanKopieczek/gossip by @StefanKopieczek

Index

Constants

This section is empty.

Variables

View Source
var MockMode = false

Controls whether library calls should be mocked, or whether we should use the standard Go time library. If we're in Mock Mode, then time does not pass as normal, but only progresses when Elapse is called. False by default, indicating that we just call through to standard Go functions.

Functions

func After

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

See built-in time.After() function.

func Elapse

func Elapse(d time.Duration)

Increment the current time by the given Duration. This function can only be called in Mock Mode, otherwise we will panic.

func Now

func Now() time.Time

Returns the current time. If Mock Mode is set, this will be the sum of all Durations passed into Elapse calls; otherwise it will be the true system time.

func Sleep

func Sleep(d time.Duration)

See built-in time.Sleep() function.

Types

type Timer

type Timer interface {
	// Returns a channel which sends the current time immediately when the timer expires.
	// Equivalent to time.Timer.C; however, we have to use a method here instead of a member since this is an interface.
	C() <-chan time.Time

	// Resets the timer such that it will expire in duration 'd' after the current time.
	// Returns true if the timer had been active, and false if it had expired or been stopped.
	Reset(d time.Duration) bool

	// Stops the timer, preventing it from firing.
	// Returns true if the timer had been active, and false if it had expired or been stopped.
	Stop() bool
}

Interface over Golang's built-in Timers, allowing them to be swapped out for mocked timers.

func AfterFunc

func AfterFunc(d time.Duration, f func()) Timer

See built-in time.AfterFunc() function.

func NewTimer

func NewTimer(d time.Duration) Timer

Creates a new Timer; either a wrapper around a standard Go time.Timer, or a mocked-out Timer, depending on whether MockMode is set.

Jump to

Keyboard shortcuts

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