pub struct MockClock { /* private fields */ }Expand description
Clock implementation whose readings are derived from a shared mock timeline.
MockClock is a controllable UTC clock for tests. It combines a monotonic
MockTimeline with a wall-clock anchor. The current UTC reading is:
wall_origin + timeline.elapsed()
Creating a clock with at or
with_timeline establishes the wall-clock value for
the timeline’s current elapsed instant. Later calls to
set_time move only that wall-clock anchor;
they do not change elapsed mock time. Calls to advance
or add_duration advance the underlying
monotonic timeline and therefore wake timeline waiters such as mock sleeps.
This clock implements Clock, NanoClock, and ControllableClock.
It is frozen until its timeline advances, which makes elapsed-time tests
deterministic and independent of wall-clock scheduling noise.
A cloned MockClock shares both the timeline and the wall-clock anchor with
the original clock. A clock created with MockClock::with_timeline shares
only the provided timeline; it owns its own wall-clock anchor.
Implementations§
Source§impl MockClock
impl MockClock
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a mock clock anchored at the current system time.
§Returns
A mock clock with a fresh zero-elapsed timeline.
Sourcepub fn with_timeline(start: DateTime<Utc>, timeline: MockTimeline) -> Self
pub fn with_timeline(start: DateTime<Utc>, timeline: MockTimeline) -> Self
Sourcepub fn timeline(&self) -> MockTimeline
pub fn timeline(&self) -> MockTimeline
Sourcepub fn advance(&self, duration: StdDuration)
pub fn advance(&self, duration: StdDuration)
Sourcepub fn try_reset(&self) -> Result<(), MockTimeError>
pub fn try_reset(&self) -> Result<(), MockTimeError>
Resets the shared timeline and this clock’s wall-time anchor.
§Returns
Ok(()) when reset succeeds.
§Errors
Returns MockTimeError::ActiveWaiters when timeline waiters are active.
Trait Implementations§
Source§impl ControllableClock for MockClock
impl ControllableClock for MockClock
Source§fn set_time(&self, instant: DateTime<Utc>)
fn set_time(&self, instant: DateTime<Utc>)
Reanchors this clock so the current timeline instant reads as instant.
Source§fn add_duration(&self, duration: Duration)
fn add_duration(&self, duration: Duration)
Advances the shared mock timeline by a non-negative duration.
§Panics
Panics if duration is negative or cannot be represented as
std::time::Duration.