Skip to main content

Module clock

Module clock 

Source
Expand description

Time abstraction for es-entity with support for real and manual time.

This crate provides a unified interface for time operations that works identically whether using real time or manual time for testing.

§Overview

The main type is ClockHandle, a cheap-to-clone handle that provides:

  • now() - Get current time (synchronous, fast)
  • sleep(duration) - Sleep for a duration
  • timeout(duration, future) - Timeout a future

For manual clocks, a ClockController is also provided for controlling time.

§Clock Types

  • Realtime: Uses system clock and tokio timers
  • Manual: Time only advances via explicit advance() calls

§Example

use es_entity::clock::ClockHandle;
use std::time::Duration;

// Production: use real time
let clock = ClockHandle::realtime();

// Testing: use manual clock
let (clock, ctrl) = ClockHandle::manual();

// Same interface regardless of clock type
let now = clock.now();

§Deterministic Testing

In manual mode, time only advances when you call advance(). Wake events are processed in chronological order, so tasks always see the correct time when they wake:

use es_entity::clock::ClockHandle;
use std::time::Duration;

let (clock, ctrl) = ClockHandle::manual();

let clock2 = clock.clone();
tokio::spawn(async move {
    clock2.sleep(Duration::from_secs(3600)).await; // 1 hour
    // When this wakes, clock2.now() == start + 1 hour
    // (even if advance() jumped further)
});

// Advance 1 day - but the task wakes at exactly +1 hour
ctrl.advance(Duration::from_secs(86400)).await;

Structs§

Clock
Global clock access - like Utc::now() but testable.
ClockController
Controller for manual time operations.
ClockHandle
A handle to a clock for getting time and performing time-based operations.
ClockSleep
A future that completes after a duration has elapsed on the clock.
ClockTimeout
A future that completes with a timeout after a duration has elapsed on the clock.
Elapsed
Error returned when a timeout expires.