use std::collections::VecDeque;
use super::{settle, AttachError, Stop};
struct Script<T> {
what: &'static str,
answers: VecDeque<T>,
}
impl<T> Script<T> {
fn next(&mut self) -> T {
self.answers
.pop_front()
.unwrap_or_else(|| panic!("{} was read more often than the test scripted", self.what))
}
}
impl<T> Drop for Script<T> {
fn drop(&mut self) {
assert!(
std::thread::panicking() || self.answers.is_empty(),
"{} was read less often than the test scripted",
self.what
);
}
}
fn scripted<T>(what: &'static str, answers: Vec<T>) -> impl FnMut() -> T {
let mut script = Script {
what,
answers: answers.into(),
};
move || script.next()
}
fn record(si_pid: i32, si_code: i32, si_status: i32) -> libc::siginfo_t {
let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
info.si_pid = si_pid;
info.si_code = si_code;
info.si_status = si_status;
info
}
fn never<T>(what: &'static str) -> impl FnMut() -> T {
scripted(what, Vec::new())
}
#[skuld::test]
fn an_exit_record_ends_the_wait_with_its_code_and_status() {
let result = settle(
scripted("stop", vec![Ok(Stop::Running)]),
scripted("exited", vec![Ok(record(7, libc::CLD_KILLED, libc::SIGKILL))]),
);
assert!(
matches!(result, Err(AttachError::Exited { code, status }) if code == libc::CLD_KILLED && status == libc::SIGKILL),
"{result:?}"
);
}
#[skuld::test]
fn every_exit_kind_ends_the_wait() {
for code in [libc::CLD_EXITED, libc::CLD_KILLED, libc::CLD_DUMPED] {
let result = settle(
scripted("stop", vec![Ok(Stop::Settling)]),
scripted("exited", vec![Ok(record(7, code, 0))]),
);
assert!(
matches!(result, Err(AttachError::Exited { code: c, .. }) if c == code),
"{code}: {result:?}"
);
}
}
#[skuld::test]
fn a_stop_record_is_not_an_exit() {
let result = settle(
scripted("stop", vec![Ok(Stop::Running), Ok(Stop::Stopped(libc::SIGSTOP))]),
scripted("exited", vec![Ok(record(7, libc::CLD_TRAPPED, libc::SIGSTOP))]),
);
assert!(result.is_ok(), "{result:?}");
}
#[skuld::test]
fn an_empty_record_is_not_an_exit() {
let result = settle(
scripted("stop", vec![Ok(Stop::Running), Ok(Stop::Stopped(libc::SIGSTOP))]),
scripted("exited", vec![Ok(record(0, libc::CLD_KILLED, 0))]),
);
assert!(result.is_ok(), "{result:?}");
}
#[skuld::test]
fn a_settled_stop_ends_the_wait_without_an_exit_peek() {
let result = settle(
scripted("stop", vec![Ok(Stop::Stopped(libc::SIGSTOP))]),
never("exited"),
);
assert!(result.is_ok(), "{result:?}");
}
#[skuld::test]
fn a_settling_stop_is_polled_again() {
let result = settle(
scripted(
"stop",
vec![Ok(Stop::Settling), Ok(Stop::Settling), Ok(Stop::Stopped(libc::SIGSTOP))],
),
scripted("exited", vec![Ok(record(0, 0, 0)), Ok(record(0, 0, 0))]),
);
assert!(result.is_ok(), "{result:?}");
}
#[skuld::test]
fn a_failed_stop_peek_is_its_errno() {
let result = settle(scripted("stop", vec![Err(libc::EPERM)]), never("exited"));
assert!(matches!(result, Err(AttachError::Errno(libc::EPERM))), "{result:?}");
}
#[skuld::test]
fn a_failed_exit_peek_is_its_errno() {
let result = settle(
scripted("stop", vec![Ok(Stop::Running)]),
scripted("exited", vec![Err(libc::ECHILD)]),
);
assert!(matches!(result, Err(AttachError::Errno(libc::ECHILD))), "{result:?}");
}