clock

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2023 License: Apache-2.0 Imports: 2 Imported by: 4

Documentation

Overview

Package clock defines interfaces for interacting with the passage of time.

For testing, it is sometimes appropriate to inject a mock clock into a function to control it, for example, when it uses a timer to make a decision.

This package's interface mirrors that of the Kubernetes API machinery clock interface. We encourage you to use their mocks when possible. You can find an easy-to-use adapter in the package clock/k8sext.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	PassiveClock
	After(time.Duration) <-chan time.Time
	NewTimer(time.Duration) Timer
	Sleep(time.Duration)
	NewTicker(time.Duration) Ticker
}

Clock is a clock that provides both timing information and the ability to schedule.

var RealClock Clock = &realClock{}

RealClock is the system clock. It delegates to the time package.

func NewTimerCallbackClock

func NewTimerCallbackClock(delegate Clock, fn TimerCallbackFunc) Clock

NewTimerCallbackClock makes a clock that calls a particular function just after NewTimer() or a timer's Reset() is called.

type PassiveClock

type PassiveClock interface {
	Now() time.Time
	Since(time.Time) time.Duration
}

PassiveClock is a clock that provides timing information but not the ability to schedule events for the future.

type Ticker

type Ticker interface {
	C() <-chan time.Time
	Stop()
}

Ticker is an interface that allows for abstraction of time.Ticker.

type Timer

type Timer interface {
	C() <-chan time.Time
	Stop() bool
	Reset(d time.Duration) bool
}

Timer is an interface that allows for abstraction of time.Timer.

func AfterFunc added in v0.3.0

func AfterFunc(c Clock, d time.Duration, fn func()) Timer

AfterFunc abstracts time.AfterFunc to use a Clock.

Like the time package, the returned Timer will never report a time on its channel. The semantics of the Stop and Reset methods are also the same.

type TimerCallbackFunc

type TimerCallbackFunc func(d time.Duration)

TimerCallbackFunc is the callback function for a timer.

Directories

Path Synopsis
Package k8sext provides adapters for the Kubernetes API machinery clock implementation to this library.
Package k8sext provides adapters for the Kubernetes API machinery clock implementation to this library.

Jump to

Keyboard shortcuts

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