use std::time::Duration;
fn expected_drained_verdict(containment: crate::containment::Containment) -> crate::containment::TreeDrain {
match containment {
crate::containment::Containment::FdMarker => crate::containment::TreeDrain::AllMarkersClosed,
_ => crate::containment::TreeDrain::AllMembersExited,
}
}
fn quick_contained_child() -> crate::Child {
let mut cmd = crate::Command::new();
#[cfg(unix)]
cmd.args(["true"]);
#[cfg(windows)]
cmd.args(["cmd", "/C", "exit 0"]);
cmd.contain();
cmd.spawn().expect("spawn")
}
fn long_lived_contained_child() -> (crate::Child, std::io::PipeWriter) {
crate::test_child::held_contained_blocker(crate::Stdio::null())
}
#[skuld::test]
fn wait_tree_reports_the_drained_verdict_when_the_tree_drains() {
let child = quick_contained_child();
let containment = child.containment();
let drainable = containment.can_observe_drain();
let result = child.wait_tree();
if drainable {
assert_eq!(
result.expect("a fully-exited tree must report a drained verdict"),
expected_drained_verdict(containment)
);
} else {
let err = result.expect_err("a non-drainable mechanism must refuse wait_tree");
assert!(matches!(err, crate::error::Error::Unsupported { .. }), "got {err:?}");
}
_ = child.wait();
}
#[skuld::test]
fn wait_tree_timeout_reports_members_remain_before_the_deadline() {
let (child, _stdin) = long_lived_contained_child();
let drainable = child.containment().can_observe_drain();
let result = child.wait_tree_timeout(Duration::from_millis(200));
if drainable {
assert_eq!(
result.expect("an unmet deadline on a live tree must not be an error"),
crate::containment::TreeDrain::MembersRemain
);
} else {
let err = result.expect_err("a non-drainable mechanism must refuse wait_tree_timeout");
assert!(matches!(err, crate::error::Error::Unsupported { .. }), "got {err:?}");
}
_ = child.kill_tree();
_ = child.wait();
}
#[skuld::test]
fn wait_tree_timeout_zero_reports_members_remain_on_a_live_tree() {
let (child, _stdin) = long_lived_contained_child();
let drainable = child.containment().can_observe_drain();
let result = child.wait_tree_timeout(Duration::ZERO);
if drainable {
assert_eq!(
result.expect("a ZERO probe against a live tree must not be an error"),
crate::containment::TreeDrain::MembersRemain
);
} else {
let err = result.expect_err("a non-drainable mechanism must refuse wait_tree_timeout");
assert!(matches!(err, crate::error::Error::Unsupported { .. }), "got {err:?}");
}
_ = child.kill_tree();
_ = child.wait();
}
#[skuld::test]
fn wait_tree_timeout_zero_reports_the_drained_verdict_after_the_tree_has_already_drained() {
let child = quick_contained_child();
let containment = child.containment();
let drainable = containment.can_observe_drain();
let first = child.wait_tree();
if drainable {
first.expect("a fully-exited tree must report a drained verdict");
assert_eq!(
child
.wait_tree_timeout(Duration::ZERO)
.expect("a ZERO probe against an already-drained tree must not be an error"),
expected_drained_verdict(containment),
"a ZERO probe must observe the SAME drained state an unbounded wait already found, \
not report MembersRemain just because the deadline is already past"
);
} else {
first.expect_err("a non-drainable mechanism must refuse wait_tree");
let err = child.wait_tree_timeout(Duration::ZERO);
assert!(
matches!(err, Err(crate::error::Error::Unsupported { .. })),
"got {err:?}"
);
}
_ = child.wait();
}
#[skuld::test]
fn wait_tree_is_unsupported_on_a_non_drainable_mechanism() {
let mut cmd = crate::Command::new();
#[cfg(unix)]
cmd.args(["true"]);
#[cfg(windows)]
cmd.args(["cmd", "/C", "exit 0"]);
cmd.contain_with(crate::ContainMode::TreeWalk);
let treewalk_child = cmd.spawn().expect("spawn");
let containment = treewalk_child.containment();
let drainable = containment.can_observe_drain();
let err = treewalk_child.wait_tree_timeout(Duration::from_millis(200));
if drainable {
assert_eq!(
err.expect("macOS promotes an explicit TreeWalk request to the drainable fd marker"),
expected_drained_verdict(containment),
"a quick-exiting tree must still be observed as drained through the promoted mechanism"
);
} else {
let err = err.expect_err("TreeWalk, honored as requested, has no kernel drain edge");
assert!(matches!(err, crate::error::Error::Unsupported { .. }), "got {err:?}");
let err2 = treewalk_child
.wait_tree()
.expect_err("TreeWalk, honored as requested, has no kernel drain edge");
assert!(matches!(err2, crate::error::Error::Unsupported { .. }), "got {err2:?}");
}
_ = treewalk_child.wait();
}
use std::time::Instant;
fn spawn_never_exiting() -> (crate::Child, std::io::PipeWriter) {
let mut cmd = crate::Command::new();
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(crate::Stdio::pipe_in()).expect("configure piped stdin");
cmd.stdout(crate::Stdio::null()).expect("configure null stdout");
let mut child = cmd.spawn().expect("spawn");
let stdin = child.stdin().expect("piped stdin was just configured above");
(child, stdin)
}
#[skuld::test]
fn wait_deadline_in_the_past_reports_an_exited_child() {
let child = quick_contained_child();
let waited = child.wait().expect("wait");
let status = child
.wait_deadline(Instant::now())
.expect("an exited child must not fail");
assert_eq!(status, Some(waited));
}
fn assert_consecutive_frozen_waits_never_early(child: &crate::Child) {
let (_clock, at) = crate::wait::test_clock::FrozenClockGuard::install();
let first = at + Duration::from_millis(50);
let second = at + Duration::from_millis(100);
assert!(child.wait_deadline(first).expect("first wait").is_none());
assert!(child.wait_deadline(second).expect("second wait").is_none());
assert!(Instant::now() >= second, "the second wait returned before its deadline");
}
#[skuld::test]
fn std_child_consecutive_frozen_waits_never_return_early() {
let (child, _stdin) = spawn_never_exiting();
assert_consecutive_frozen_waits_never_early(&child);
}
#[cfg(windows)]
#[skuld::test]
fn raw_child_consecutive_frozen_waits_never_return_early() {
let mut cmd = crate::Command::new();
let findstr = std::path::Path::new(&std::env::var_os("SystemRoot").expect("SystemRoot"))
.join("System32")
.join("findstr.exe");
cmd.executable(findstr).args(["findstr", "x"]);
cmd.stdin(crate::Stdio::pipe_in()).expect("configure piped stdin");
cmd.stdout(crate::Stdio::null()).expect("configure null stdout");
let mut child = cmd.spawn().expect("spawn");
let _stdin = child.stdin().expect("piped stdin");
assert!(
matches!(child.proc, crate::child::proc_handle::ProcHandle::Raw(_)),
"an explicit executable() must route to the raw backend"
);
assert_consecutive_frozen_waits_never_early(&child);
}