use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;
use tokio::time::Instant;
#[derive(Clone)]
pub(crate) struct Progress {
start: Instant,
last_ms: Arc<AtomicU64>,
}
impl Progress {
pub(crate) fn new() -> Self {
Self {
start: Instant::now(),
last_ms: Arc::new(AtomicU64::new(0)),
}
}
pub(crate) fn touch(&self) {
let ms = self.start.elapsed().as_millis() as u64;
self.last_ms.store(ms, Ordering::Relaxed);
}
pub(crate) fn idle_for(&self) -> Duration {
let last = Duration::from_millis(self.last_ms.load(Ordering::Relaxed));
self.start.elapsed().saturating_sub(last)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test(start_paused = true)]
async fn a_touch_is_seen_by_every_clone() {
let progress = Progress::new();
let other = progress.clone();
tokio::time::advance(Duration::from_secs(5)).await;
other.touch();
assert_eq!(progress.last_ms.load(Ordering::Relaxed), 5_000);
}
#[tokio::test(start_paused = true)]
async fn idle_for_counts_from_the_last_touch() {
let progress = Progress::new();
tokio::time::advance(Duration::from_secs(5)).await;
assert_eq!(progress.idle_for(), Duration::from_secs(5));
progress.touch();
tokio::time::advance(Duration::from_secs(2)).await;
assert_eq!(progress.idle_for(), Duration::from_secs(2));
}
}