use core::{any::Any, error::Error};
use jiff::SignedDuration;
use super::{
App, Enrolled, Farewell, Stopped, Untorn,
teardown::{Collected, collected},
};
use crate::{AsyncTeardown, Resources, Shutdown, Taps, drive::engine::Join};
fn one_class_each(stopped: &Stopped) {
let unsettled: Vec<&str> = stopped.unsettled().map(|name| name.as_str()).collect();
for row in stopped.untorn() {
assert!(
!unsettled.contains(&row.name().as_str()),
"`{}` is named in both classes, which is the report inventing a fact: {stopped}",
row.name().as_str()
);
}
}
fn named(rows: &[Untorn]) -> Vec<&str> {
rows.iter().map(|row| row.name().as_str()).collect()
}
struct Nothing;
impl AsyncTeardown for Nothing {
async fn teardown(self) -> Result<(), Box<dyn Error + Send + Sync>> {
Ok(())
}
}
fn goodbye(name: &'static str) -> Farewell {
Farewell::new(name, Nothing)
}
struct SettlesAsItGoes(Option<Enrolled>);
impl Drop for SettlesAsItGoes {
fn drop(&mut self) {
if let Some(enrolled) = self.0.take() {
enrolled.settle_saying(Box::pin(async { Ok(()) }));
}
}
}
fn source_that_settles(enrolled: Enrolled) -> Box<dyn Any + Send> {
Box::new(SettlesAsItGoes(Some(enrolled)))
}
fn task_that_settles(enrolled: Enrolled) -> Join {
let settling = SettlesAsItGoes(Some(enrolled));
Box::pin(async move {
drop(settling);
})
}
fn world(
shutdown: Shutdown,
sources: Vec<Box<dyn Any + Send>>,
tasks: Vec<Join>,
teardowns: Vec<Farewell>,
) -> App<()> {
App::new(
std::sync::Arc::new(Resources::new()),
shutdown,
Taps::new(),
std::sync::Arc::new(crate::Registries::default()),
Vec::new(),
sources,
Vec::new(),
Vec::new(),
tasks,
teardowns,
std::collections::HashMap::new(),
std::collections::HashMap::new(),
std::collections::HashMap::new(),
)
}
#[test]
fn a_party_that_settles_inside_the_window_is_named_once_and_not_twice() {
let mut shutdown = Shutdown::new();
let decode = shutdown.register("decode");
let scenes = shutdown.register("scenes");
let stopped = world(
shutdown,
Vec::new(),
vec![task_that_settles(decode)],
Vec::new(),
)
.abort();
one_class_each(&stopped);
let unanswered: Vec<&str> = stopped.unsettled().map(|name| name.as_str()).collect();
assert_eq!(
unanswered,
["decode", "scenes"],
"the seat was read before either of them had settled, so both are named: {stopped}"
);
assert_eq!(
stopped.settled() + unanswered.len(),
2,
"and the registry's `N` is spent exactly once: {stopped}"
);
assert!(
stopped.untorn().is_empty(),
"a goodbye handed over after the reading is one nothing collected — and a party already named \
un-settled does not also get a row: {stopped}"
);
drop(scenes);
}
#[test]
fn the_seat_is_read_once_so_a_party_that_settles_mid_read_lands_in_one_class() {
let mut shutdown = Shutdown::new();
let decode = shutdown.register("decode");
shutdown.register("scenes").settle();
assert!(
!shutdown.parties()[0].settled(),
"the look finds it still running, which is the window's first instant"
);
decode.settle_saying(Box::pin(async { Ok(()) }));
let Collected { chain, unsettled } = collected(&shutdown);
assert!(
unsettled.is_empty(),
"the take found the settlement, so the party is ANSWERED — and the look that preceded it names \
nobody, because nothing reads it"
);
let rows: Vec<Untorn> = chain.into_iter().map(Farewell::unsaid).collect();
assert_eq!(
named(&rows),
["decode"],
"its goodbye is the one row: the party is in this class INSTEAD of the other, not as well"
);
}
#[test]
fn a_silent_settlement_is_answered_and_in_neither_class() {
let mut shutdown = Shutdown::new();
shutdown.register("decode").settle();
let Collected { chain, unsettled } = collected(&shutdown);
assert!(unsettled.is_empty(), "it settled, so it is not abandoned");
assert!(
chain.is_empty(),
"and it said nothing, so it is not a row either: no name, no allocation"
);
}
#[test]
fn a_party_that_never_settles_is_named_once_and_hands_over_no_row() {
let mut shutdown = Shutdown::new();
let decode = shutdown.register("decode");
let stopped = world(shutdown, Vec::new(), Vec::new(), vec![goodbye("pool")]).abort();
one_class_each(&stopped);
assert_eq!(stopped.settled(), 0);
let unanswered: Vec<&str> = stopped.unsettled().map(|name| name.as_str()).collect();
assert_eq!(unanswered, ["decode"]);
assert_eq!(
named(stopped.untorn()),
["pool"],
"the only row is the launch's OWN enrolment — a party that handed nothing over has none"
);
drop(decode);
}
#[test]
fn a_party_that_settles_during_an_abort_is_answered_and_never_also_abandoned() {
let mut shutdown = Shutdown::new();
let decode = shutdown.register("decode");
shutdown.register("scenes").settle();
let stopped = world(
shutdown,
vec![source_that_settles(decode)],
Vec::new(),
Vec::new(),
)
.abort();
one_class_each(&stopped);
assert_eq!(
stopped.settled(),
2,
"the mid-stop settler answered, and the registry counts it as answered: {stopped}"
);
assert_eq!(
stopped.unsettled().len(),
0,
"so nobody is abandoned: {stopped}"
);
assert_eq!(
named(stopped.untorn()),
["decode"],
"and the goodbye it handed over is its ONE class — the silent party has no row at all"
);
}
#[test]
fn an_overrun_puts_the_mid_stop_settler_and_the_abandoned_party_in_one_class_each() {
let mut shutdown = Shutdown::new();
let decode = shutdown.register("decode");
let scenes = shutdown.register("scenes");
let stopped = world(
shutdown,
vec![source_that_settles(decode)],
Vec::new(),
vec![goodbye("pool")],
)
.shutdown(Some(SignedDuration::from_millis(-500)));
one_class_each(&stopped);
assert!(
stopped.overran(),
"the deadline had passed before it was given"
);
let unanswered: Vec<&str> = stopped.unsettled().map(|name| name.as_str()).collect();
assert_eq!(unanswered, ["scenes"], "only the one that never settled");
assert_eq!(
stopped.settled() + unanswered.len(),
2,
"and the registry's `N` is spent exactly once: {stopped}"
);
assert_eq!(
named(stopped.untorn()),
["decode", "pool"],
"the settler's goodbye and the launch's own, in enrolment order — and `scenes`, which handed \
nothing over, is in neither"
);
drop(scenes);
}