use std::net::TcpStream;
use std::time::Duration;
use super::fault as term_fault;
use crate::containment::Containment;
use crate::test_child::ctrl_break::{
self, assert_acked, assert_in_our_console, kill_release_reap_async, Grandchild, Handler, STATUS_CONTROL_C_EXIT,
};
use crate::GracefulMechanism;
fn spawn_root(handler: Handler) -> (crate::tokio::Child, TcpStream) {
spawn_root_configured(handler, crate::ContainMode::Strongest, |_| {})
}
fn spawn_root_configured(
handler: Handler,
mode: crate::ContainMode,
configure: impl FnOnce(&mut crate::tokio::Command),
) -> (crate::tokio::Child, TcpStream) {
let (child, sock, _) = spawn(handler, None, mode, configure);
(child, sock)
}
fn spawn_tree(root: Handler, grandchild: Handler) -> (crate::tokio::Child, TcpStream, Grandchild) {
let (child, sock, grand) = spawn(root, Some(grandchild), crate::ContainMode::Strongest, |_| {});
(child, sock, grand.expect("a grandchild was asked for"))
}
fn spawn(
handler: Handler,
grandchild: Option<Handler>,
mode: crate::ContainMode,
configure: impl FnOnce(&mut crate::tokio::Command),
) -> (crate::tokio::Child, TcpStream, Option<Grandchild>) {
ctrl_break::spawn_member_async(handler, grandchild, mode, configure)
}
#[skuld::test]
async fn async_child_terminate_delivers_ctrl_break_to_a_contained_root() {
let _warns = crate::log_capture::warn_watch();
crate::tokio::test_runtime::assert_current_thread();
let (mut child, mut sock) = spawn_root(Handler::Ack);
assert_eq!(child.graceful_mechanism(), GracefulMechanism::ConsoleGroup);
assert_in_our_console(child.pid(), "the root");
child.terminate().expect("terminate a console-group child");
ctrl_break::assert_break_sent_to(child.pid());
assert_acked(&mut sock, "the root");
assert_eq!(
child.is_alive(),
crate::identity::Liveness::Alive,
"the root handles the break and keeps running"
);
kill_release_reap_async(&mut child, sock, false).await;
}
#[skuld::test]
async fn an_async_plain_contained_root_really_receives_the_break() {
crate::tokio::test_runtime::assert_current_thread();
let (mut child, mut sock) = spawn_root(Handler::Ack);
assert_eq!(child.containment(), Containment::JobObject);
assert_eq!(child.graceful_mechanism(), GracefulMechanism::ConsoleGroup);
assert_in_our_console(child.pid(), "the root");
child.terminate_tree().expect("terminate_tree on a contained root");
ctrl_break::assert_break_sent_to(child.pid());
assert_acked(&mut sock, "the root");
kill_release_reap_async(&mut child, sock, true).await;
}
#[skuld::test]
async fn async_child_graceful_shutdown_exits_via_ctrl_break_on_windows() {
let _warns = crate::log_capture::warn_watch();
crate::tokio::test_runtime::assert_current_thread();
let (mut child, _sock) = spawn_root(Handler::Default);
let pid = child.pid();
assert_in_our_console(pid, "the root");
let check = term_fault::at_checked(term_fault::HookPoint::AfterTerminate, move || {
ctrl_break::assert_break_sent_to(pid)
});
let status = child
.graceful_shutdown(Duration::from_secs(30))
.await
.expect("graceful_shutdown on a console-group child");
check.assert_fired();
assert_eq!(
status.code(),
Some(STATUS_CONTROL_C_EXIT),
"the child must die to the console event, got {status:?}"
);
}
#[skuld::test]
async fn async_child_graceful_shutdown_tree_tears_down_tree_on_windows() {
let _warns = crate::log_capture::warn_watch();
crate::tokio::test_runtime::assert_current_thread();
let (mut child, _root_sock, grand) = spawn_tree(Handler::Default, Handler::Default);
let pid = child.pid();
assert_in_our_console(pid, "the root");
let check = term_fault::at_checked(term_fault::HookPoint::AfterTerminate, move || {
ctrl_break::assert_break_sent_to(pid)
});
let status = child
.graceful_shutdown_tree(Duration::from_secs(30))
.await
.expect("graceful_shutdown_tree on a console-group root");
check.assert_fired();
assert_eq!(
status.code(),
Some(STATUS_CONTROL_C_EXIT),
"the root must die to the break, got {status:?}"
);
assert_eq!(
grand.exit.wait_for_exit_code(),
STATUS_CONTROL_C_EXIT,
"the grandchild must die to the break"
);
}
#[skuld::test]
async fn async_child_graceful_shutdown_signals_the_group_but_only_kills_the_child() {
let _warns = crate::log_capture::warn_watch();
crate::tokio::test_runtime::assert_current_thread();
let (mut child, root_sock, mut grand) = spawn_tree(Handler::Ignore, Handler::Ack);
assert_in_our_console(child.pid(), "the root");
let _release = term_fault::release_at(term_fault::HookPoint::BeforeReap, root_sock);
let status = child
.graceful_shutdown(Duration::ZERO)
.await
.expect("graceful_shutdown on a console-group root");
assert_eq!(
status.code(),
Some(1),
"the root ignores the break, so only the escalation can have ended it, got {status:?}"
);
assert_eq!(
child.is_alive(),
crate::identity::Liveness::Dead,
"the escalation really ended the root"
);
ctrl_break::assert_break_sent_to(child.pid());
assert_acked(&mut grand.sock, "the grandchild");
assert_eq!(
child.wait_tree_timeout(Duration::ZERO).await.expect("drain verdict"),
crate::containment::TreeDrain::MembersRemain,
"the escalation must not have reached the group"
);
child.kill_tree().expect("kill_tree");
let Grandchild { sock, exit } = grand;
drop(sock);
assert_eq!(exit.wait_for_exit_code(), 1, "kill_tree must kill the grandchild");
}
#[skuld::test]
async fn async_graceful_shutdown_tree_sweep_is_load_bearing_on_windows() {
let _warns = crate::log_capture::warn_watch();
crate::tokio::test_runtime::assert_current_thread();
let (mut child, root_sock, grand) = spawn_tree(Handler::Ignore, Handler::Ignore);
assert_in_our_console(child.pid(), "the root");
let _release = term_fault::release_at(term_fault::HookPoint::BeforeReap, root_sock);
let status = child
.graceful_shutdown_tree(Duration::ZERO)
.await
.expect("tree escalates");
assert_eq!(status.code(), Some(1), "the sweep ends the root, got {status:?}");
assert_eq!(
child.is_alive(),
crate::identity::Liveness::Dead,
"the sweep really ended the root"
);
let Grandchild { sock, exit } = grand;
drop(sock);
assert_eq!(exit.wait_for_exit_code(), 1, "the sweep must kill the grandchild");
}
fn assert_own_console_route(child: &crate::tokio::Child, expected_containment: Containment) {
assert_eq!(
child.containment(),
expected_containment,
"this test is about the wrong thing if containment did not take"
);
assert_eq!(
child.graceful_mechanism(),
GracefulMechanism::OtherConsoleGroup,
"cosca's honest report of the route"
);
}
async fn assert_false_success(
expected_containment: Containment,
mode: crate::ContainMode,
configure: impl Fn(&mut crate::tokio::Command),
) {
use std::io::Write as _;
crate::tokio::test_runtime::assert_current_thread();
let (mut child, mut sock) = spawn_root_configured(Handler::Default, mode, &configure);
assert_own_console_route(&child, expected_containment);
child.terminate_tree().expect("the cooperative op reports success");
ctrl_break::assert_break_sent_to(child.pid());
assert_eq!(
child.is_alive(),
crate::identity::Liveness::Alive,
"nothing was delivered, so the root must still be running"
);
sock.write_all(b"x").expect("release the root through its own socket");
let status = child.wait().await.expect("reap");
assert_eq!(
status.code(),
Some(0),
"the root exited on OUR release byte, so the cooperative op delivered nothing, got {status:?}"
);
drop(sock);
let (mut child, sock) = spawn_root_configured(Handler::Default, mode, &configure);
assert_own_console_route(&child, expected_containment);
let _release = term_fault::release_at(term_fault::HookPoint::BeforeReap, sock);
let status = child
.graceful_shutdown_tree(Duration::ZERO)
.await
.expect("the forced half tears the tree down");
assert_eq!(
status.code(),
Some(1),
"the forced half must kill the root, got {status:?}"
);
assert_eq!(child.is_alive(), crate::identity::Liveness::Dead);
}
#[skuld::test]
async fn an_async_no_window_contained_root_gets_a_false_success() {
assert_false_success(Containment::JobObject, crate::ContainMode::Strongest, |c| {
c.no_window();
})
.await;
}
#[skuld::test]
async fn an_async_detached_contained_root_gets_a_false_success() {
assert_false_success(Containment::JobObject, crate::ContainMode::Strongest, |c| {
c.detached();
})
.await;
}
#[skuld::test]
async fn an_async_treewalk_contained_root_gets_a_false_success_too() {
assert_false_success(Containment::TreeWalk, crate::ContainMode::TreeWalk, |c| {
c.no_window();
})
.await;
}
#[skuld::test]
async fn async_child_graceful_ops_report_success_for_a_child_that_shares_no_console() {
let _warns = crate::log_capture::warn_watch();
crate::tokio::test_runtime::assert_current_thread();
let (mut child, sock) = ctrl_break::spawn_consoleless_root_async();
let pid = child.pid();
assert_eq!(
child.graceful_mechanism(),
GracefulMechanism::ConsoleGroup,
"the flags do not exclude delivery: that is exactly what makes this a gap"
);
child.terminate().expect("the documented false success");
ctrl_break::assert_break_sent_to(pid);
assert_eq!(
child.is_alive(),
crate::identity::Liveness::Alive,
"nothing was delivered, so the child must still be running"
);
let check = term_fault::at_checked(term_fault::HookPoint::AfterTerminate, move || {
ctrl_break::assert_break_sent_to(pid)
});
let _release = term_fault::release_at(term_fault::HookPoint::BeforeReap, sock);
let status = child
.graceful_shutdown(Duration::ZERO)
.await
.expect("the forced half needs no console");
check.assert_fired();
assert_eq!(status.code(), Some(1), "escalation's kill, got {status:?}");
}