clock

package
v1.24.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2017 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package clock provides swappable real and fake clocks. The real clock is a stateless wrapper around the "time" module. The fake clock provides manual control over progress. They share a common Clock and Timer interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	AfterFunc(d time.Duration, f func()) Timer
	After(d time.Duration) <-chan time.Time
	Now() time.Time
	Sleep(d time.Duration)
}

Clock represents an interface to the functions in the standard library time package. Two implementations are available in the clock package. The first is a real-time clock which simply wraps the time package's functions. The second is a mock clock which will only make forward progress when programmatically adjusted.

type FakeClock

type FakeClock struct {
	sync.Mutex
	// contains filtered or unexported fields
}

FakeClock represents a fake clock that only moves forward programmically. It can be preferable to a real-time clock when testing time-based functionality.

func NewFake

func NewFake() *FakeClock

NewFake returns an instance of a fake clock. The current time of the fake clock on initialization is the Unix epoch.

func (*FakeClock) Add

func (fc *FakeClock) Add(d time.Duration)

Add moves the current time of the fake clock forward by the duration. This should only be called from a single goroutine at a time.

func (*FakeClock) After

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

After produces a channel that will emit the time after a duration passes.

func (*FakeClock) AfterFunc

func (fc *FakeClock) AfterFunc(d time.Duration, f func()) Timer

AfterFunc waits for the duration to elapse and then executes a function. A Timer is returned that can be stopped.

func (*FakeClock) FakeAfterFunc

func (fc *FakeClock) FakeAfterFunc(d time.Duration, f func()) *FakeTimer

FakeAfterFunc waits for the duration to elapse and then executes a function. A Timer is returned that can be stopped.

func (*FakeClock) FakeTimer

func (fc *FakeClock) FakeTimer(d time.Duration) *FakeTimer

FakeTimer produces a timer that will emit a time some duration after now, exposing the fake timer internals and type.

func (*FakeClock) Now

func (fc *FakeClock) Now() time.Time

Now returns the current time on the fake clock.

func (*FakeClock) Set

func (fc *FakeClock) Set(end time.Time)

Set advances the current time of the fake clock to the given absolute time.

func (*FakeClock) Sleep

func (fc *FakeClock) Sleep(d time.Duration)

Sleep pauses the goroutine for the given duration on the fake clock. The clock must be moved forward in a separate goroutine.

func (*FakeClock) Timer

func (fc *FakeClock) Timer(d time.Duration) Timer

Timer produces a timer that will emit a time some duration after now.

type FakeTimer

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

FakeTimer represents a single event.

func (*FakeTimer) C

func (t *FakeTimer) C() <-chan time.Time

C returns a channel that will send the time when it fires.

func (*FakeTimer) Reset

func (t *FakeTimer) Reset(d time.Duration) bool

Reset adjusts the timer's scheduled time forward from now, unless it has already fired.

func (*FakeTimer) Stop

func (t *FakeTimer) Stop() bool

Stop removes a timer from the scheduled timers.

type RealClock

type RealClock struct{}

RealClock implements a real-time clock by simply wrapping the time package functions.

func NewReal

func NewReal() RealClock

NewReal returns an instance of a real clock (changing in the very real fourth dimension).

func (RealClock) After

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

After produces a channel that will emit the time after a duration passes.

func (RealClock) AfterFunc

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

AfterFunc waits for the duration to elapse and then executes a function. A Timer is returned that can be stopped.

func (RealClock) Now

func (RealClock) Now() time.Time

Now returns the current time on the real clock.

func (RealClock) Sleep

func (RealClock) Sleep(d time.Duration)

Sleep pauses the goroutine for the given duration on the fake clock. The clock must be moved forward in a separate goroutine.

func (RealClock) Timer

func (RealClock) Timer(d time.Duration) Timer

Timer produces a timer that will emit a time some duration after now.

type Timer

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

Timer represents an individual timer in a clock, either real or fake. Unlike time.Timer, this is an interface instead of a struct to abstract the implementation. Consequently, the C property is instead a C() method.

Jump to

Keyboard shortcuts

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