use std::time::Duration;
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::stop::{self, StopFuture};
use crate::tokio::test_runtime::{assert_current_thread, never_blocking};
use crate::{ContainMode, StopTarget};
fn through_the_proxy(
event: CtrlEvent,
) -> impl for<'t, 'c> Fn(&'t StopTarget<'c>) -> StopFuture<'t> + Send + Sync + 'static {
move |target| Box::pin(stop::send_console_event_via_proxy(target, event))
}
fn stops_through_the_proxy(cmd: &mut crate::tokio::Command, event: CtrlEvent) {
cmd.no_window();
cmd.graceful_stop(through_the_proxy(event));
}
#[skuld::test]
async fn async_the_proxy_delivers_ctrl_break_to_a_no_window_child(#[fixture(ctrl_proxy)] _group: &Group) {
assert_current_thread();
let (mut child, mut sock, _) = spawn_member_async(Handler::AckEvent, None, Some(ContainMode::Strongest), |cmd| {
stops_through_the_proxy(cmd, CtrlEvent::CtrlBreak)
})
.await;
never_blocking(child.terminate())
.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;
}
#[skuld::test]
async fn async_the_tree_calls_use_the_proxy_too(#[fixture(ctrl_proxy)] _group: &Group) {
assert_current_thread();
let (mut child, _sock, grand) = spawn_member_async(
Handler::Default,
Some(Handler::Default),
Some(ContainMode::Strongest),
|cmd| stops_through_the_proxy(cmd, CtrlEvent::CtrlBreak),
)
.await;
let grand = grand.expect("a grandchild was asked for");
never_blocking(child.terminate_tree())
.await
.expect("terminate_tree through the proxy");
assert_proxy_answered(child.pid(), CtrlEvent::CtrlBreak, ProxyAnswer::Sent);
assert_eq!(grand.exit.wait_for_exit_code(), STATUS_CONTROL_C_EXIT, "the grandchild");
assert_eq!(
child.wait().await.expect("reap the root").code(),
Some(STATUS_CONTROL_C_EXIT),
"the root"
);
let (mut child, _sock, grand) = spawn_member_async(
Handler::Default,
Some(Handler::Default),
Some(ContainMode::Strongest),
|cmd| stops_through_the_proxy(cmd, CtrlEvent::CtrlBreak),
)
.await;
let grand = grand.expect("a grandchild was asked for");
let pid = child.pid();
let check = crate::graceful_hooks::at_checked(crate::graceful_hooks::HookPoint::AfterTerminate, move || {
assert_proxy_answered(pid, CtrlEvent::CtrlBreak, ProxyAnswer::Sent)
});
let status = never_blocking(child.graceful_shutdown_tree(Duration::from_secs(30)))
.await
.expect("graceful_shutdown_tree through the proxy");
check.assert_fired();
assert_eq!(status.code(), Some(STATUS_CONTROL_C_EXIT), "the root, got {status:?}");
assert_eq!(grand.exit.wait_for_exit_code(), STATUS_CONTROL_C_EXIT, "the grandchild");
}