use std::time::{Duration, Instant};
use super::super::read::Taken;
use super::stream::piece;
use super::{Mailbox, NOW, call, poll, quick, took};
fn patient() -> Mailbox {
Mailbox::holding(4000, Duration::from_millis(5))
}
fn parked(mail: &Mailbox, client: &str) {
let deadline = Instant::now() + Duration::from_secs(10);
while !mail.serving(client) {
assert!(Instant::now() < deadline, "{client} never parked");
std::thread::sleep(Duration::from_millis(1));
}
}
#[test]
fn an_activation_ends_the_parked_read_and_tells_it() {
let mail = patient();
let old = mail.clone();
let began = Instant::now();
let reader = std::thread::spawn(move || old.take("laptop"));
parked(&mail, "laptop");
mail.supersede("laptop");
assert!(!mail.serving("laptop"), "the slot is free at once");
let told = reader.join().expect("the old read");
assert_eq!(
told,
Ok(Taken::Superseded),
"the read that lost the queue is told so"
);
assert!(
told.is_ok_and(|taken| taken.work().is_empty()),
"and was handed nothing"
);
assert!(
began.elapsed() < Duration::from_secs(10),
"and it was ended, not left to expire"
);
}
#[test]
fn work_posted_after_the_activation_goes_to_the_successor() {
let mail = patient();
let old = mail.clone();
let reader = std::thread::spawn(move || old.take("laptop"));
parked(&mail, "laptop");
mail.supersede("laptop");
let id = mail.post(NOW, "local", &call("laptop", "Bash"));
assert_eq!(reader.join().expect("the old read"), Ok(Taken::Superseded));
assert_eq!(
took(&quick_on(&mail), "laptop")
.first()
.map(|i| i.id.clone()),
Some(id),
"handed once, to the read that holds the queue"
);
}
fn quick_on(mail: &Mailbox) -> Mailbox {
Mailbox {
cell: mail.cell.clone(),
..quick()
}
}
#[test]
fn a_superseded_read_leaving_does_not_free_its_successors_slot() {
let mail = quick();
let old = mail.reading("laptop").expect("the first activation's read");
mail.supersede("laptop");
let new = mail.reading("laptop").expect("granted at once");
drop(old);
assert!(
mail.serving("laptop"),
"the successor still holds the queue"
);
drop(new);
assert!(!mail.serving("laptop"));
}
#[test]
fn an_activation_with_nothing_parked_changes_nothing() {
let mail = quick();
mail.supersede("laptop");
assert!(took(&mail, "laptop").is_empty());
}
#[test]
fn an_activation_ends_only_its_own_certificates_read() {
let mail = quick();
let _phone = mail.reading("phone").expect("the phone's own slot");
mail.supersede("laptop");
assert!(mail.serving("phone"));
}
#[test]
fn work_the_superseded_activation_held_follows_the_lease() {
let mail = quick();
let begun = mail.post(NOW, "local", &call("laptop", "Bash"));
let unheard = mail.post(NOW, "local", &call("laptop", "Bash"));
assert_eq!(took(&mail, "laptop").len(), 2, "both handed to the old one");
mail.complete(NOW, "laptop", &begun, &piece("half", ""))
.expect("in flight");
mail.supersede("laptop");
let again: Vec<String> = took(&mail, "laptop").into_iter().map(|i| i.id).collect();
assert_eq!(again, vec![unheard], "only the unacknowledged one");
let said = poll(&mail, "local", &begun)
.expect("the asker's own handle")
.expect("the server ended the run itself");
assert_eq!(said.stdout, "half");
assert_ne!(said.exit_code, 0);
assert!(
said.stderr
.contains("asked for work again without finishing it"),
"{}",
said.stderr
);
}