use std::cell::Cell;
use super::{after_grace, after_tree_grace, GracefulMechanism};
use crate::error::Error;
fn io_error(msg: &str) -> Error {
Error::Io(std::io::Error::other(msg.to_owned()))
}
fn counted(
result: Result<(), Error>,
policy: impl FnOnce(&mut dyn FnMut() -> Result<(), Error>) -> Result<(), Error>,
) -> (Result<(), Error>, u32) {
let calls = Cell::new(0);
let mut result = Some(result);
let out = policy(&mut || {
calls.set(calls.get() + 1);
result.take().expect("called at most once")
});
(out, calls.get())
}
fn message(r: Result<(), Error>) -> String {
r.expect_err("an error").to_string()
}
#[skuld::test]
fn graceful_mechanism_display_is_stable_and_distinct() {
let all = [
(GracefulMechanism::Process, "process"),
(GracefulMechanism::ConsoleGroup, "own console group"),
(
GracefulMechanism::OtherConsoleGroup,
"own console group in another console",
),
(GracefulMechanism::Unknown, "unknown"),
(GracefulMechanism::None, "none"),
];
for (mechanism, expected) in all {
assert_eq!(mechanism.to_string(), expected, "Display for {mechanism:?}");
}
for (i, (_, a)) in all.iter().enumerate() {
for (_, b) in all.iter().skip(i + 1) {
assert_ne!(a, b, "two variants share a Display string");
}
}
}
fn root_is_not_signalled(sig: crate::signal::Sig) -> Result<(), crate::error::Error> {
panic!("the mechanism signalled the root with {sig:?}")
}
#[cfg(unix)]
#[skuld::test]
fn signal_hands_the_process_mechanism_to_the_roots_closure_as_a_term() {
let mut sent = Vec::new();
super::signal(GracefulMechanism::Process, 0x7fff_fffe, |sig| {
sent.push(sig);
Ok(())
})
.expect("the closure's answer is the call's");
assert_eq!(sent, [crate::signal::Sig::Term]);
}
#[cfg(windows)]
#[skuld::test]
fn signal_refuses_the_process_mechanism_without_signalling_the_root() {
let err = super::signal(GracefulMechanism::Process, std::process::id(), root_is_not_signalled)
.expect_err("Windows has no per-process graceful signal");
assert!(matches!(err, crate::error::Error::Unsupported { .. }), "{err:?}");
}
#[skuld::test]
fn signal_refuses_a_child_with_no_mechanism() {
let err = super::signal(GracefulMechanism::None, std::process::id(), root_is_not_signalled)
.expect_err("no group to address");
let crate::error::Error::Unsupported { detail, .. } = &err else {
panic!("expected Unsupported, got {err:?}");
};
assert!(detail.contains("contain"), "the refusal must name the remedy: {detail}");
}
#[cfg(windows)]
#[skuld::test]
fn signal_attempts_a_child_whose_group_may_be_in_another_console() {
let (child, stdin) = crate::test_child::windows_blocker_on_console();
let other = super::signal(GracefulMechanism::OtherConsoleGroup, child.pid(), root_is_not_signalled);
let group = super::signal(GracefulMechanism::ConsoleGroup, child.pid(), root_is_not_signalled);
assert!(
other.is_ok(),
"OtherConsoleGroup must be attempted, not refused: {other:?}"
);
assert_eq!(
other.is_ok(),
group.is_ok(),
"the two console mechanisms must take one path: {other:?} vs {group:?}"
);
_ = child.kill_tree();
drop(stdin);
_ = child.wait();
}
#[cfg(windows)]
#[skuld::test]
fn signal_refuses_a_child_cosca_did_not_create() {
let (child, stdin) = crate::test_child::windows_blocker_on_console();
let mechanism = crate::containment::Attachment::uac_elevated().graceful;
let err = super::signal(mechanism, child.pid(), root_is_not_signalled)
.expect_err("a child cosca did not create must not be signalled");
let crate::error::Error::Unsupported { detail, .. } = &err else {
panic!("expected Unsupported, got {err:?}");
};
assert!(
detail.contains("did not create this child"),
"the refusal must say whose child this is not: {detail}"
);
assert!(detail.contains("kill()"), "the refusal must name the remedy: {detail}");
child.kill_tree().expect("kill_tree");
drop(stdin);
crate::test_child::assert_killed("the blocker", child.wait().expect("reap the blocker"));
}
#[skuld::test]
fn after_grace_is_done_without_a_kill_when_the_process_exited_in_grace() {
let (out, kills) = counted(Ok(()), |k| after_grace(Ok(true), k));
out.expect("exited in grace is done");
assert_eq!(kills, 0);
}
#[skuld::test]
fn after_grace_kills_when_the_grace_ran_out() {
let (out, kills) = counted(Ok(()), |k| after_grace(Ok(false), k));
out.expect("a successful kill is done");
assert_eq!(kills, 1);
}
#[skuld::test]
fn after_grace_kills_then_reports_the_watch_error() {
let (out, kills) = counted(Ok(()), |k| after_grace(Err(io_error("watch")), k));
assert!(message(out).contains("watch"));
assert_eq!(kills, 1, "an unobservable grace escalates");
}
#[skuld::test]
fn after_grace_kill_error_wins_over_the_watch_error() {
let (out, kills) = counted(Err(io_error("kill")), |k| after_grace(Err(io_error("watch")), k));
let msg = message(out);
assert!(msg.contains("kill") && !msg.contains("watch"), "{msg}");
assert_eq!(kills, 1);
}
#[skuld::test]
fn after_tree_grace_sweeps_even_when_the_root_exited() {
let (out, sweeps) = counted(Ok(()), |s| after_tree_grace(Ok(true), s));
out.expect("a successful sweep is done");
assert_eq!(sweeps, 1);
}
#[skuld::test]
fn after_tree_grace_sweep_error_wins_over_the_watch_error() {
let (out, sweeps) = counted(Err(io_error("sweep")), |s| after_tree_grace(Err(io_error("watch")), s));
let msg = message(out);
assert!(msg.contains("sweep") && !msg.contains("watch"), "{msg}");
assert_eq!(sweeps, 1);
}
#[skuld::test]
fn after_tree_grace_reports_the_watch_error_after_the_sweep() {
let (out, sweeps) = counted(Ok(()), |s| after_tree_grace(Err(io_error("watch")), s));
assert!(message(out).contains("watch"));
assert_eq!(sweeps, 1);
}