use std::{
collections::HashSet,
sync::{Arc, Mutex},
};
use jiff::SignedDuration;
use super::super::{
Ack, Answer, Custody, Delivery, DropOldest, Failure, Handover, Item, ItemId, Lifecycle,
LifecycleTap, NodeName, Observation, Occupancy, Outbound, OverflowDecision, Owed, Payload,
Policies, QueryDeadline, QueryError, ShedKey, Station, Taps, Terminal, ack_channel, channel,
reply,
};
use super::Fate;
#[derive(Debug, derive_more::Display)]
#[display("the reader gave out")]
struct GaveOut;
impl core::error::Error for GaveOut {}
fn shed(key: u64) -> Fate {
Fate::Shed {
key: ShedKey::new(ItemId::new(key)),
decision: OverflowDecision::DropOldest,
occupancy: Occupancy::bounded(1, 1),
message: (),
}
}
#[derive(Debug, Default)]
struct Counting {
seen: Mutex<Vec<(Option<Station>, Option<Terminal>)>>,
}
impl Counting {
fn seen(&self) -> Vec<(Option<Station>, Option<Terminal>)> {
self.seen.lock().expect("the counter").clone()
}
fn terminals(&self) -> Vec<Terminal> {
self
.seen()
.into_iter()
.filter_map(|(_, terminal)| terminal)
.collect()
}
}
impl LifecycleTap for Counting {
fn observe(&self, _node: &NodeName, observation: &Observation) {
self
.seen
.lock()
.expect("the counter")
.push((observation.station(), observation.terminal()));
}
}
fn counted() -> (Arc<Counting>, Taps) {
let tap = Arc::new(Counting::default());
let taps = Taps::new().with(tap.clone());
(tap, taps)
}
#[test]
fn every_terminal_names_itself_without_its_payload() {
assert_eq!(shed(7).terminal(), Terminal::Shed);
assert_eq!(
Fate::GivenUp {
attempt: 3,
failure: Some(Failure::new(GaveOut)),
message: (),
}
.terminal(),
Terminal::GivenUp
);
assert_eq!(
Fate::Expired {
budget: SignedDuration::from_millis(50),
message: (),
}
.terminal(),
Terminal::Expired
);
}
#[test]
fn the_terminal_names_are_stable() {
assert_eq!(Terminal::Shed.as_str(), "shed");
assert_eq!(Terminal::GivenUp.as_str(), "given_up");
assert_eq!(Terminal::Expired.as_str(), "expired");
assert_eq!(Terminal::Expired.to_string(), "expired");
}
#[test]
fn every_terminal_a_fate_speaks_is_a_terminal_an_observation_names() {
for fate in [
shed(1),
Fate::GivenUp {
attempt: 1,
failure: None,
message: (),
},
Fate::Expired {
budget: SignedDuration::ZERO,
message: (),
},
] {
let terminal = fate.terminal();
let (observation, ()) = fate.erase();
assert_eq!(
observation.terminal(),
Some(terminal),
"the stream saw a different ending from the one the fate spoke"
);
assert_eq!(observation.station(), None);
}
}
#[test]
fn the_shed_terminal_erases_onto_the_arm_the_stream_already_had() {
let (observation, ()) = shed(41).erase();
let Observation::Shed {
key,
decision,
occupancy,
} = observation
else {
panic!("a shed fate is the shed arm, and not an arm of its own");
};
assert_eq!(key.rendered(), ItemId::new(41).to_string());
assert_eq!(decision, OverflowDecision::DropOldest);
assert_eq!(occupancy.queued(), 1);
}
#[test]
fn the_given_up_and_expired_terminals_carry_the_endings_own_payload() {
let (observation, ()) = Fate::GivenUp {
attempt: 4,
failure: Some(Failure::new(GaveOut)),
message: (),
}
.erase();
let Observation::GivenUp {
attempt,
failure: Some(failure),
} = observation
else {
panic!("a given-up fate is the given-up arm, carrying its own attempt and error");
};
assert_eq!(attempt, 4);
assert_eq!(failure.to_string(), "the reader gave out");
let (observation, ()) = Fate::Expired {
budget: SignedDuration::from_secs(2),
message: (),
}
.erase();
let Observation::Expired { budget } = observation else {
panic!("an expired fate is the expired arm, carrying the budget that ran out");
};
assert_eq!(budget, SignedDuration::from_secs(2));
}
#[test]
fn the_extra_message_survives_the_erasure_beside_the_observation() {
let fate = Fate::Shed {
key: ShedKey::new(ItemId::new(9)),
decision: OverflowDecision::DropNewest,
occupancy: Occupancy::unbounded(3),
message: Why("the frame was already stale"),
};
assert_eq!(fate.message(), &Why("the frame was already stale"));
let (observation, message) = fate.erase();
assert_eq!(observation.terminal(), Some(Terminal::Shed));
assert_eq!(message, Why("the frame was already stale"));
}
#[test]
fn the_default_message_is_the_unit_and_needs_no_turbofish() {
let fate: Fate = Fate::Expired {
budget: SignedDuration::from_millis(1),
message: (),
};
assert!(fate.is_expired());
assert_eq!(fate.message(), &());
}
#[test]
fn nothing_is_both_a_station_and_a_terminal() {
let arrived = Lifecycle::Processed(0u8).erase();
assert_eq!(arrived.station(), Some(Station::Processed));
assert_eq!(arrived.terminal(), None);
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct Why(&'static str);
#[test]
fn the_sender_of_an_evicted_delivery_is_told_the_shed_and_credited_for_it() {
let (outbox, inbox) = channel::<ItemId, u64>(1);
let evictor = inbox.evictor();
let (tap, taps) = counted();
let sheds = Policies::new()
.with_overflow_policy(Box::new(DropOldest))
.sheds("stage", taps);
let (ack, answers) = ack_channel::<ItemId>();
outbox
.deliver(Delivery::new(
Payload::One(Item::new(ItemId::new(1), 10u64)),
ack,
))
.expect("the edge is empty and the consumer is there");
let (newest, _newest_answers) = ack_channel::<ItemId>();
let handover = sheds.deliver(
&outbox,
Some(&evictor),
Delivery::new(Payload::One(Item::new(ItemId::new(2), 20u64)), newest),
);
let Handover::Evicted(evicted) = handover else {
panic!("the oldest was pulled out to make room");
};
let answer = answers.take();
assert!(
!answer.is_hung_up(),
"a shed delivery is answered, and never left to read as a consumer that hung up"
);
let Answer::Settled(settled) = answer else {
panic!("the shed answered the token it was owed");
};
assert_eq!(settled.observation().terminal(), Some(Terminal::Shed));
assert!(settled.released().contains(&ItemId::new(1)));
assert_eq!(settled.worker().as_str(), "stage");
let (payload, custody) = evicted.into_parts();
assert!(custody.is_none());
assert_eq!(payload.len(), 1);
assert_eq!(tap.terminals(), vec![Terminal::Shed]);
}
#[test]
fn a_delivery_refused_by_a_dead_consumer_keeps_its_token() {
let (outbox, inbox) = channel::<ItemId, u64>(1);
let (_tap, taps) = counted();
let sheds = Policies::new()
.with_overflow_policy(Box::new(DropOldest))
.sheds("stage", taps);
drop(inbox);
let (ack, answers) = ack_channel::<ItemId>();
let handover = sheds.deliver(
&outbox,
None,
Delivery::new(Payload::One(Item::new(ItemId::new(1), 10u64)), ack),
);
let Handover::Gone(returned) = handover else {
panic!("the consumer hung up, and nothing was shed");
};
let (_payload, custody) = returned.into_parts();
assert!(custody.is_some(), "the token is still owed");
assert!(answers.take().is_waiting(), "and still unanswered");
drop(custody);
}
#[test]
fn a_given_up_delivery_answers_its_oneshot_and_the_gate_says_so() {
let (tap, taps) = counted();
let retries = Policies::new().retries("stage", taps);
let failure = Failure::new(GaveOut);
let decision = retries.settling(Station::Failed, 3, Some(&failure));
assert!(decision.is_give_up());
assert_eq!(tap.terminals(), vec![Terminal::GivenUp]);
let (ack, answers) = ack_channel::<ItemId>();
let mut owed = Owed::open([ItemId::new(5)], Some(Custody::sole(ack)));
assert!(owed.settle_fate(
"stage",
Fate::GivenUp {
attempt: 3,
failure: Some(failure),
message: (),
}
));
let Answer::Settled(settled) = answers.take() else {
panic!("the give-up answered the token it was owed");
};
assert_eq!(settled.observation().terminal(), Some(Terminal::GivenUp));
assert!(settled.released().contains(&ItemId::new(5)));
}
#[test]
fn an_expired_wait_is_a_terminal_to_the_stream_and_a_timeout_to_the_caller() {
let (tap, taps) = counted();
let budget = SignedDuration::from_millis(20);
let deadlines = Policies::new()
.with_deadline_policy(Box::new(QueryDeadline::of(budget)))
.deadlines("door", taps);
let (held, awaiting) = reply::<u64>();
let taken = deadlines.take(awaiting, None);
assert_eq!(taken.expect_err("the budget ran out"), QueryError::TimedOut);
assert_eq!(tap.terminals(), vec![Terminal::Expired]);
drop(held);
let (ack, answers) = ack_channel::<ItemId>();
assert!(Custody::sole(ack).fate(
HashSet::from([ItemId::new(8)]),
"door",
Fate::Expired {
budget,
message: (),
}
));
let Answer::Settled(settled) = answers.take() else {
panic!("the expiry answered the token it was owed");
};
assert_eq!(settled.observation().terminal(), Some(Terminal::Expired));
assert!(settled.released().contains(&ItemId::new(8)));
}
#[test]
fn every_delivery_a_sender_put_out_is_arrived_ended_or_still_in_flight() {
let tap = Counting::default();
let mut window: Outbound<ItemId> = Outbound::new();
let mut acks = Vec::new();
for key in 1..=5u64 {
let (ack, answers) = ack_channel::<ItemId>();
window.record([ItemId::new(key)], answers);
acks.push(ack);
}
let mut acks = acks.into_iter();
assert!(acks.next().expect("the first").send(Ack::new(
HashSet::from([ItemId::new(1)]),
"consumer",
Lifecycle::Processed(()),
)));
assert!(
acks
.next()
.expect("the second")
.fate(HashSet::from([ItemId::new(2)]), "stage", shed(2))
);
assert!(acks.next().expect("the third").fate(
HashSet::from([ItemId::new(3)]),
"stage",
Fate::GivenUp {
attempt: 2,
failure: None,
message: (),
}
));
assert!(acks.next().expect("the fourth").fate(
HashSet::from([ItemId::new(4)]),
"door",
Fate::Expired {
budget: SignedDuration::from_millis(5),
message: (),
}
));
let held = acks.next().expect("the fifth");
let harvest = window.harvest();
let mut arrived = 0usize;
let mut ended = 0usize;
for answer in harvest.answers() {
tap.observe(answer.worker(), answer.observation());
arrived += usize::from(answer.observation().station().is_some());
ended += usize::from(answer.observation().terminal().is_some());
}
assert_eq!(arrived, 1);
assert_eq!(ended, 3);
assert_eq!(window.outstanding(), 1, "the fifth is still in flight");
assert_eq!(
arrived + ended + window.outstanding(),
5,
"every delivery is in exactly one column"
);
assert_eq!(
window.lost(),
0,
"nothing hung up: an ending is an answer, not a disconnect"
);
let mut terminals = tap.terminals();
terminals.sort_by_key(|terminal| terminal.as_str());
assert_eq!(
terminals,
vec![Terminal::Expired, Terminal::GivenUp, Terminal::Shed]
);
drop(held);
}
#[test]
fn a_fate_carrying_more_than_the_ending_round_trips_to_the_sender() {
let (ack, answers) = ack_channel::<ItemId>();
let mut window: Outbound<ItemId> = Outbound::new();
window.record([ItemId::new(3)], answers);
assert!(Custody::sole(ack).fate(
HashSet::from([ItemId::new(3)]),
"stage",
Fate::Shed {
key: ShedKey::new(ItemId::new(3)),
decision: OverflowDecision::DropOldest,
occupancy: Occupancy::bounded(1, 1),
message: Why("the frame was already stale"),
}
));
let harvest = window.harvest();
let [answer] = harvest.answers() else {
panic!("one delivery, one answer");
};
assert_eq!(answer.observation().terminal(), Some(Terminal::Shed));
assert_eq!(
answer.message::<Why>(),
Some(&Why("the frame was already stale"))
);
assert_eq!(answer.message::<u8>(), None);
}
#[test]
fn a_delivery_with_no_token_is_still_only_tapped() {
let (outbox, inbox) = channel::<ItemId, u64>(1);
let evictor = inbox.evictor();
let (tap, taps) = counted();
let sheds = Policies::new()
.with_overflow_policy(Box::new(DropOldest))
.sheds("stage", taps);
outbox
.deliver(Delivery::in_custody(Payload::One(Item::new(
ItemId::new(1),
10u64,
))))
.expect("the edge is empty and the consumer is there");
let handover = sheds.deliver(
&outbox,
Some(&evictor),
Delivery::in_custody(Payload::One(Item::new(ItemId::new(2), 20u64))),
);
assert!(matches!(handover, Handover::Evicted(_)));
assert_eq!(tap.terminals(), vec![Terminal::Shed]);
let mut nobody: Owed<ItemId> = Owed::open([ItemId::new(1)], None);
assert!(
!nobody.settle_fate(
"stage",
Fate::GivenUp {
attempt: 1,
failure: None,
message: (),
}
),
"an obligation with no custody has nobody to answer"
);
}
#[test]
fn an_ordinary_answer_still_arrives_and_carries_no_fate() {
let (ack, answers) = ack_channel::<ItemId>();
let mut owed = Owed::open([ItemId::new(1)], Some(Custody::sole(ack)));
owed.release(ItemId::new(1));
assert!(owed.settle("consumer", Lifecycle::Processed(())));
let Answer::Settled(settled) = answers.take() else {
panic!("the consumer answered");
};
assert_eq!(settled.observation().station(), Some(Station::Processed));
assert_eq!(settled.observation().terminal(), None);
assert!(settled.message::<()>().is_none());
}