Documentation
¶
Overview ¶
Package testclock provides a deterministic, manually-advanced implementation of warden.IClock for tests. Time only moves when Advance is called; timers and tickers fire from Advance in chronological order. This lets the election and watchdog state machines be exercised without real sleeps, so tests are fast and non-flaky.
The zero value is not usable; construct with New. A single *Clock can be shared by an entire simulated cluster so every node observes the same global time.
This is a supported test double, not an internal fixture: anything built on warden.IClock — including code outside this repository — can drive it the same way warden's own suites do. Callers may rely on Advance being the only thing that moves time, and on the waiters due within one Advance firing in deadline order, ties broken by creation order, so a test observes one fixed interleaving rather than a scheduler-dependent one. Delivery matches the real time package rather than improving on it: each channel is buffered to one and the send is non-blocking, so a tick an unread receiver has not drained is dropped exactly as time.Ticker would drop it. BlockUntilTimers is how a test waits for the code under test to arm its timers before advancing. It never returns wall-clock time and has no place in a production wiring.
Index ¶
- type Clock
- func (c *Clock) Advance(d time.Duration)
- func (c *Clock) After(d time.Duration) <-chan time.Time
- func (c *Clock) ArmedTimers() int
- func (c *Clock) BlockUntilTimers(n int)
- func (c *Clock) NewTicker(d time.Duration) warden.Ticker
- func (c *Clock) NewTimer(d time.Duration) warden.Timer
- func (c *Clock) Now() time.Time
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Clock ¶
type Clock struct {
// contains filtered or unexported fields
}
Clock is a deterministic fake clock implementing warden.IClock. All methods are safe for concurrent use.
func (*Clock) Advance ¶
Advance moves simulated time forward by d, firing every timer and ticker whose deadline falls within the new interval, in chronological order. Tickers reschedule and may fire multiple times. After each fire the lock is released and the scheduler is yielded so a woken goroutine gets a chance to run (and arm follow-on timers that Advance will then also fire if due), keeping multi-goroutine tests deterministic when combined with an explicit settle barrier.
func (*Clock) After ¶
After implements warden.IClock. It returns a channel that receives the time once d has elapsed (via Advance).
func (*Clock) ArmedTimers ¶
ArmedTimers returns the number of currently armed timers/tickers.
func (*Clock) BlockUntilTimers ¶
BlockUntilTimers blocks until at least n timers/tickers are armed. It is the recommended synchronization point for tests that arm timers on background goroutines: wait for the expected number to be registered before advancing.
func (*Clock) NewTicker ¶
NewTicker implements warden.IClock. It panics if d <= 0, mirroring time.NewTicker.