clock

package
v0.0.0-...-eb934a0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package clock provides an abstraction for time to enable testing of functionality that uses time as an input.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	// Now provides the current local time. Equivalent to time.Now
	Now() time.Time

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

	// NewTicker returns a new Ticker containing a channel that will send
	// the current time on the channel after each tick. The period of the
	// ticks is specified by the duration argument. The ticker will adjust
	// the time interval or drop ticks to make up for slow receivers.
	// The duration d must be greater than zero; if not, NewTicker will
	// panic. Stop the ticker to release associated resources.
	NewTicker(d time.Duration) Ticker
}

Clock represents time in a way that can be provided by varying implementations. Methods are designed to be direct replacements for methods in the time package.

var SystemClock Clock = systemClock{}

SystemClock provides an instance of Clock that uses the system clock via methods in the time package.

type DeterministicClock

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

func NewDeterministicClock

func NewDeterministicClock(now time.Time) *DeterministicClock

NewDeterministicClock creates a new clock where time only advances when the DeterministicClock.AdvanceTime method is called. This is intended for use in situations where a deterministic clock is required, such as testing or event driven systems.

func (*DeterministicClock) AdvanceTime

func (s *DeterministicClock) AdvanceTime(d time.Duration)

AdvanceTime moves the time forward by the specific duration

func (*DeterministicClock) After

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

func (*DeterministicClock) NewTicker

func (s *DeterministicClock) NewTicker(d time.Duration) Ticker

func (*DeterministicClock) Now

func (s *DeterministicClock) Now() time.Time

func (*DeterministicClock) WaitForNewPendingTask

func (s *DeterministicClock) WaitForNewPendingTask(ctx context.Context) bool

WaitForNewPendingTask blocks until a new task is scheduled since the last time this method was called. true is returned if a new task was scheduled, false if the context completed before a new task was added.

func (*DeterministicClock) WaitForNewPendingTaskWithTimeout

func (s *DeterministicClock) WaitForNewPendingTaskWithTimeout(timeout time.Duration) bool

type SystemTicker

type SystemTicker struct {
	*time.Ticker
}

func (*SystemTicker) Ch

func (t *SystemTicker) Ch() <-chan time.Time

type Ticker

type Ticker interface {
	// Ch returns the channel for the ticker. Equivalent to time.Ticker.C
	Ch() <-chan time.Time

	// Stop turns off a ticker. After Stop, no more ticks will be sent.
	// Stop does not close the channel, to prevent a concurrent goroutine
	// reading from the channel from seeing an erroneous "tick".
	Stop()

	// Reset stops a ticker and resets its period to the specified duration.
	// The next tick will arrive after the new period elapses. The duration d
	// must be greater than zero; if not, Reset will panic.
	Reset(d time.Duration)
}

A Ticker holds a channel that delivers "ticks" of a clock at intervals

Jump to

Keyboard shortcuts

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