use std::cell::Cell;
use super::{after_grace, after_tree_grace};
use crate::error::{said, Error};
const CALL: &str = "graceful_shutdown_tree(7)";
fn io_error(msg: &str) -> Error {
Error::Io(std::io::Error::other(msg.to_owned()))
}
fn counted(
result: Result<(), Error>,
policy: impl FnOnce(&mut dyn FnMut() -> Result<(), Error>) -> Result<(), Error>,
) -> (Result<(), Error>, u32) {
let calls = Cell::new(0);
let mut result = Some(result);
let out = policy(&mut || {
calls.set(calls.get() + 1);
result.take().expect("called at most once")
});
(out, calls.get())
}
fn message(r: Result<(), Error>) -> String {
said(&r.expect_err("an error"))
}
fn call() -> String {
CALL.to_owned()
}
fn grace_surfacing(watch: Result<bool, Error>, kill: &mut dyn FnMut() -> Result<(), Error>) -> Result<(), Error> {
after_grace(call, watch, kill)?.map_or(Ok(()), Err)
}
#[skuld::test]
fn after_grace_hands_back_the_watch_error_once_the_kill_is_done() {
let (out, kills) = counted(Ok(()), |k| {
let handed = after_grace(call, Err(io_error("watch")), k).expect("the kill succeeded");
assert!(message(handed.map_or(Ok(()), Err)).contains("watch"));
Ok(())
});
out.expect("handed back");
assert_eq!(kills, 1);
}
#[skuld::test]
fn after_grace_builds_the_label_for_a_kill_error_only() {
let (out, _) = counted(Ok(()), |k| {
after_grace(|| panic!("the label was built"), Ok(false), k)?;
Ok(())
});
out.expect("no kill error");
}
#[skuld::test]
fn after_grace_is_done_without_a_kill_when_the_process_exited_in_grace() {
let (out, kills) = counted(Ok(()), |k| grace_surfacing(Ok(true), k));
out.expect("exited in grace is done");
assert_eq!(kills, 0);
}
#[skuld::test]
fn after_grace_kills_when_the_grace_ran_out() {
let (out, kills) = counted(Ok(()), |k| grace_surfacing(Ok(false), k));
out.expect("a successful kill is done");
assert_eq!(kills, 1);
}
#[skuld::test]
fn after_grace_kills_then_reports_the_watch_error() {
let (out, kills) = counted(Ok(()), |k| grace_surfacing(Err(io_error("watch")), k));
assert!(message(out).contains("watch"));
assert_eq!(kills, 1, "an unobservable grace escalates");
}
fn failures_below(error: &Error) -> Vec<String> {
error
.failures_below()
.into_iter()
.map(|failure| said(failure))
.collect()
}
fn containment(detail: &str) -> Error {
Error::Containment {
detail: detail.to_owned(),
source: None,
}
}
#[skuld::test]
fn after_grace_an_io_kill_error_stays_the_answer_and_the_watch_error_is_a_warn() {
crate::log_capture::install();
let mark = crate::log_capture::mark();
let (out, kills) = counted(Err(io_error("kill")), |k| grace_surfacing(Err(io_error("watch")), k));
assert!(matches!(&out, Err(Error::Io(e)) if e.to_string() == "kill"), "{out:?}");
assert_eq!(kills, 1);
let logged = crate::log_capture::records_since_on_current_thread(mark, "watch");
assert!(
matches!(&logged[..], [(log::Level::Warn, line)] if line.starts_with(CALL)),
"the warn names the caller's call: {logged:?}"
);
}
#[skuld::test]
fn after_grace_a_lone_kill_error_stays_as_it_is() {
let (out, _) = counted(Err(io_error("kill")), |k| grace_surfacing(Ok(false), k));
assert!(matches!(out, Err(Error::Io(_))), "{out:?}");
}
#[skuld::test]
fn after_grace_a_kill_error_with_a_slot_keeps_its_variant() {
let (out, _) = counted(Err(containment("kill")), |k| grace_surfacing(Err(io_error("watch")), k));
assert!(
matches!(&out, Err(Error::Containment { detail, source: Some(cause) })
if detail == "kill" && said(cause) == crate::error::io_said("watch")),
"{out:?}"
);
}
#[skuld::test]
fn after_tree_grace_sweeps_even_when_the_root_exited() {
let (out, sweeps) = counted(Ok(()), |s| after_tree_grace(CALL, Ok(true), s));
out.expect("a successful sweep is done");
assert_eq!(sweeps, 1);
}
#[skuld::test]
fn after_tree_grace_an_io_sweep_error_stays_the_answer_and_the_watch_error_is_a_warn() {
crate::log_capture::install();
let mark = crate::log_capture::mark();
let (out, sweeps) = counted(Err(io_error("sweep")), |s| {
after_tree_grace(CALL, Err(io_error("watch")), s)
});
assert!(matches!(&out, Err(Error::Io(e)) if e.to_string() == "sweep"), "{out:?}");
assert_eq!(sweeps, 1);
let logged = crate::log_capture::records_since_on_current_thread(mark, "watch");
assert!(matches!(&logged[..], [(log::Level::Warn, _)]), "{logged:?}");
}
#[skuld::test]
fn a_sweep_that_left_members_carries_the_refusal_and_the_watch_error_in_others() {
let left = || {
Error::members_left(
crate::error::Failures::new(
std::sync::Arc::new(Error::MemberNotKilled {
pid: 7,
source: std::sync::Arc::new(io_error("member")),
}),
[],
),
None,
)
};
let out = super::joined(CALL, left(), [containment("refused"), io_error("watch")]);
let Error::MembersLeft { members, others } = &out else {
panic!("the variant is kept: {out:?}");
};
assert_eq!(members.iter().len(), 1);
let Some(Error::Several(others)) = others.as_deref() else {
panic!("expected a Several in others: {out:?}");
};
let texts: Vec<_> = others.iter().map(|e| said(e)).collect();
assert!(
texts[0].contains("refused") && texts[1] == crate::error::io_said("watch") && texts.len() == 2,
"{texts:?}"
);
let out = after_tree_grace(CALL, Err(io_error("watch")), || Err(left()));
assert!(
matches!(&out, Err(Error::MembersLeft { others: Some(o), .. }) if said(o) == crate::error::io_said("watch")),
"{out:?}"
);
}
#[skuld::test]
fn after_tree_grace_a_sweep_error_with_a_slot_keeps_its_variant() {
let (out, _) = counted(Err(containment("sweep")), |s| {
after_tree_grace(CALL, Err(io_error("watch")), s)
});
assert!(
matches!(&out, Err(Error::Containment { detail, source: Some(cause) })
if detail == "sweep" && said(cause) == crate::error::io_said("watch")),
"{out:?}"
);
}
#[skuld::test]
fn after_reap_takes_the_refusal_and_the_watch_error_as_peers_of_a_failed_reap() {
let status = std::process::ExitStatus::default();
let out = super::after_reap(
call,
Err(containment("reap")),
Some(containment("refused")),
Some(io_error("watch")),
);
let out = out.expect_err("all failed");
assert!(
matches!(&out, Error::Containment { detail, .. } if detail == "reap"),
"{out:?}"
);
let below = failures_below(&out);
assert!(
below.len() == 2 && below[0].contains("refused") && below[1] == crate::error::io_said("watch"),
"{below:?}"
);
assert_eq!(
message_of(super::after_reap(
call,
Ok(status),
Some(containment("refused")),
Some(io_error("watch"))
)),
crate::error::io_said("watch")
);
assert_eq!(
message_of(super::after_reap(call, Err(io_error("reap")), None, None)),
crate::error::io_said("reap")
);
assert_eq!(
super::after_reap(call, Ok(status), None, None).expect("neither failed"),
status
);
assert_eq!(
super::after_reap(call, Ok(status), Some(containment("refused")), None).expect("a superseded refusal"),
status
);
}
fn message_of(r: Result<std::process::ExitStatus, Error>) -> String {
said(&r.expect_err("an error"))
}
#[skuld::test]
fn after_tree_grace_reports_the_watch_error_after_the_sweep() {
let (out, sweeps) = counted(Ok(()), |s| after_tree_grace(CALL, Err(io_error("watch")), s));
assert!(message(out).contains("watch"));
assert_eq!(sweeps, 1);
}