Documentation ¶
Overview ¶
Package mclock is a wrapper for a monotonic clock source
Index ¶
- type AbsTime
- type ChanTimer
- type Clock
- type Simulated
- func (s *Simulated) ActiveTimers() int
- func (s *Simulated) After(d time.Duration) <-chan AbsTime
- func (s *Simulated) AfterFunc(d time.Duration, fn func()) Timer
- func (s *Simulated) NewTimer(d time.Duration) ChanTimer
- func (s *Simulated) Now() AbsTime
- func (s *Simulated) Run(d time.Duration)
- func (s *Simulated) Sleep(d time.Duration)
- func (s *Simulated) WaitForTimers(n int)
- type System
- type Timer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AbsTime ¶
type AbsTime int64
AbsTime represents absolute monotonic time.
type ChanTimer ¶ added in v1.1.0
type ChanTimer interface { Timer // The channel returned by C receives a value when the timer expires. C() <-chan AbsTime // Reset reschedules the timer with a new timeout. // It should be invoked only on stopped or expired timers with drained channels. Reset(time.Duration) }
ChanTimer is a cancellable event created by NewTimer.
type Clock ¶
type Clock interface { Now() AbsTime Sleep(time.Duration) NewTimer(time.Duration) ChanTimer After(time.Duration) <-chan AbsTime AfterFunc(d time.Duration, f func()) Timer }
The Clock interface makes it possible to replace the monotonic system clock with a simulated clock.
type Simulated ¶
type Simulated struct {
// contains filtered or unexported fields
}
Simulated implements a virtual Clock for reproducible time-sensitive tests. It simulates a scheduler on a virtual timescale where actual processing takes zero time.
The virtual clock doesn't advance on its own, call Run to advance it and execute timers. Since there is no way to influence the Go scheduler, testing timeout behaviour involving goroutines needs special care. A good way to test such timeouts is as follows: First perform the action that is supposed to time out. Ensure that the timer you want to test is created. Then run the clock until after the timeout. Finally observe the effect of the timeout using a channel or semaphore.
func (*Simulated) ActiveTimers ¶
ActiveTimers returns the number of timers that haven't fired.
func (*Simulated) After ¶
After returns a channel which receives the current time after the clock has advanced by d.
func (*Simulated) AfterFunc ¶ added in v1.1.0
AfterFunc runs fn after the clock has advanced by d. Unlike with the system clock, fn runs on the goroutine that calls Run.
func (*Simulated) NewTimer ¶ added in v1.1.0
NewTimer creates a timer which fires when the clock has advanced by d.
func (*Simulated) Run ¶
Run moves the clock by the given duration, executing all timers before that duration.
func (*Simulated) WaitForTimers ¶
WaitForTimers waits until the clock has at least n scheduled timers.
type System ¶
type System struct{}
System implements Clock using the system clock.
func (System) AfterFunc ¶ added in v1.1.0
AfterFunc runs f on a new goroutine after the duration has elapsed.