use std::os::windows::io::AsRawHandle;
use std::time::{Duration, Instant};
use windows::Win32::Foundation::HANDLE;
use crate::identity::ProcessId;
use crate::wait::test_clock::FrozenClockGuard;
use crate::wait::wait_ms_probe;
use crate::wait::{remaining_override_seam, wait_clamp_seam};
fn spawn_never_exiting() -> (std::process::Child, ProcessId) {
let child = crate::test_spawn::spawn(
std::process::Command::new("cmd")
.args(["/C", "more"])
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null()),
)
.expect("spawn cmd /C more");
let handle = HANDLE(child.as_raw_handle());
let id = crate::identity::windows_identity_from_handle(handle, child.id())
.expect("the owned handle always yields an identity");
(child, id)
}
fn let_child_exit(mut child: std::process::Child) {
drop(child.stdin.take());
child.wait().expect("wait for cmd /C more to exit");
}
#[skuld::test]
fn block_until_exit_arms_the_ceiling_of_the_remaining_duration() {
let (child, id) = spawn_never_exiting();
let (_clock, at) = FrozenClockGuard::install();
wait_ms_probe::take();
let _override = remaining_override_seam::set(Duration::from_micros(500));
let deadline = at + Duration::from_millis(5);
let result = super::block_until_exit(id, Some(Some(deadline)));
let reached = Instant::now() >= deadline;
let arms = wait_ms_probe::take();
let_child_exit(child);
assert!(
!result.expect("a live never-exiting child must not report a wait failure"),
"a never-exiting child is still alive at the deadline"
);
assert!(reached, "the call returned before the real deadline");
let first = arms.first().expect("at least one armed wait");
assert_eq!(first.remaining, Duration::from_micros(500));
assert_eq!(first.ms, 1);
assert_eq!(
first.requested,
Duration::from_millis(5),
"the site must pass the time left to its own deadline (clock frozen at the deadline's origin)"
);
}
#[skuld::test]
fn block_until_exit_never_reports_still_alive_before_the_deadline() {
let (mut child, id) = spawn_never_exiting();
let stdin = child.stdin.take().expect("piped stdin");
wait_ms_probe::take();
let _override = remaining_override_seam::set(Duration::from_micros(500));
wait_ms_probe::on_second_arm(move || drop(stdin)); let deadline = Instant::now() + Duration::from_secs(3600);
let result = super::block_until_exit(id, Some(Some(deadline)));
let arms = wait_ms_probe::take();
let exited = result.expect("a genuinely-terminated child must not report a wait failure");
child.wait().expect("reap the child after it exits");
assert!(exited, "an early WAIT_TIMEOUT was trusted");
assert!(arms.len() >= 2, "expected a re-arm, got {} arm(s)", arms.len());
}
#[skuld::test]
fn block_until_exit_re_arms_past_a_clamped_timeout() {
let (mut child, id) = spawn_never_exiting();
let stdin = child.stdin.take().expect("piped stdin");
wait_ms_probe::take();
let _clamp = wait_clamp_seam::set(5);
wait_ms_probe::on_second_arm(move || drop(stdin));
let deadline = Instant::now() + Duration::from_secs(3600);
let result = super::block_until_exit(id, Some(Some(deadline)));
let arms = wait_ms_probe::take();
let exited = result.expect("a genuinely-terminated child must not report a wait failure");
child.wait().expect("reap the child after it exits");
assert!(exited, "a clamped WAIT_TIMEOUT was trusted");
wait_ms_probe::assert_rearmed_with_fresh_remaining(&arms, 5);
}
#[skuld::test]
fn block_until_exit_terminates_under_a_frozen_clock() {
let (child, id) = spawn_never_exiting();
let (_clock, at) = FrozenClockGuard::install();
let deadline = at + Duration::from_millis(50);
let result = super::block_until_exit(id, Some(Some(deadline)));
let reached = Instant::now() >= deadline;
let_child_exit(child);
assert!(!result.expect("a live never-exiting child must not report a wait failure"));
assert!(reached, "the call returned before the real deadline");
}
#[skuld::test]
#[should_panic(expected = "no progress")]
fn wait_until_panics_when_its_advance_is_dropped() {
use windows::Win32::Foundation::WAIT_TIMEOUT;
let (_clock, at) = FrozenClockGuard::install();
let _skip = crate::wait::test_clock::SkipAdvanceGuard::install();
let deadline = Some(Some(at + Duration::from_secs(3600)));
let mut waits = 0;
crate::wait::wait_until(deadline, |_ms| {
waits += 1;
assert!(waits < 2, "the check let a second round run");
WAIT_TIMEOUT
});
}
#[skuld::test]
fn block_until_exit_or_cancel_arms_the_ceiling_of_the_remaining_duration() {
let (child, id) = spawn_never_exiting();
let cancel = super::new_cancel_event().expect("create cancel event");
let (_clock, at) = FrozenClockGuard::install();
wait_ms_probe::take();
let _override = remaining_override_seam::set(Duration::from_micros(500));
let deadline = at + Duration::from_millis(5);
let result = super::block_until_exit_or_cancel(id, Some(deadline), &cancel);
let reached = Instant::now() >= deadline;
let arms = wait_ms_probe::take();
let_child_exit(child);
assert!(
!result.expect("a live never-exiting child must not report a wait failure"),
"a never-exiting child is still alive when the grace elapses"
);
assert!(reached, "the call returned before the real deadline");
let first = arms.first().expect("at least one armed wait");
assert_eq!(first.remaining, Duration::from_micros(500));
assert_eq!(first.ms, 1);
assert_eq!(
first.requested,
Duration::from_millis(5),
"the site must pass the time left to its own grace (clock frozen at the grace's origin)"
);
}
#[skuld::test]
fn block_until_exit_or_cancel_never_reports_still_alive_before_the_deadline() {
let (mut child, id) = spawn_never_exiting();
let stdin = child.stdin.take().expect("piped stdin");
let cancel = super::new_cancel_event().expect("create cancel event");
wait_ms_probe::take();
let _override = remaining_override_seam::set(Duration::from_micros(500));
wait_ms_probe::on_second_arm(move || drop(stdin));
let result = super::block_until_exit_or_cancel(id, Some(Instant::now() + Duration::from_secs(3600)), &cancel);
let arms = wait_ms_probe::take();
let exited = result.expect("a genuinely-terminated child must not report a wait failure");
child.wait().expect("reap the child after it exits");
assert!(exited, "an early WAIT_TIMEOUT was trusted");
assert!(arms.len() >= 2, "expected a re-arm, got {} arm(s)", arms.len());
}
#[skuld::test]
fn block_until_exit_or_cancel_re_arms_past_a_clamped_timeout() {
let (mut child, id) = spawn_never_exiting();
let stdin = child.stdin.take().expect("piped stdin");
let cancel = super::new_cancel_event().expect("create cancel event");
wait_ms_probe::take();
let _clamp = wait_clamp_seam::set(5);
wait_ms_probe::on_second_arm(move || drop(stdin));
let result = super::block_until_exit_or_cancel(id, Some(Instant::now() + Duration::from_secs(3600)), &cancel);
let arms = wait_ms_probe::take();
let exited = result.expect("a genuinely-terminated child must not report a wait failure");
child.wait().expect("reap the child after it exits");
assert!(exited, "a clamped WAIT_TIMEOUT was trusted");
wait_ms_probe::assert_rearmed_with_fresh_remaining(&arms, 5);
}