use std::time::Duration;
use super::{assert_no_break_sent, connect_reporter, kill_release_reap, report_done, spawn_ignoring_root};
use crate::error::Error;
use crate::identity::Liveness;
use crate::{Containment, GracefulMechanism};
pub(crate) const REPORTER_TEST: &str = "test_child::ctrl_break::console_less::fixture_console_less_reporter";
pub(crate) const ADDR_ENV: &str = "COSCA_FIXTURE_CONSOLE_LESS_ADDR";
fn assert_no_console() {
use windows::Win32::Foundation::{SetLastError, ERROR_INVALID_HANDLE, WIN32_ERROR};
use windows::Win32::System::Console::GetConsoleProcessList;
let mut list = [0u32; 1];
let n = unsafe {
SetLastError(WIN32_ERROR(0));
GetConsoleProcessList(&mut list)
};
assert_eq!(n, 0, "the reporter must have no console, else the checks are vacuous");
assert_eq!(
std::io::Error::last_os_error().raw_os_error(),
Some(ERROR_INVALID_HANDLE.0 as i32),
"GetConsoleProcessList must fail for want of a console, not for another reason"
);
}
#[skuld::test]
fn fixture_console_less_reporter() {
let Some(mut report) = connect_reporter(ADDR_ENV) else {
return;
};
assert_no_console();
let (child, sock) = spawn_ignoring_root();
assert_eq!(child.containment(), Containment::JobObject);
assert!(
matches!(child.terminate_tree(), Err(Error::NoConsole { .. })),
"terminate_tree must report NoConsole"
);
assert_no_break_sent();
assert_eq!(
child.is_alive(),
Liveness::Alive,
"a fail-fast terminate leaves the tree alone"
);
assert!(
matches!(
child.graceful_shutdown_tree(Duration::ZERO),
Err(Error::NoConsole { .. })
),
"graceful_shutdown_tree must report NoConsole"
);
assert_no_break_sent();
assert_eq!(
child.is_alive(),
Liveness::Alive,
"graceful_shutdown_tree must fail before signalling, not after hard-killing"
);
kill_release_reap(&child, sock, true);
let (child, sock) = spawn_ignoring_root();
assert_eq!(child.graceful_mechanism(), GracefulMechanism::ConsoleGroup);
assert!(
matches!(child.terminate(), Err(Error::NoConsole { .. })),
"terminate must report NoConsole"
);
assert_no_break_sent();
assert_eq!(
child.is_alive(),
Liveness::Alive,
"a fail-fast terminate leaves the child alone"
);
assert!(
matches!(child.graceful_shutdown(Duration::ZERO), Err(Error::NoConsole { .. })),
"graceful_shutdown must report NoConsole"
);
assert_no_break_sent();
assert_eq!(
child.is_alive(),
Liveness::Alive,
"graceful_shutdown must fail before signalling, not after hard-killing"
);
kill_release_reap(&child, sock, false);
report_done(&mut report);
}