use super::*;
use std::time::Instant;
#[test]
fn test9988_a_nudge_before_the_wait_is_not_lost() {
let nudge = Nudge::default();
nudge.wake();
let started = Instant::now();
nudge.wait(Duration::from_secs(30));
assert!(
started.elapsed() < Duration::from_secs(5),
"a nudge raised before the wait was lost: waited {:?}",
started.elapsed()
);
let started = Instant::now();
nudge.wait(Duration::from_millis(300));
assert!(
started.elapsed() >= Duration::from_millis(250),
"a spent nudge woke a second wait"
);
}
#[test]
fn test9989_the_wait_ends_without_any_nudge() {
let nudge = Nudge::default();
let started = Instant::now();
nudge.wait(Duration::from_millis(200));
let waited = started.elapsed();
assert!(waited >= Duration::from_millis(150), "returned too early: {waited:?}");
assert!(waited < Duration::from_secs(5), "did not return: {waited:?}");
}
#[test]
fn test9990_a_wake_ends_a_wait_in_progress() {
let nudge = Arc::new(Nudge::default());
let waker = nudge.clone();
std::thread::spawn(move || {
std::thread::sleep(Duration::from_millis(100));
waker.wake();
});
let started = Instant::now();
nudge.wait(Duration::from_secs(30));
assert!(
started.elapsed() < Duration::from_secs(5),
"the wait ignored a wake and ran to its timeout"
);
}