use core::time::Duration;
use std::{collections::HashSet, sync::Arc, thread, time::Instant};
use super::super::{
Ack, AckMode, AckReceiver, Answer, Delivery, Drives, Inbox, Item, ItemId, Lifecycle, Outbox,
Payload, Registries, RegistryOptions, Resident, RetireOrigin, RootState, Settled, Stamp, Wake,
ack_channel, channel,
};
use crate::{Arrival, Attributes, Context, Emitter, NodeName, Operator};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct MediaId(u64);
const MEDIA: MediaId = MediaId(7);
struct OneMedia;
impl Attributes<u64> for OneMedia {
const FAMILY: &'static str = "media";
type Root = MediaId;
fn root(&self, _record: &u64) -> MediaId {
MEDIA
}
fn observe(&self, _root: &MediaId, _worker: &NodeName, _arrival: &Arrival) {}
}
struct Doubling;
impl Operator for Doubling {
type Error = core::convert::Infallible;
type In = u64;
type Out = u64;
fn accept(
&mut self,
_ctx: &Context,
delivery: Delivery<Self::In>,
emitter: &mut Emitter<u64>,
) -> Result<(), Self::Error> {
for item in delivery.into_items() {
emitter.emit(item.into_value() * 2);
}
Ok(())
}
}
struct Fanning(u64);
impl Operator for Fanning {
type Error = core::convert::Infallible;
type In = u64;
type Out = u64;
fn accept(
&mut self,
_ctx: &Context,
delivery: Delivery<Self::In>,
emitter: &mut Emitter<u64>,
) -> Result<(), Self::Error> {
for item in delivery.into_items() {
let value = item.into_value();
for arm in 0..self.0 {
emitter.emit(value * 10 + arm);
}
}
Ok(())
}
}
fn send(out: &Outbox<ItemId, u64>, keys: &[u64]) -> AckReceiver<ItemId> {
let (ack, answers) = ack_channel();
let items = keys
.iter()
.map(|&key| Item::new(ItemId::new(key), key))
.collect();
out
.deliver(Delivery::new(Payload::Batch(items), ack))
.expect("the resident is listening");
answers
}
fn settled(answers: &AckReceiver<ItemId>, within: Duration) -> Option<Settled<ItemId>> {
let deadline = Instant::now() + within;
loop {
match answers.take() {
Answer::Settled(settled) => return Some(settled),
Answer::HungUp => return None,
Answer::Waiting if Instant::now() >= deadline => return None,
Answer::Waiting => thread::sleep(Duration::from_millis(2)),
}
}
}
fn taken(inbox: &Inbox<ItemId, u64>, within: Duration) -> Option<Delivery<u64>> {
let deadline = Instant::now() + within;
loop {
match inbox.park(Duration::from_millis(5)) {
Some(Wake::Delivered(delivery)) => return Some(delivery),
None => return None,
Some(Wake::Sweep | Wake::Tick(_)) if Instant::now() >= deadline => return None,
Some(Wake::Sweep | Wake::Tick(_)) => {}
}
}
}
fn release_all(delivery: Delivery<u64>) -> Vec<u64> {
let (payload, ack) = delivery.into_parts();
let ack = ack.expect("a delivery off the wire carries its custody token");
let released: HashSet<ItemId> = payload.keys().copied().collect();
let values = payload.into_iter().map(|item| *item.value()).collect();
ack.send(Ack::new(released, "consumer", Lifecycle::Persisted(())));
values
}
fn registries() -> Arc<Registries> {
Arc::new(Registries::new(RegistryOptions::new()))
}
fn state(registries: &Registries) -> RootState {
registries
.of::<MediaId>()
.map_or(RootState::Idle, |family| family.state(&MEDIA))
}
fn awaiting(registries: &Registries, want: u32, within: Duration) -> u32 {
let deadline = Instant::now() + within;
loop {
let seen = state(registries).inflight();
if seen == want || Instant::now() >= deadline {
return seen;
}
thread::sleep(Duration::from_millis(2));
}
}
fn analyzing_since(registries: &Registries) -> Instant {
match state(registries) {
RootState::Analyzing { since, .. } => since,
other => panic!("the key is not analyzing: {other:?}"),
}
}
#[test]
fn a_node_counts_its_output_out_before_it_answers_the_input() {
let registries = registries();
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(1);
let resident = Resident::new("double", Doubling, inbox)
.attributing(OneMedia)
.publish(out)
.with_registries(Arc::clone(®istries))
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let first = send(&feed, &[1]);
assert!(
settled(&first, Duration::from_secs(2)).is_some(),
"the first delivery had room and was answered"
);
assert_eq!(awaiting(®istries, 1, Duration::from_secs(2)), 1);
let second = send(&feed, &[2]);
assert_eq!(
awaiting(®istries, 2, Duration::from_secs(2)),
2,
"the second item is counted out while its input is still unanswered"
);
assert!(
settled(&second, Duration::from_millis(80)).is_none(),
"the ack is what has NOT been sent yet — that is the whole of R1"
);
assert!(matches!(state(®istries), RootState::Analyzing { .. }));
let held = taken(&downstream, Duration::from_secs(2)).expect("the first delivery");
assert!(
settled(&second, Duration::from_secs(2)).is_some(),
"the ack follows the dispatch that was waiting for room"
);
assert_eq!(release_all(held), [2]);
assert_eq!(awaiting(®istries, 1, Duration::from_secs(2)), 1);
let last = taken(&downstream, Duration::from_secs(2)).expect("the second delivery");
assert_eq!(release_all(last), [4]);
assert_eq!(awaiting(®istries, 0, Duration::from_secs(2)), 0);
assert_eq!(
state(®istries),
RootState::Idle,
"the last answer is what ends the analysis"
);
drop(feed);
engine.join().expect("the engine thread");
}
#[test]
fn a_chained_node_holds_its_input_while_its_outputs_are_out() {
let registries = registries();
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
let resident = Resident::new("fan", Fanning(3), inbox)
.attributing(OneMedia)
.publish(out)
.with_ack(AckMode::Chained)
.with_registries(Arc::clone(®istries))
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[1]);
let held = taken(&downstream, Duration::from_secs(2)).expect("the fan-out");
assert_eq!(
awaiting(®istries, 3, Duration::from_secs(2)),
3,
"three arms out, and the input still owed"
);
assert!(
settled(&answers, Duration::from_millis(80)).is_none(),
"the input is HELD — chained does not answer over work that is still out"
);
assert_eq!(release_all(held), [10, 11, 12]);
assert!(
settled(&answers, Duration::from_secs(2)).is_some(),
"the input is released once every arm came back"
);
assert_eq!(awaiting(®istries, 0, Duration::from_secs(2)), 0);
assert_eq!(state(®istries), RootState::Idle);
drop(feed);
engine.join().expect("the engine thread");
}
#[test]
fn the_count_never_reaches_zero_in_the_middle_of_a_tree() {
let registries = registries();
let family = registries.open::<MediaId>("media");
let (left_out, left_leaves) = channel::<ItemId, u64>(8);
let (right_out, right_leaves) = channel::<ItemId, u64>(8);
let (to_left, left_inbox) = channel::<ItemId, u64>(8);
let (to_right, right_inbox) = channel::<ItemId, u64>(8);
let (feed, head_inbox) = channel::<ItemId, u64>(8);
let left = Resident::new("left", Fanning(2), left_inbox)
.attributing(OneMedia)
.publish(left_out)
.with_registries(Arc::clone(®istries))
.with_sweep(Duration::from_millis(5));
let right = Resident::new("right", Fanning(1), right_inbox)
.attributing(OneMedia)
.publish(right_out)
.with_registries(Arc::clone(®istries))
.with_sweep(Duration::from_millis(5));
let head = Resident::new("head", Doubling, head_inbox)
.attributing(OneMedia)
.publish(to_left)
.publish(to_right)
.with_registries(Arc::clone(®istries))
.with_sweep(Duration::from_millis(5));
let engines = [
thread::spawn(move || {
let mut left = left;
left.run()
}),
thread::spawn(move || {
let mut right = right;
right.run()
}),
thread::spawn(move || {
let mut head = head;
head.run()
}),
];
let opened = Stamp::now();
let _taken_in = family.intake(&MEDIA, opened);
let _fed = family.dispatched(&MEDIA, opened);
assert_eq!(
state(®istries),
RootState::Analyzing {
inflight: 1,
since: opened.monotonic(),
},
"the fed item is out until the head answers it"
);
let fed = send(&feed, &[1]);
assert!(
settled(&fed, Duration::from_secs(5)).is_some(),
"the head answered the fed item"
);
let _answered = family.observed(&MEDIA, Stamp::now());
let left_held = taken(&left_leaves, Duration::from_secs(5)).expect("the left leaves");
let right_held = taken(&right_leaves, Duration::from_secs(5)).expect("the right leaf");
assert_eq!(
awaiting(®istries, 3, Duration::from_secs(5)),
3,
"two leaves from the left child and one from the right"
);
assert_eq!(
analyzing_since(®istries),
opened.monotonic(),
"the analysis the intake opened is the one still running — a count that had touched zero \
anywhere in the tree would have gone Idle and stamped a new one"
);
assert_eq!(release_all(right_held), [20]);
assert_eq!(awaiting(®istries, 2, Duration::from_secs(5)), 2);
assert_eq!(
analyzing_since(®istries),
opened.monotonic(),
"still the same analysis"
);
assert_eq!(release_all(left_held), [20, 21]);
assert_eq!(
awaiting(®istries, 0, Duration::from_secs(5)),
0,
"the last leaf's answer is what empties it"
);
assert_eq!(
state(®istries),
RootState::Idle,
"and zero is what ends the analysis"
);
drop(feed);
for engine in engines {
engine.join().expect("the engine thread");
}
}
#[test]
fn a_retirement_drains_against_the_work_a_node_actually_has_out() {
let registries = registries();
let family = registries.open::<MediaId>("media");
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
let resident = Resident::new("fan", Fanning(2), inbox)
.attributing(OneMedia)
.publish(out)
.with_registries(Arc::clone(®istries))
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[1]);
assert!(settled(&answers, Duration::from_secs(2)).is_some());
let held = taken(&downstream, Duration::from_secs(2)).expect("the fan-out");
assert_eq!(awaiting(®istries, 2, Duration::from_secs(2)), 2);
let now = Stamp::now();
let _moved = family.retire(&MEDIA, RetireOrigin::Vanished, now);
assert_eq!(
state(®istries),
RootState::Retiring {
inflight: 2,
since: now.monotonic(),
},
"the retirement waits on the work already out"
);
assert_eq!(release_all(held), [10, 11]);
let deadline = Instant::now() + Duration::from_secs(5);
while !matches!(state(®istries), RootState::Retired { .. }) {
assert!(Instant::now() < deadline, "{:?}", state(®istries));
thread::sleep(Duration::from_millis(2));
}
let RootState::Retired {
origin, late_tails, ..
} = state(®istries)
else {
unreachable!("the loop above just checked")
};
assert_eq!(origin, RetireOrigin::Vanished);
assert!(
!late_tails,
"the drain reached zero on its own, so nothing can land after the row"
);
drop(feed);
engine.join().expect("the engine thread");
}
#[test]
fn a_hung_up_consumer_gives_its_count_back() {
let registries = registries();
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
drop(downstream);
let resident = Resident::new("fan", Fanning(3), inbox)
.attributing(OneMedia)
.publish(out)
.with_registries(Arc::clone(®istries))
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let _answers = send(&feed, &[1]);
assert_eq!(
awaiting(®istries, 0, Duration::from_secs(5)),
0,
"the three items went nowhere and were credited back"
);
assert_eq!(state(®istries), RootState::Idle);
drop(feed);
engine.join().expect("the engine thread");
}
#[test]
fn a_node_with_no_attribution_opens_no_family() {
let registries = registries();
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
let resident = Resident::new("double", Doubling, inbox)
.publish(out)
.with_registries(Arc::clone(®istries))
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[1]);
assert!(settled(&answers, Duration::from_secs(2)).is_some());
assert_eq!(
release_all(taken(&downstream, Duration::from_secs(2)).expect("the emission")),
[2]
);
assert!(
registries.is_empty(),
"no projection means no family, and never a family keyed by the unit"
);
drop(feed);
engine.join().expect("the engine thread");
}
#[test]
fn a_resident_with_no_registries_runs_unchanged() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
let resident = Resident::new("double", Doubling, inbox)
.attributing(OneMedia)
.publish(out)
.with_sweep(Duration::from_millis(5));
assert!(resident.registries().is_none());
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[3]);
assert!(settled(&answers, Duration::from_secs(2)).is_some());
assert_eq!(
release_all(taken(&downstream, Duration::from_secs(2)).expect("the emission")),
[6]
);
drop(feed);
engine.join().expect("the engine thread");
}
#[test]
fn a_page_of_one_roots_records_counts_per_item() {
let registries = registries();
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
let resident = Resident::new("double", Doubling, inbox)
.attributing(OneMedia)
.publish(out)
.with_registries(Arc::clone(®istries))
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let _answers = send(&feed, &[1, 2, 3, 4]);
let held = taken(&downstream, Duration::from_secs(2)).expect("the batch");
assert_eq!(
awaiting(®istries, 4, Duration::from_secs(2)),
4,
"four records of one root are four items out, not one"
);
assert_eq!(release_all(held), [2, 4, 6, 8]);
assert_eq!(awaiting(®istries, 0, Duration::from_secs(2)), 0);
drop(feed);
engine.join().expect("the engine thread");
}
#[test]
fn a_record_on_two_edges_is_counted_once_per_edge() {
let registries = registries();
let (feed, inbox) = channel::<ItemId, u64>(8);
let (left_out, left) = channel::<ItemId, u64>(8);
let (right_out, right) = channel::<ItemId, u64>(8);
let resident = Resident::new("double", Doubling, inbox)
.attributing(OneMedia)
.publish(left_out)
.publish(right_out)
.with_registries(Arc::clone(®istries))
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let _answers = send(&feed, &[1]);
let left_held = taken(&left, Duration::from_secs(2)).expect("the left edge");
let right_held = taken(&right, Duration::from_secs(2)).expect("the right edge");
assert_eq!(
awaiting(®istries, 2, Duration::from_secs(2)),
2,
"one record, two edges, two items out"
);
assert_eq!(release_all(left_held), [2]);
assert_eq!(
awaiting(®istries, 1, Duration::from_secs(2)),
1,
"one consumer answered and the other's item is still out"
);
assert_eq!(release_all(right_held), [2]);
assert_eq!(awaiting(®istries, 0, Duration::from_secs(2)), 0);
drop(feed);
engine.join().expect("the engine thread");
}
#[test]
fn every_resident_counts_into_the_one_shared_family() {
let registries = registries();
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
let (second_feed, second_inbox) = channel::<ItemId, u64>(8);
let (second_out, second_downstream) = channel::<ItemId, u64>(8);
let first = Resident::new("first", Fanning(2), inbox)
.attributing(OneMedia)
.publish(out)
.with_registries(Arc::clone(®istries))
.with_sweep(Duration::from_millis(5));
let second = Resident::new("second", Fanning(3), second_inbox)
.attributing(OneMedia)
.publish(second_out)
.with_registries(Arc::clone(®istries))
.with_sweep(Duration::from_millis(5));
let engines = [
thread::spawn(move || {
let mut first = first;
first.run()
}),
thread::spawn(move || {
let mut second = second;
second.run()
}),
];
let _first = send(&feed, &[1]);
let held = taken(&downstream, Duration::from_secs(2)).expect("the first fan-out");
assert_eq!(awaiting(®istries, 2, Duration::from_secs(2)), 2);
let _second = send(&second_feed, &[1]);
let second_held = taken(&second_downstream, Duration::from_secs(2)).expect("the second fan-out");
assert_eq!(
awaiting(®istries, 5, Duration::from_secs(2)),
5,
"the second node's three add to the first node's two"
);
assert_eq!(registries.families(), 1, "one root type is one family");
release_all(held);
release_all(second_held);
assert_eq!(awaiting(®istries, 0, Duration::from_secs(5)), 0);
drop(feed);
drop(second_feed);
for engine in engines {
engine.join().expect("the engine thread");
}
}