Skip to main content

MockClock

Struct MockClock 

Source
pub struct MockClock { /* private fields */ }
Expand description

Deterministic test clock (REQ-CLK-005).

  • Advances only by explicit calls — the OS is never consulted (G3).
  • mono is additive-only and can never go backward.
  • wall is a separate signed field; step_wall moves it without touching mono, simulating NTP slews and PTP steps.
  • Shared-handle use is safe: cells are atomic (or critical-section) backed, and single-threaded use — the spec’s determinism contract — is exactly reproducible.

The process-wide monotonic floor is deliberately not applied here (see crate::monotonic): ambient real-clock reads elsewhere in a test binary must not perturb a fiction.

§Example

use clock_kit::{Clock, ClockSource, MockClock};
use std::time::Duration;

let clock = MockClock::start_at(10, 1_000_000);
assert_eq!(clock.now().mono, 10);
clock.advance(Duration::from_nanos(90));
assert_eq!(clock.now().mono, 100);
clock.step_wall(-500_000); // wall steps, mono does not
assert_eq!(clock.now().mono, 100);
assert_eq!(clock.source(), ClockSource::Mock);

Implementations§

Source§

impl MockClock

Source

pub fn start_at(mono_ns: u64, wall_ns: u64) -> Self

A mock starting at mono_ns / wall_ns (both nanos; wall is signed-epoch, mono is process-local scale).

Source

pub fn new() -> Self

A mock starting everything at zero.

Source

pub fn advance(&self, dt: Duration)

Advance mono (and mono only) by dt. Deterministic.

Source

pub fn advance_nanos(&self, ns: u64)

Advance mono by ns nanoseconds. Deterministic.

Source

pub fn step_wall(&self, offset_ns: i64)

Step the wall field by offset_ns (negative = backward, e.g. an NTP correction) without touching mono. REQ-CLK-005 skew tests.

Source

pub fn wall_nanos(&self) -> u64

Current wall reading, epoch nanos.

Source

pub fn mono_nanos(&self) -> u64

Current monotonic reading, nanos.

Trait Implementations§

Source§

impl Clock for MockClock

Source§

fn now(&self) -> Timestamp

Current reading. t.mono never decreases across calls within a process, even if the underlying source regresses. REQ-CLK-001.
Source§

fn quality(&self) -> SyncQuality

Advisory sync quality of the underlying source (G2 — a hint, never a guarantee). REQ-CLK-004.
Source§

fn source(&self) -> ClockSource

Which underlying source reads are actually served from. REQ-CLK-003.
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

Returns the “default value” for a type. 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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.