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.
MockClockandcrate::sleep::MockSleeperderive their behavior from this value. - event epoch is a notification counter incremented when time advances
or when
notify_external_changeis 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
impl MockTimeline
Sourcepub const fn id(&self) -> u64
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.
Sourcepub fn elapsed(&self) -> Duration
pub fn elapsed(&self) -> Duration
Returns elapsed mock time as a standard duration.
§Returns
Elapsed monotonic time since the timeline origin.
Sourcepub fn elapsed_nanos(&self) -> u128
pub fn elapsed_nanos(&self) -> u128
Returns elapsed mock time in nanoseconds.
§Returns
Elapsed monotonic nanoseconds since the timeline origin.
Sourcepub fn now(&self) -> MockInstant
pub fn now(&self) -> MockInstant
Sourcepub fn event_epoch(&self) -> u64
pub fn event_epoch(&self) -> u64
Returns the current event epoch.
§Returns
Epoch incremented by time advances and external notifications.
Sourcepub fn advance(&self, duration: Duration)
pub fn advance(&self, duration: Duration)
Advances mock time and wakes all timeline waiters.
§Parameters
duration: Non-negative duration to add.
Sourcepub fn reset(&self) -> Result<(), MockTimeError>
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.
Sourcepub fn notify_external_change(&self)
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.
Sourcepub fn wait_until(&self, deadline: MockInstant) -> Result<(), MockTimeError>
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.
Sourcepub fn wait_for(&self, duration: Duration)
pub fn wait_for(&self, duration: Duration)
Blocks until duration has elapsed on the mock timeline.
§Parameters
duration: Relative mock duration to wait.
Sourcepub fn wait_for_event_after(&self, observed_epoch: u64)
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.
Sourcepub fn wait_for_blocked_waiters(
&self,
kind: MockWaiterKind,
count: usize,
real_timeout: Duration,
) -> bool
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
impl Clone for MockTimeline
Source§fn clone(&self) -> MockTimeline
fn clone(&self) -> MockTimeline
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more