Skip to main content

MockClock

Struct MockClock 

Source
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

Source

pub fn new() -> Self

Creates a mock clock anchored at the current system time.

§Returns

A mock clock with a fresh zero-elapsed timeline.

Source

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

Creates a mock clock anchored at a specific UTC time.

§Parameters
  • start: UTC time returned while the associated timeline is at its current elapsed value.
§Returns

A mock clock with a fresh timeline.

Source

pub fn with_timeline(start: DateTime<Utc>, timeline: MockTimeline) -> Self

Creates a mock clock backed by an existing timeline.

§Parameters
  • start: UTC time returned for the timeline’s current elapsed value.
  • timeline: Shared mock timeline used by this clock.
§Returns

A mock clock view over the provided timeline.

Source

pub fn timeline(&self) -> MockTimeline

Returns the timeline backing this clock.

§Returns

The shared mock timeline.

Source

pub fn advance(&self, duration: StdDuration)

Advances the shared mock timeline.

§Parameters
  • duration: Duration to add to the shared timeline.
Source

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 Clock for MockClock

Source§

fn millis(&self) -> i64

Returns the current mock UTC timestamp in milliseconds.

Source§

fn time(&self) -> DateTime<Utc>

Returns the current mock UTC time with nanosecond precision.

Source§

impl Clone for MockClock

Source§

fn clone(&self) -> MockClock

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 ControllableClock for MockClock

Source§

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)

Advances the shared mock timeline by a non-negative duration.

§Panics

Panics if duration is negative or cannot be represented as std::time::Duration.

Source§

fn reset(&self)

Resets the shared timeline and this clock’s wall-time anchor.

§Panics

Panics when active timeline waiters prevent reset.

Source§

impl Debug for MockClock

Source§

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

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

impl Default for MockClock

Source§

fn default() -> Self

Creates a mock clock anchored at the current system time.

Source§

impl NanoClock for MockClock

Source§

fn nanos(&self) -> i128

Returns the current mock UTC timestamp in nanoseconds.

Source§

fn time_precise(&self) -> DateTime<Utc>

Returns the current time as a DateTime<Utc> with nanosecond precision. 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.