Skip to main content

zenith_session/adapter/
clock.rs

1//! Clock adapter trait and implementations.
2
3use std::time::SystemTime;
4
5/// Abstraction over wall-clock time.
6///
7/// Injected wherever zenith-session needs the current time, so tests can
8/// substitute a fixed value without relying on the real system clock.
9pub trait Clock {
10    fn now(&self) -> SystemTime;
11}
12
13/// Real clock: delegates to [`SystemTime::now`].
14pub struct OsClock;
15
16impl Clock for OsClock {
17    fn now(&self) -> SystemTime {
18        SystemTime::now()
19    }
20}
21
22/// Deterministic test clock: always returns the fixed [`SystemTime`] it was
23/// constructed with.
24pub struct FakeClock(pub SystemTime);
25
26impl Clock for FakeClock {
27    fn now(&self) -> SystemTime {
28        self.0
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn fake_clock_returns_fixed_value() {
38        let fixed = SystemTime::UNIX_EPOCH
39            .checked_add(std::time::Duration::from_secs(1_000_000))
40            .unwrap();
41        let clock = FakeClock(fixed);
42        assert_eq!(clock.now(), fixed);
43        // Calling twice returns the same value.
44        assert_eq!(clock.now(), fixed);
45    }
46}