Skip to main content

Clock

Trait Clock 

Source
pub trait Clock: Send + Sync {
    // Required methods
    fn elapsed(&self) -> Duration;
    fn sleep(
        &self,
        duration: Duration,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + '_>>;
}
Expand description

Monotonic time and sleeping, injectable so a wait can be tested without waiting.

§Why a trait rather than tokio::time::pause

Because the waits here are driven by elapsed comparisons against a deadline and a stall grace, and a test has to be able to say “now 300 seconds have passed” between two polls of a fake control plane. A paused tokio clock can do that, but only for code that sleeps on tokio — and it makes every test in the file share one global clock state. A Clock parameter makes the dependency visible in the signature.

Monotonic rather than wall-clock, and this is the same reasoning the Python client’s injectable time.monotonic carries: the suspended window is a duration, and a wall clock that steps backward would reopen a closed one.

Required Methods§

Source

fn elapsed(&self) -> Duration

Monotonic elapsed time since this clock started. Never decreases.

Source

fn sleep( &self, duration: Duration, ) -> Pin<Box<dyn Future<Output = ()> + Send + '_>>

Sleeps for duration.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§