Skip to main content

Clock

Struct Clock 

Source
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

Source

pub fn new() -> Self

Build a new clock anchored at the current Instant.

Source

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().

Source

pub fn now(&self) -> ClockTime

Current virtual time, as offset from the anchor.

Source

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.

Source

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.

Source

pub fn skew_by(&self, delta_ns: i64)

Skew the clock by delta_ns, which may be negative.

Negative skew simulates clock-step events (e.g. NTP correction, VM pause/resume backward jump). Tests can validate that retry loops with timeout-based escape don’t get stuck on negative elapsed values.

Source

pub fn reset(&self)

Reset the clock to anchor (offset zero).

Trait Implementations§

Source§

impl Clone for Clock

Source§

fn clone(&self) -> Clock

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 Clock

Source§

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

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

impl Default for Clock

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Clock

§

impl RefUnwindSafe for Clock

§

impl Send for Clock

§

impl Sync for Clock

§

impl Unpin for Clock

§

impl UnsafeUnpin for Clock

§

impl UnwindSafe for Clock

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.