pub struct TestClock { /* private fields */ }test-clock and (crate features test-clock) only.Expand description
Owns a clock that moves only when advanced, for tests.
Self::clock hands out handles that read and sleep on its time. Only the
owner moves it, through methods that take &mut self, so each test clock
has one driver.
Implementations§
Source§impl TestClock
impl TestClock
Sourcepub fn advance(&mut self, by: Duration)
pub fn advance(&mut self, by: Duration)
Moves both times forward by by, waking the sleeps and deadline waits
it reaches and firing its due timers.
Returns once the reached waits are woken and the due timers hold their messages, without waiting for any thread to act. Other waits keep waiting, since an advance never counts as a notification. A zero advance does nothing.
§Panics
Panics if either time would overflow, before changing either one.
Sourcepub fn advance_to(&mut self, target: Instant)
pub fn advance_to(&mut self, target: Instant)
Moves monotonic time to target and wall time by the same amount,
waking the sleeps and deadline waits it reaches and firing its due timers.
Returns once the reached waits are woken and the due timers hold their messages, without waiting for any thread to act. Other waits keep waiting, since an advance never counts as a notification. Advancing to the current time does nothing.
§Panics
Panics if target is before now or wall time would overflow.
Neither time changes after a panic.
Sourcepub fn set_system_time(&mut self, time: SystemTime)
pub fn set_system_time(&mut self, time: SystemTime)
Sets wall time forwards or backwards without moving monotonic time.
This wakes no waits and fires no timers, since deadlines use monotonic time.
Sourcepub fn wait_blocked(&self, count: usize)
pub fn wait_blocked(&self, count: usize)
Blocks until at least count threads are parked in this clock’s sleeps
and condvar waits.
Threads blocked in crossbeam receives and selects do not count. A thread
parked earlier counts too, so the count proves no progress on its own.
Nothing bounds the wait, so run tests under a runner with a per-test
timeout, since cargo test alone never stops a hung test.
Sourcepub fn wait_timers(&self, count: usize)
Available on crate feature crossbeam only.
pub fn wait_timers(&self, count: usize)
crossbeam only.Blocks until at least count timers are armed on this clock and unfired.
A waiting receive’s timer counts, and so does a timer whose receiver was
dropped. A timer armed earlier counts too, so the count proves no
progress on its own. Nothing bounds the wait, so run tests under a runner
with a per-test timeout, since cargo test alone never stops a hung test.
Sourcepub fn next_deadline(&self) -> Option<Instant>
pub fn next_deadline(&self) -> Option<Instant>
Returns the earliest deadline among this clock’s parked sleeps, deadline
waits and unfired timers, or None when there is none.
A timed wait stays listed until it stops waiting, so one an advance reaches stays listed until its thread runs, and the result is then the current time. Await an advance’s effect before reading the next deadline. A timer leaves the list when it fires.