testclock

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2023 License: LGPL-3.0 Imports: 9 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AdvanceableClock added in v1.0.1

type AdvanceableClock interface {
	clock.Clock
	Advance(time.Duration)
}

AdvanceableClock is a clock that can be advanced to trigger timers/trigger timers earlier than they would otherwise.

func NewDilatedWallClock

func NewDilatedWallClock(realSecondDuration time.Duration) AdvanceableClock

NewDilatedWallClock returns a clock that can be sped up or slowed down. realSecondDuration is the real duration of a second.

type AutoAdvancingClock

type AutoAdvancingClock struct {
	clock.Clock
	Advance func(time.Duration)
}

AutoAdvancingClock wraps a clock.Clock, calling the Advance function whenever After or AfterFunc are called.

func (*AutoAdvancingClock) After

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

func (*AutoAdvancingClock) AfterFunc

func (c *AutoAdvancingClock) AfterFunc(d time.Duration, f func()) clock.Timer

type Clock

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

Clock implements a mock clock.Clock for testing purposes.

func NewClock

func NewClock(now time.Time) *Clock

NewClock returns a new clock set to the supplied time. If your SUT needs to call After, AfterFunc, NewTimer or Timer.Reset more than 10000 times: (1) you have probably written a bad test; and (2) you'll need to read from the Alarms chan to keep the buffer clear.

func (*Clock) Advance

func (clock *Clock) Advance(d time.Duration)

Advance advances the result of Now by the supplied duration, and sends the "current" time on all alarms which are no longer "in the future".

func (*Clock) After

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

After is part of the clock.Clock interface.

func (*Clock) AfterFunc

func (clock *Clock) AfterFunc(d time.Duration, f func()) clock.Timer

AfterFunc is part of the clock.Clock interface.

func (*Clock) Alarms

func (clock *Clock) Alarms() <-chan struct{}

Alarms returns a channel on which you can read one value for every call to After and AfterFunc; and for every successful Timer.Reset backed by this Clock. It might not be elegant but it's necessary when testing time logic that runs on a goroutine other than that of the test.

func (*Clock) NewTimer

func (clock *Clock) NewTimer(d time.Duration) clock.Timer

func (*Clock) Now

func (clock *Clock) Now() time.Time

Now is part of the clock.Clock interface.

func (*Clock) WaitAdvance

func (clock *Clock) WaitAdvance(d, w time.Duration, n int) error

WaitAdvance functions the same as Advance, but only if there is n timers in clock.waiting. This came about while fixing lp:1607044 intermittent failures. It turns out that testing.Clock.Advance might advance the time and trigger notifications before triggers are set. So we wait a limited time 'w' for 'n' timers to show up in clock.waiting, and if they do we advance 'd'.

Jump to

Keyboard shortcuts

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