Skip to main content

TestClock

Struct TestClock 

Source
pub struct TestClock { /* private fields */ }
Available on crate feature 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

Source

pub fn new() -> Self

Creates a stopped clock at the current real monotonic and wall times.

Source

pub fn clock(&self) -> Clock

Returns a handle that reads and sleeps on this clock.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn wait_timers(&self, count: usize)

Available on crate feature 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.

Source

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.

Trait Implementations§

Source§

impl Debug for TestClock

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Shows the advance, wall time, parked threads and armed timers.

Source§

impl Default for TestClock

Source§

fn default() -> Self

Creates a stopped clock at the current real monotonic and wall times.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.