use std::time::Duration;
use super::ctrl_break_tests::{Call, CALLS};
use crate::stop::proxy::protocol::{CtrlEvent, ProxyAnswer};
use crate::test_child::ctrl_break::{
assert_acked_event, assert_proxy_answered, kill_release_reap_async, spawn_member_async, Handler,
STATUS_CONTROL_C_EXIT,
};
use crate::test_groups::{ctrl_proxy, Group};
use crate::tokio::test_runtime::assert_current_thread;
use crate::ContainMode;
async fn ending(call: Call, child: &mut crate::tokio::Child) -> std::process::ExitStatus {
let pid = child.pid();
let answered = move || assert_proxy_answered(pid, CtrlEvent::CtrlBreak, ProxyAnswer::Sent);
if call.escalates() {
let check = crate::graceful_hooks::at_checked(crate::graceful_hooks::HookPoint::AfterTerminate, answered);
let status = call.run(child, Duration::from_secs(30)).await;
check.assert_fired();
status
.unwrap_or_else(|e| panic!("{call:?}: {e:?}"))
.expect("a graceful call answers a status")
} else {
call.run(child, Duration::ZERO)
.await
.unwrap_or_else(|e| panic!("{call:?}: {e:?}"));
answered();
child.wait().await.expect("reap the root")
}
}
async fn assert_default_stops_through_the_proxy(mode: ContainMode) {
assert_current_thread();
for call in CALLS {
let (mut child, _sock, _) =
spawn_member_async(Handler::Default, None, Some(mode), |cmd| _ = cmd.no_window()).await;
let status = ending(call, &mut child).await;
assert_eq!(
status.code(),
Some(STATUS_CONTROL_C_EXIT),
"{mode:?} {call:?}: the root must die to the break, got {status:?}"
);
}
}
#[skuld::test]
async fn an_async_no_window_contained_root_is_stopped_through_the_proxy(#[fixture(ctrl_proxy)] _group: &Group) {
assert_default_stops_through_the_proxy(ContainMode::Strongest).await;
}
#[skuld::test]
async fn an_async_treewalk_contained_root_is_stopped_through_the_proxy(#[fixture(ctrl_proxy)] _group: &Group) {
assert_default_stops_through_the_proxy(ContainMode::TreeWalk).await;
}
#[skuld::test]
async fn an_async_uncontained_no_window_child_is_stopped_through_the_proxy(#[fixture(ctrl_proxy)] _group: &Group) {
assert_current_thread();
let (mut child, mut sock, _) = spawn_member_async(Handler::AckEvent, None, None, |cmd| _ = cmd.no_window()).await;
Call::Terminate
.run(&mut child, Duration::ZERO)
.await
.expect("terminate through the proxy");
assert_proxy_answered(child.pid(), CtrlEvent::CtrlBreak, ProxyAnswer::Sent);
assert_acked_event(&mut sock, "the member", CtrlEvent::CtrlBreak);
kill_release_reap_async(&mut child, sock, false).await;
}