use chrono::{DateTime, Utc};
use std::{sync::Arc, time::Duration};
use super::artificial::ArtificialClock;
#[derive(Clone)]
pub struct ClockController {
pub(crate) clock: Arc<ArtificialClock>,
}
impl ClockController {
pub async fn advance(&self, duration: Duration) -> usize {
self.clock.advance(duration).await
}
pub async fn advance_to_next_wake(&self) -> Option<DateTime<Utc>> {
self.clock.advance_to_next_wake().await
}
pub fn set_time(&self, time: DateTime<Utc>) {
self.clock.set_time(time);
self.clock.wake_tasks_at(time.timestamp_millis());
}
pub fn pending_wake_count(&self) -> usize {
self.clock.pending_wake_count()
}
pub fn now(&self) -> DateTime<Utc> {
self.clock.now()
}
pub fn transition_to_realtime(&self) {
self.clock.transition_to_realtime();
}
pub fn is_realtime(&self) -> bool {
self.clock.is_realtime()
}
pub fn clear_pending_wakes(&self) {
self.clock.clear_pending_wakes();
}
pub fn reset_to(&self, time: DateTime<Utc>) {
self.clock.set_time(time);
self.clock.clear_pending_wakes();
}
}
impl std::fmt::Debug for ClockController {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ClockController")
.field("now", &self.clock.now())
.field("pending_wakes", &self.clock.pending_wake_count())
.finish()
}
}