pub struct MockTime { /* private fields */ }Expand description
A complete mock time runtime sharing one timeline across clocks and sleepers.
MockTime is the recommended entry point for tests that need both “what
time is it?” and “how long should this operation sleep?” to use the same
deterministic time source. It constructs one MockTimeline, then creates
a MockClock and MockSleeper over that
timeline.
Advancing the runtime through advance advances every
component derived from it. This avoids a common testing bug where a mock
clock reports one logical time while a mock sleeper or monitor waits on a
different logical time source.
Wall-clock anchoring is separate from elapsed mock time. Calling
set_time changes the UTC value read at the current
timeline instant, but it does not advance or rewind the timeline. Calling
reset restores both the elapsed timeline and the clock
anchor, and fails if any timeline waiter is active.
§Example
use std::time::Duration;
use qubit_clock::{Clock, MockTime};
let mock = MockTime::unix_epoch();
let clock = mock.clock();
assert_eq!(0, clock.millis());
mock.advance(Duration::from_millis(250));
assert_eq!(250, clock.millis());
assert_eq!(Duration::from_millis(250), mock.elapsed());Implementations§
Source§impl MockTime
impl MockTime
Sourcepub fn unix_epoch() -> Self
pub fn unix_epoch() -> Self
Creates a mock runtime anchored at the Unix epoch.
§Returns
A mock runtime starting at 1970-01-01T00:00:00Z.
Sourcepub fn timeline(&self) -> MockTimeline
pub fn timeline(&self) -> MockTimeline
Sourcepub fn sleeper(&self) -> MockSleeper
pub fn sleeper(&self) -> MockSleeper
Sourcepub fn elapsed(&self) -> StdDuration
pub fn elapsed(&self) -> StdDuration
Sourcepub fn advance(&self, duration: StdDuration)
pub fn advance(&self, duration: StdDuration)
Sourcepub fn set_time(&self, instant: DateTime<Utc>)
pub fn set_time(&self, instant: DateTime<Utc>)
Reanchors the runtime clock at the current timeline instant.
§Parameters
instant: New UTC time returned at the current timeline instant.
Sourcepub fn reset(&self) -> Result<(), MockTimeError>
pub fn reset(&self) -> Result<(), MockTimeError>
Resets the timeline and clock anchor to the runtime’s initial state.
§Returns
Ok(()) when reset succeeds.
§Errors
Returns MockTimeError::ActiveWaiters when timeline waiters are active.