use core::error::Error;
use std::sync::{Arc, Mutex};
use crate::{LifecycleTap, RecordingTap, Station, Taps, drive::lane::block_on};
use super::{AsyncTeardown, Farewell, farewell};
struct Says {
name: &'static str,
said: Arc<Mutex<Vec<&'static str>>>,
refuses: bool,
}
impl Says {
fn new(name: &'static str, said: &Arc<Mutex<Vec<&'static str>>>) -> Self {
Self {
name,
said: Arc::clone(said),
refuses: false,
}
}
const fn refusing(mut self) -> Self {
self.refuses = true;
self
}
}
#[derive(Debug, derive_more::Display)]
#[display("the device would not release")]
struct Stuck {
handle: u32,
}
impl Error for Stuck {}
impl AsyncTeardown for Says {
async fn teardown(self) -> Result<(), Box<dyn Error + Send + Sync>> {
self
.said
.lock()
.expect("the chain's record")
.push(self.name);
if self.refuses {
return Err(Box::new(Stuck { handle: 7 }));
}
Ok(())
}
}
fn enrolled(said: &Arc<Mutex<Vec<&'static str>>>, names: &[&'static str]) -> Vec<Farewell> {
names
.iter()
.map(|name| Farewell::new(*name, Says::new(name, said)))
.collect()
}
#[test]
fn the_goodbyes_are_said_last_enrolled_first() {
let said = Arc::new(Mutex::new(Vec::new()));
let untorn = block_on(farewell(
enrolled(&said, &["pool", "decode", "scenes"]),
&Taps::new(),
));
assert_eq!(
*said.lock().expect("the chain's record"),
["scenes", "decode", "pool"],
"the order everything was built in, run backwards"
);
assert!(untorn.is_empty(), "every one of them said it cleanly");
}
#[test]
fn a_refusal_never_interrupts_the_chain_and_is_named_in_the_report() {
let said = Arc::new(Mutex::new(Vec::new()));
let farewells = vec![
Farewell::new("pool", Says::new("pool", &said)),
Farewell::new("decode", Says::new("decode", &said).refusing()),
Farewell::new("scenes", Says::new("scenes", &said)),
];
let untorn = block_on(farewell(farewells, &Taps::new()));
assert_eq!(
*said.lock().expect("the chain's record"),
["scenes", "decode", "pool"],
"the goodbye after the refusal was still said"
);
assert_eq!(untorn.len(), 1);
assert_eq!(untorn[0].name().as_str(), "decode");
let refusal = untorn[0].error().expect("it refused with its own error");
assert_eq!(
refusal
.downcast_ref::<Stuck>()
.expect("the author's own error survives the chain")
.handle,
7
);
assert!(
untorn[0]
.to_string()
.contains("the device would not release")
);
}
#[test]
fn a_refusal_is_observed_under_its_own_name() {
let said = Arc::new(Mutex::new(Vec::new()));
let tap = Arc::new(RecordingTap::new());
let taps = Taps::new().with(Arc::clone(&tap) as Arc<dyn LifecycleTap>);
let farewells = vec![Farewell::new(
"decode",
Says::new("decode", &said).refusing(),
)];
let untorn = block_on(farewell(farewells, &taps));
assert_eq!(untorn.len(), 1);
let seen = tap.seen();
assert_eq!(seen.len(), 1, "the tap watching the shutdown was told");
assert_eq!(seen[0], ("decode".to_owned(), Station::Failed));
}
#[test]
fn a_goodbye_nobody_said_carries_no_refusal() {
let said = Arc::new(Mutex::new(Vec::new()));
let untorn = Farewell::new("decode", Says::new("decode", &said)).unsaid();
assert_eq!(untorn.name().as_str(), "decode");
assert!(
untorn.error().is_none(),
"an abort does not start flushing what it gave up waiting for"
);
assert!(untorn.to_string().contains("never told"));
assert!(
said.lock().expect("the chain's record").is_empty(),
"and the value's own goodbye never ran"
);
}