Skip to main content

MockTimeline

Struct MockTimeline 

Source
pub struct MockTimeline { /* private fields */ }
Expand description

Shared monotonic time source for deterministic tests.

MockTimeline is the single authority for elapsed mock time. Cloned timelines share the same internal state, so a clock, sleeper, and future timeout-aware primitive can all observe the same logical time progression. The timeline starts at elapsed zero and advances only when a test calls advance. It never follows wall-clock time by itself.

The timeline maintains two related notions of progress:

  • elapsed time is the monotonic duration since the timeline origin. MockClock and crate::sleep::MockSleeper derive their behavior from this value.
  • event epoch is a notification counter incremented when time advances or when notify_external_change is called. It lets monitor-like primitives wait for either a state change or a later deadline without inventing a second mock clock.

Blocking waiters use condition variables, and async waiters use a Tokio watch channel when the tokio feature is enabled. Waiter counts are tracked by MockWaiterKind so tests can wait until a thread or future has really entered a mock wait before advancing time. Reset is rejected while waiters are active, because rewinding a timeline under a blocked waiter would make deadline semantics ambiguous. Deadlines also carry the id of the timeline that created them. Passing a deadline from one timeline into another timeline is rejected with MockTimeError::MismatchedTimeline.

MockTimeline uses non-poisoning synchronization primitives internally. A panic in one test thread does not permanently poison the timeline for the rest of the test process.

Implementations§

Source§

impl MockTimeline

Source

pub fn new() -> Self

Creates a new timeline at elapsed zero.

§Returns

A mock timeline with no elapsed time.

Source

pub const fn id(&self) -> u64

Returns the globally unique id of this timeline.

Clones of the same timeline return the same id. Independently created timelines receive different ids, which lets deadline APIs reject MockInstant values from the wrong timeline.

§Returns

The timeline id.

Source

pub fn elapsed(&self) -> Duration

Returns elapsed mock time as a standard duration.

§Returns

Elapsed monotonic time since the timeline origin.

Source

pub fn elapsed_nanos(&self) -> u128

Returns elapsed mock time in nanoseconds.

§Returns

Elapsed monotonic nanoseconds since the timeline origin.

Source

pub fn now(&self) -> MockInstant

Returns the current mock instant.

§Returns

Current instant on this timeline.

Source

pub fn event_epoch(&self) -> u64

Returns the current event epoch.

§Returns

Epoch incremented by time advances and external notifications.

Source

pub fn advance(&self, duration: Duration)

Advances mock time and wakes all timeline waiters.

§Parameters
  • duration: Non-negative duration to add.
Source

pub fn reset(&self) -> Result<(), MockTimeError>

Resets the timeline to elapsed zero when no waiters are active.

§Returns

Ok(()) when reset succeeds.

§Errors

Returns MockTimeError::ActiveWaiters when timeline waiters are active.

Source

pub fn notify_external_change(&self)

Wakes waiters without changing elapsed time.

This is intended for synchronization primitives that combine state-change notifications with timeout deadlines.

Source

pub fn wait_until(&self, deadline: MockInstant) -> Result<(), MockTimeError>

Blocks until the current mock instant reaches deadline.

§Parameters
  • deadline: Mock instant at which the wait should complete.
§Returns

Ok(()) when the wait completes.

§Errors

Returns MockTimeError::MismatchedTimeline if deadline was created by a different timeline.

Source

pub fn wait_for(&self, duration: Duration)

Blocks until duration has elapsed on the mock timeline.

§Parameters
  • duration: Relative mock duration to wait.
Source

pub fn wait_for_event_after(&self, observed_epoch: u64)

Blocks until the event epoch changes after observed_epoch.

§Parameters
  • observed_epoch: Event epoch already observed by the caller.
Source

pub fn wait_for_blocked_waiters( &self, kind: MockWaiterKind, count: usize, real_timeout: Duration, ) -> bool

Blocks until a registered waiter count is observed or real timeout expires.

§Parameters
  • kind: Waiter group to inspect.
  • count: Minimum number of waiters expected.
  • real_timeout: Real wall-clock limit used only to keep tests from hanging forever.
§Returns

true when enough waiters are observed before the real timeout.

Trait Implementations§

Source§

impl Clone for MockTimeline

Source§

fn clone(&self) -> MockTimeline

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MockTimeline

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for MockTimeline

Source§

fn default() -> Self

Creates a zero-elapsed mock timeline.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.