diff options
Diffstat (limited to 'src/time.rs')
| -rw-r--r-- | src/time.rs | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/src/time.rs b/src/time.rs index 8460f20..1ffc116 100644 --- a/src/time.rs +++ b/src/time.rs @@ -1,6 +1,13 @@ +use std::sync::atomic::{AtomicBool, Ordering}; +use std::time::Duration; + use chrono::{DateTime, SecondsFormat, TimeZone, Utc}; pub fn now() -> String { + #[cfg(feature = "test-support")] + if let Some(t) = clock::now() { + return fmt(t); + } match pinned() { Some(pinned) => pinned, None => fmt(Utc::now()), @@ -8,6 +15,10 @@ pub fn now() -> String { } fn base() -> DateTime<Utc> { + #[cfg(feature = "test-support")] + if let Some(t) = clock::now() { + return t; + } match pinned().and_then(|p| parse(&p)) { Some(t) => t, None => Utc::now(), @@ -59,3 +70,141 @@ pub fn secs_ago(secs: i64) -> String { pub fn secs_ahead(secs: i64) -> String { fmt(base() + chrono::Duration::seconds(secs)) } + +// ---- waiting ---- + +/// Wait for `d`, checking `stop` every `slice`. Returns false if `stop` was set +/// before the wait finished, so a control loop can leave instead of doing one +/// more pass. +pub fn wait(d: Duration, slice: Duration, stop: &AtomicBool) -> bool { + if clock::advance(d) { + return !stop.load(Ordering::SeqCst); + } + let mut left = d; + while !left.is_zero() { + if stop.load(Ordering::SeqCst) { + return false; + } + let step = slice.min(left); + std::thread::sleep(step); + left -= step; + } + true +} + +pub fn sleep(d: Duration) { + if clock::advance(d) { + return; + } + std::thread::sleep(d); +} + +/// Wrap a closure about to be spawned so the child thread inherits this one's +/// clock setting. Without it a control loop spawned by a test runs on wall time +/// while the test itself runs on the virtual clock, and the two disagree. +pub fn inherit<F, T>(f: F) -> impl FnOnce() -> T +where + F: FnOnce() -> T + Send + 'static, + T: Send + 'static, +{ + clock::wrap(f) +} + +#[cfg(feature = "test-support")] +pub fn virtualize() -> clock::VirtualClock { + clock::virtualize() +} + +#[cfg(feature = "test-support")] +pub fn virtual_now() -> Option<DateTime<Utc>> { + clock::now() +} + +#[cfg(feature = "test-support")] +mod clock { + use super::*; + use std::cell::Cell; + use std::sync::{Mutex, MutexGuard}; + + thread_local! { + static ON: Cell<bool> = const { Cell::new(false) }; + } + + static AT: Mutex<Option<DateTime<Utc>>> = Mutex::new(None); + static OWNER: Mutex<()> = Mutex::new(()); + + pub struct VirtualClock { + _owner: MutexGuard<'static, ()>, + } + + pub fn virtualize() -> VirtualClock { + let owner = OWNER.lock().unwrap_or_else(|e| e.into_inner()); + *lock(&AT) = Some(Utc::now()); + ON.set(true); + VirtualClock { _owner: owner } + } + + impl Drop for VirtualClock { + fn drop(&mut self) { + ON.set(false); + *lock(&AT) = None; + } + } + + fn lock<T>(m: &Mutex<T>) -> std::sync::MutexGuard<'_, T> { + m.lock().unwrap_or_else(|e| e.into_inner()) + } + + pub fn now() -> Option<DateTime<Utc>> { + if !ON.get() { + return None; + } + *lock(&AT) + } + + /// Returns true when the wait was virtual: the clock moved, no wall time did. + pub fn advance(d: Duration) -> bool { + if !ON.get() { + return false; + } + let mut at = lock(&AT); + if let Some(t) = *at { + let step = chrono::TimeDelta::from_std(d).unwrap_or_else(|_| chrono::TimeDelta::zero()); + *at = Some(t + step); + } + // A control loop that never blocks still needs to let its caller run. + std::thread::yield_now(); + true + } + + pub fn wrap<F, T>(f: F) -> impl FnOnce() -> T + where + F: FnOnce() -> T + Send + 'static, + T: Send + 'static, + { + let on = ON.get(); + move || { + ON.set(on); + f() + } + } +} + +#[cfg(not(feature = "test-support"))] +mod clock { + use super::*; + + #[inline(always)] + pub fn advance(_d: Duration) -> bool { + false + } + + #[inline(always)] + pub fn wrap<F, T>(f: F) -> F + where + F: FnOnce() -> T + Send + 'static, + T: Send + 'static, + { + f + } +} |
