pub struct MockClock { /* private fields */ }Expand description
Deterministic test clock (REQ-CLK-005).
- Advances only by explicit calls — the OS is never consulted (G3).
monois additive-only and can never go backward.wallis a separate signed field;step_wallmoves it without touchingmono, 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
impl MockClock
Sourcepub fn start_at(mono_ns: u64, wall_ns: u64) -> Self
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).
Sourcepub fn advance_nanos(&self, ns: u64)
pub fn advance_nanos(&self, ns: u64)
Advance mono by ns nanoseconds. Deterministic.
Sourcepub fn step_wall(&self, offset_ns: i64)
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.
Sourcepub fn wall_nanos(&self) -> u64
pub fn wall_nanos(&self) -> u64
Current wall reading, epoch nanos.
Sourcepub fn mono_nanos(&self) -> u64
pub fn mono_nanos(&self) -> u64
Current monotonic reading, nanos.
Trait Implementations§
Source§impl Clock for MockClock
impl Clock for MockClock
Source§fn now(&self) -> Timestamp
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
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
fn source(&self) -> ClockSource
Which underlying source reads are actually served from. REQ-CLK-003.
Auto Trait Implementations§
impl !Freeze for MockClock
impl RefUnwindSafe for MockClock
impl Send for MockClock
impl Sync for MockClock
impl Unpin for MockClock
impl UnsafeUnpin for MockClock
impl UnwindSafe for MockClock
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more