use std::time::SystemTime;
use crate::simulator::TestSimulator;
#[derive(Clone, Debug)]
pub struct TestWallClock {
simulator: TestSimulator,
epoch: SystemTime,
}
impl TestWallClock {
pub(crate) fn new(simulator: TestSimulator, epoch: SystemTime) -> Self {
Self { simulator, epoch }
}
pub const fn epoch(&self) -> SystemTime {
self.epoch
}
pub fn now(&self) -> Option<SystemTime> {
self.epoch.checked_add(self.simulator.now())
}
}
impl TestSimulator {
pub fn wall_clock(&self, epoch: SystemTime) -> TestWallClock {
TestWallClock::new(self.clone(), epoch)
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use super::*;
#[test]
fn wall_clock_advances_only_with_its_simulator() {
let simulator = TestSimulator::new();
let epoch = SystemTime::UNIX_EPOCH + Duration::from_secs(1_700_000_000);
let clock = simulator.wall_clock(epoch);
assert_eq!(clock.epoch(), epoch);
assert_eq!(clock.now(), Some(epoch));
simulator.advance(Duration::from_millis(250));
assert_eq!(clock.now(), Some(epoch + Duration::from_millis(250)),);
}
}