diff options
Diffstat (limited to 'src/time/clock.rs')
-rw-r--r-- | src/time/clock.rs | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/src/time/clock.rs b/src/time/clock.rs index fab7eca..a62fbe3 100644 --- a/src/time/clock.rs +++ b/src/time/clock.rs @@ -17,7 +17,7 @@ cfg_not_test_util! { } impl Clock { - pub(crate) fn new() -> Clock { + pub(crate) fn new(_enable_pausing: bool) -> Clock { Clock {} } @@ -59,6 +59,9 @@ cfg_test_util! { #[derive(Debug)] struct Inner { + /// True if the ability to pause time is enabled. + enable_pausing: bool, + /// Instant to use as the clock's base instant. base: std::time::Instant, @@ -69,14 +72,18 @@ cfg_test_util! { /// Pause time /// /// The current value of `Instant::now()` is saved and all subsequent calls - /// to `Instant::now()` until the timer wheel is checked again will return the saved value. - /// Once the timer wheel is checked, time will immediately advance to the next registered - /// `Sleep`. This is useful for running tests that depend on time. + /// to `Instant::now()` until the timer wheel is checked again will return + /// the saved value. Once the timer wheel is checked, time will immediately + /// advance to the next registered `Sleep`. This is useful for running tests + /// that depend on time. + /// + /// Pausing time requires the `current_thread` Tokio runtime. This is the + /// default runtime used by `#[tokio::test]` /// /// # Panics /// - /// Panics if time is already frozen or if called from outside of the Tokio - /// runtime. + /// Panics if time is already frozen or if called from outside of a + /// `current_thread` Tokio runtime. pub fn pause() { let clock = clock().expect("time cannot be frozen from outside the Tokio runtime"); clock.pause(); @@ -142,11 +149,12 @@ cfg_test_util! { impl Clock { /// Return a new `Clock` instance that uses the current execution context's /// source of time. - pub(crate) fn new() -> Clock { + pub(crate) fn new(enable_pausing: bool) -> Clock { let now = std::time::Instant::now(); Clock { inner: Arc::new(Mutex::new(Inner { + enable_pausing, base: now, unfrozen: Some(now), })), @@ -156,6 +164,12 @@ cfg_test_util! { pub(crate) fn pause(&self) { let mut inner = self.inner.lock().unwrap(); + if !inner.enable_pausing { + drop(inner); // avoid poisoning the lock + panic!("`time::pause()` requires the `current_thread` Tokio runtime. \ + This is the default Runtime used by `#[tokio::test]."); + } + let elapsed = inner.unfrozen.as_ref().expect("time is already frozen").elapsed(); inner.base += elapsed; inner.unfrozen = None; |