#[test]
fn job_handle_debug_does_not_panic() {
use super::JobHandle;
let h = JobHandle::create_empty_for_test();
h.hard_kill();
let s = format!("{h:?}");
assert!(s.contains("JobHandle"), "debug output: {s}");
}
#[test]
fn wait_drained_on_a_consumed_handle_is_unassessable() {
use super::JobHandle;
let h = JobHandle::create_empty_for_test();
h.hard_kill();
let err = h
.wait_drained(Some(None), None)
.expect_err("a consumed job handle must not report a live drain verdict");
let crate::error::Error::Unassessable { source, .. } = err else {
panic!("expected Unassessable, got {err:?}");
};
assert!(
source.is_none(),
"nothing was asked of the OS on this path — no source is expected"
);
}
#[test]
fn query_job_pid_list_is_empty_for_an_unpopulated_job() {
use super::JobHandle;
let job = JobHandle::create_empty_for_test();
let job_handle = job.as_handle().expect("freshly created job handle must be live");
let pids = super::query_job_pid_list(job_handle).expect("query pid list");
assert!(pids.is_empty(), "an empty job must report no members: {pids:?}");
}
#[test]
fn wait_drained_raw_reports_drained_for_an_empty_job() {
use super::JobHandle;
let job = JobHandle::create_empty_for_test();
let job_handle = job.as_handle().expect("freshly created job handle must be live");
let verdict = super::wait_drained_raw(job_handle, Some(None), None).expect("wait_drained_raw");
assert_eq!(verdict, crate::containment::TreeDrain::AllMembersExited);
}
#[test]
fn wait_drained_raw_tracks_a_real_member_through_exit() {
use std::os::windows::io::AsRawHandle;
let mut child = std::process::Command::new("cmd")
.args(["/C", "more"])
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
.expect("spawn cmd /C more");
let raw = child.as_raw_handle();
let job = super::assign_to_kill_on_close_job(raw).expect("assign to job");
let job_handle = job.as_handle().expect("freshly created job handle must be live");
let pids = super::query_job_pid_list(job_handle).expect("query pid list");
assert_eq!(
pids,
vec![child.id()],
"the job must report exactly the member just assigned"
);
let past = std::time::Instant::now() - std::time::Duration::from_secs(1);
let verdict = super::wait_drained_raw(job_handle, Some(Some(past)), None).expect("wait_drained_raw");
assert_eq!(verdict, crate::containment::TreeDrain::MembersRemain);
drop(child.stdin.take());
child.wait().expect("wait for cmd /C more to exit");
let verdict = super::wait_drained_raw(job_handle, Some(None), None).expect("wait_drained_raw");
assert_eq!(verdict, crate::containment::TreeDrain::AllMembersExited);
}
#[test]
fn caller_has_console_is_true_under_cargo_test() {
assert!(
matches!(super::caller_has_console(), Ok(true)),
"cargo test is expected to run with a console attached"
);
}
#[test]
fn console_probe_error_surfaces() {
super::fault::set_force_console_probe_error(true);
assert!(super::caller_has_console().is_err());
assert!(!super::fault::armed(), "the seam must be consumed by one call");
assert!(matches!(super::caller_has_console(), Ok(true)));
}
const E_INVALID_HANDLE: i32 = 0x8007_0006u32 as i32;
const E_ACCESS_DENIED: i32 = 0x8007_0005u32 as i32;
fn io_err(hresult: i32) -> std::io::Error {
std::io::Error::from_raw_os_error(hresult)
}
#[test]
fn invalid_handle_with_no_console_is_typed_no_console() {
let e = super::classify_ctrl_event_failure(4242, io_err(E_INVALID_HANDLE), Ok(false));
let crate::error::Error::NoConsole { detail } = e else {
panic!("expected NoConsole, got {e:?}");
};
assert!(detail.contains("4242"), "the detail must name the group: {detail}");
assert!(
detail.contains("kill_tree()"),
"the detail must name the way out: {detail}"
);
}
#[test]
fn invalid_handle_with_a_console_attached_stays_io() {
let e = super::classify_ctrl_event_failure(1, io_err(E_INVALID_HANDLE), Ok(true));
assert!(matches!(e, crate::error::Error::Io(_)), "got {e:?}");
}
#[test]
fn invalid_handle_with_a_failed_probe_stays_io() {
let probe = Err(std::io::Error::from_raw_os_error(87)); let e = super::classify_ctrl_event_failure(1, io_err(E_INVALID_HANDLE), probe);
assert!(matches!(e, crate::error::Error::Io(_)), "got {e:?}");
}
#[test]
fn a_different_failure_code_stays_io_whatever_the_probe_says() {
for console in [Ok(true), Ok(false)] {
let e = super::classify_ctrl_event_failure(1, io_err(E_ACCESS_DENIED), console);
assert!(matches!(e, crate::error::Error::Io(_)), "got {e:?}");
}
let e = super::classify_ctrl_event_failure(1, io_err(E_ACCESS_DENIED), Err(io_err(87)));
assert!(matches!(e, crate::error::Error::Io(_)), "got {e:?}");
}
#[test]
fn the_no_console_signature_matches_the_real_win32_mapping() {
use windows::Win32::Foundation::ERROR_INVALID_HANDLE;
let hr = windows::core::HRESULT::from_win32(ERROR_INVALID_HANDLE.0);
assert_eq!(hr.0, E_INVALID_HANDLE);
assert_eq!(
std::io::Error::from(windows::core::Error::from_hresult(hr)).raw_os_error(),
Some(E_INVALID_HANDLE)
);
}