use std::time::Duration;
use super::*;
const PEND_WINDOW: Duration = Duration::from_millis(150);
#[tokio::test]
async fn pends_while_no_source_has_fired() {
let latch = Latch::new();
let result = tokio::time::timeout(PEND_WINDOW, shutdown_signal_with(&latch)).await;
assert!(
result.is_err(),
"shutdown_signal must not resolve before a shutdown source fires"
);
}
#[tokio::test]
async fn resolves_when_the_latch_was_triggered_first() {
let latch = Latch::new();
latch.trigger();
tokio::time::timeout(Duration::from_secs(5), shutdown_signal_with(&latch))
.await
.expect("an already-triggered latch must resolve the shutdown future");
}
#[tokio::test]
async fn resolves_on_a_later_trigger() {
let latch = Latch::new();
let waiter = {
let latch = latch.clone();
tokio::spawn(async move { shutdown_signal_with(&latch).await })
};
tokio::time::sleep(Duration::from_millis(20)).await;
latch.trigger();
tokio::time::timeout(Duration::from_secs(5), waiter)
.await
.expect("a trigger must resolve an already-installed shutdown future")
.expect("shutdown task must not panic");
}
#[tokio::test]
async fn every_listener_future_resolves_from_one_trigger() {
let latch = Latch::new();
let futures: Vec<_> = (0..2)
.map(|_| {
let latch = latch.clone();
tokio::spawn(async move { shutdown_signal_with(&latch).await })
})
.collect();
tokio::time::sleep(Duration::from_millis(20)).await;
latch.trigger();
for f in futures {
tokio::time::timeout(Duration::from_secs(5), f)
.await
.expect("each listener's shutdown future must resolve")
.expect("shutdown task must not panic");
}
}
#[tokio::test]
async fn the_process_wide_helpers_drive_the_process_wide_latch() {
request_shutdown();
assert!(shutdown_requested());
tokio::time::timeout(Duration::from_secs(5), shutdown_signal())
.await
.expect("request_shutdown must resolve the process-wide shutdown future");
}
#[tokio::test]
async fn readiness_is_latched_by_mark_serving() {
mark_serving();
assert!(is_serving());
tokio::time::timeout(Duration::from_secs(5), wait_until_serving())
.await
.expect("wait_until_serving must resolve once a listener is bound");
}