Skip to main content

MockTime

Struct MockTime 

Source
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

Source

pub fn at(start: DateTime<Utc>) -> Self

Creates a mock runtime anchored at a UTC time.

§Parameters
  • start: UTC reading returned before the timeline advances.
§Returns

A mock runtime with a clock and sleeper sharing one timeline.

Source

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.

Source

pub fn timeline(&self) -> MockTimeline

Returns the shared timeline.

§Returns

The timeline that drives all components in this runtime.

Source

pub fn clock(&self) -> MockClock

Returns a clock view over the shared timeline.

§Returns

Cloneable mock clock.

Source

pub fn sleeper(&self) -> MockSleeper

Returns a sleeper view over the shared timeline.

§Returns

Cloneable mock sleeper.

Source

pub fn elapsed(&self) -> StdDuration

Returns elapsed time on the shared timeline.

§Returns

Elapsed mock time.

Source

pub fn advance(&self, duration: StdDuration)

Advances the shared timeline.

§Parameters
  • duration: Duration to add to mock elapsed time.
Source

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.
Source

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.

Trait Implementations§

Source§

impl Clone for MockTime

Source§

fn clone(&self) -> MockTime

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 MockTime

Source§

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

Formats the value using the given formatter. Read more

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.