pub struct Clock { /* private fields */ }Expand description
Deterministic, in-process clock for chaos testing.
Clock does not advance automatically. Callers explicitly
move the clock forward via advance and skew
it via skew_by. This makes time-sensitive
tests fully reproducible.
Clock is Clone-able and shares state with its clones via an
internal Arc<AtomicI64> of nanoseconds-since-anchor; advancing
or skewing one handle advances all. Safe to share across threads.
§Example
use dev_chaos::clock::Clock;
use std::time::Duration;
let c = Clock::new();
let t0 = c.now();
c.advance(Duration::from_secs(5));
let t1 = c.now();
assert_eq!(t1.since_anchor() - t0.since_anchor(), Duration::from_secs(5));
// Skew negative to simulate clock going backward (e.g. NTP step).
c.skew_by(-(Duration::from_secs(2).as_nanos() as i64));
let t2 = c.now();
assert!(t2 < t1);Implementations§
Source§impl Clock
impl Clock
Sourcepub fn anchor(&self) -> Instant
pub fn anchor(&self) -> Instant
The real Instant this clock was anchored at.
Callers that need to interoperate with code expecting Instant
can compute clock.anchor() + clock.now().since_anchor().
Sourcepub fn now_signed_ns(&self) -> i64
pub fn now_signed_ns(&self) -> i64
Current virtual offset, signed (in nanoseconds from anchor).
Useful when validating skew-backward scenarios where
since_anchor() would clamp.
Sourcepub fn advance(&self, delta: Duration)
pub fn advance(&self, delta: Duration)
Advance the clock by a non-negative delta.
Equivalent to skew_by(delta.as_nanos() as i64) but rejects
negative durations at the type level.