use crate::Context;
use core::time::Duration;
use std::{
collections::{HashSet, VecDeque},
sync::{
Arc, Mutex,
atomic::{AtomicUsize, Ordering},
},
thread,
time::Instant,
};
use super::super::{
Ack, AckMode, AckReceiver, Answer, AsyncLands, Commit, Delivery, Drives, Effect, Inbox, Item,
ItemId, Lifecycle, Outbox, Payload, RecordingTap, Resident, Settled, Station, Tally, Taps,
Unpolled, Wake, ack_channel, channel,
};
use crate::{Emitter, Operator, Rings, Sink, SkipReason};
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() {
let item = item.into_value();
emitter.emit(item * 2);
}
Ok(())
}
}
struct Splitting;
impl Operator for Splitting {
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 item = item.into_value();
emitter.emit(item);
emitter.emit(item + 1_000);
}
Ok(())
}
}
struct Sieving;
impl Operator for Sieving {
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 item = item.into_value();
if item.is_multiple_of(2) {
emitter.emit(item);
} else {
emitter.skip(SkipReason::Filtered);
}
}
Ok(())
}
}
#[derive(Debug, derive_more::Display)]
#[display("item {_0} is not decodable")]
struct Undecodable(u64);
impl core::error::Error for Undecodable {}
struct Brittle(u64);
impl Operator for Brittle {
type Error = Undecodable;
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 item = item.into_value();
emitter.emit(item * 2);
if item == self.0 {
return Err(Undecodable(item));
}
}
Ok(())
}
}
#[derive(Debug, derive_more::Display)]
#[display("{_0} rows did not go in")]
struct RowsRefused(usize);
impl core::error::Error for RowsRefused {}
type Written = Arc<Mutex<Vec<u64>>>;
struct Table {
written: Written,
refusals: VecDeque<HashSet<usize>>,
arrived: Option<flume::Sender<()>>,
gate: Option<flume::Receiver<()>>,
}
impl Table {
fn new(written: Written) -> Self {
Self {
written,
refusals: VecDeque::new(),
arrived: None,
gate: None,
}
}
fn refusing(mut self, positions: impl IntoIterator<Item = usize>) -> Self {
self.refusals.push_back(positions.into_iter().collect());
self
}
fn gated(mut self, arrived: flume::Sender<()>, gate: flume::Receiver<()>) -> Self {
self.arrived = Some(arrived);
self.gate = Some(gate);
self
}
}
impl AsyncLands<u64> for Table {
type Error = RowsRefused;
async fn land(&mut self, items: &[u64]) -> Commit<Self::Error> {
if let Some(gate) = self.gate.take() {
if let Some(arrived) = self.arrived.take() {
arrived.send(()).expect("the walk is waiting");
}
gate
.recv_timeout(Duration::from_secs(20))
.expect("the gate opened");
}
let refuse = self.refusals.pop_front().unwrap_or_default();
let mut committed = Vec::new();
let mut store = self.written.lock().expect("the lane's own store");
for (position, item) in items.iter().enumerate() {
if refuse.contains(&position) {
continue;
}
store.push(*item);
committed.push(position);
}
if refuse.is_empty() {
Commit::all()
} else {
Commit::only(committed, RowsRefused(refuse.len()))
}
}
}
struct Fanning {
returned: Arc<AtomicUsize>,
}
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 item = item.into_value();
emitter.emit(item);
emitter.emit(item + 1);
emitter.emit(item + 2);
}
self.returned.fetch_add(1, Ordering::SeqCst);
Ok(())
}
}
struct Frugal;
impl Operator for Frugal {
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() {
if emitter.wanted() {
emitter.emit(item.into_value() * 2);
} else {
emitter.skip(SkipReason::custom("unwanted"));
}
}
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_as(worker: &str, 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, worker, Lifecycle::Persisted(())));
values
}
fn release_all(delivery: Delivery<u64>) -> Vec<u64> {
release_all_as("consumer", delivery)
}
#[test]
fn the_thread_engine_runs_a_unit_from_launch_to_shutdown() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
let resident = Resident::new("double", Doubling, inbox)
.publish(out)
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let first = send(&feed, &[1, 2, 3]);
let delivery = taken(&downstream, Duration::from_secs(2)).expect("the outputs arrived");
assert_eq!(release_all(delivery), [2, 4, 6]);
let answer = settled(&first, Duration::from_secs(2)).expect("the input was answered");
assert_eq!(answer.released().len(), 3);
assert_eq!(answer.observation().station(), Some(Station::Processed));
let tally = answer
.observation()
.info::<Tally>()
.expect("the shipped loop's own info type");
assert_eq!(tally.processed(), 3);
assert_eq!(tally.emitted(), 3);
drop(feed);
let report = engine.join().expect("the engine thread");
assert_eq!(report.deliveries(), 1);
assert_eq!(report.items(), 3);
assert_eq!(report.tally().processed(), 3);
}
#[test]
fn a_shutdown_drains_what_is_already_queued() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(64);
let mut answers = Vec::new();
for keys in [&[1u64][..], &[2][..], &[3][..]] {
answers.push(send(&feed, keys));
}
drop(feed);
let mut resident = Resident::new("double", Doubling, inbox)
.publish(out)
.with_sweep(Duration::from_millis(5));
let report = resident.run();
assert_eq!(report.deliveries(), 3);
assert_eq!(report.items(), 3);
let mut values = Vec::new();
for wake in downstream.drain() {
if let Wake::Delivered(delivery) = wake {
values.extend(release_all(delivery));
}
}
assert_eq!(values, [2, 4, 6]);
}
#[tokio::test]
async fn the_async_engine_is_the_same_loop_over_the_other_park() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
let mut resident = Resident::new("double", Doubling, inbox).publish(out);
let engine = tokio::spawn(async move { resident.run_async().await });
let answers = send(&feed, &[5]);
let delivery = tokio::task::spawn_blocking(move || {
taken(&downstream, Duration::from_secs(2)).map(release_all)
})
.await
.expect("the blocking take");
assert_eq!(delivery, Some(vec![10]));
let answers =
tokio::task::spawn_blocking(move || settled(&answers, Duration::from_secs(2)).is_some())
.await
.expect("the blocking wait");
assert!(answers);
drop(feed);
let report = engine.await.expect("the engine task");
assert_eq!(report.items(), 1);
}
#[test]
fn local_release_does_not_wait_for_the_downstream_answer() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
let resident = Resident::new("double", Doubling, inbox)
.publish(out)
.with_ack(AckMode::Local)
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[1, 2]);
let answer = settled(&answers, Duration::from_secs(2)).expect("released without waiting");
assert_eq!(answer.released().len(), 2);
let delivery = taken(&downstream, Duration::from_secs(2)).expect("the outputs are there");
assert_eq!(release_all(delivery), [2, 4]);
drop(feed);
engine.join().expect("the engine thread");
}
#[test]
fn chained_withholds_the_input_until_its_output_comes_back() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
let resident = Resident::new("double", Doubling, inbox)
.publish(out)
.with_ack(AckMode::Chained)
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[1, 2]);
let delivery = taken(&downstream, Duration::from_secs(2)).expect("the outputs arrived");
assert!(
settled(&answers, Duration::from_millis(60)).is_none(),
"the input is owed until its output is released"
);
assert_eq!(release_all(delivery), [2, 4]);
let answer = settled(&answers, Duration::from_secs(2)).expect("the join completed");
assert_eq!(answer.released().len(), 2);
drop(feed);
let report = engine.join().expect("the engine thread");
assert_eq!(report.items(), 2);
}
#[test]
fn a_chained_fan_out_joins_on_every_arm() {
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)
.publish(left_out)
.publish(right_out)
.with_ack(AckMode::Chained)
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[3]);
let left_delivery = taken(&left, Duration::from_secs(2)).expect("the left arm");
let right_delivery = taken(&right, Duration::from_secs(2)).expect("the right arm");
assert_eq!(release_all(left_delivery), [6]);
assert!(
settled(&answers, Duration::from_millis(60)).is_none(),
"one arm answered is not the join"
);
assert_eq!(release_all(right_delivery), [6]);
assert!(
settled(&answers, Duration::from_secs(2)).is_some(),
"both arms answered completes it"
);
drop(feed);
engine.join().expect("the engine thread");
}
#[test]
fn a_chained_unit_that_produced_nothing_releases_at_once() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
let resident = Resident::new("sieve", Sieving, inbox)
.publish(out)
.with_ack(AckMode::Chained)
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[1, 3, 5]);
let answer = settled(&answers, Duration::from_secs(2)).expect("nothing to wait for");
assert_eq!(answer.released().len(), 3);
assert_eq!(answer.observation().station(), Some(Station::Skipped));
assert!(
answer
.observation()
.skip()
.expect("skipped")
.reason()
.is_filtered()
);
drop(feed);
engine.join().expect("the engine thread");
assert!(downstream.is_empty());
}
#[test]
fn a_failed_item_holds_the_window_and_its_neighbours_do_not() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
let resident = Resident::new("brittle", Brittle(2), inbox)
.publish(out)
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[1, 2, 3]);
let answer = settled(&answers, Duration::from_secs(2)).expect("answered");
assert_eq!(answer.released().len(), 2, "the failed item is held");
assert!(!answer.released().contains(&ItemId::new(2)));
assert_eq!(answer.observation().station(), Some(Station::Failed));
let failure = answer.observation().failure().expect("a failure");
assert_eq!(failure.to_string(), "item 2 is not decodable");
let delivery = taken(&downstream, Duration::from_secs(2)).expect("the outputs arrived");
assert_eq!(release_all(delivery), [2, 4, 6]);
drop(feed);
let report = engine.join().expect("the engine thread");
assert_eq!(report.tally().failed(), 1);
assert_eq!(report.tally().processed(), 2);
}
#[test]
fn an_emission_with_no_consumer_is_recorded_rather_than_dropped_in_silence() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let resident = Resident::new("double", Doubling, inbox)
.with_effect(Effect::Pure)
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[1]);
let answer = settled(&answers, Duration::from_secs(2)).expect("answered");
assert_eq!(answer.observation().station(), Some(Station::Skipped));
assert!(
answer
.observation()
.skip()
.expect("skipped")
.reason()
.is_no_consumers()
);
drop(feed);
engine.join().expect("the engine thread");
}
#[test]
fn a_storing_unit_says_persisted_and_a_pure_one_says_processed() {
for (effect, station) in [
(Effect::Lands, Station::Persisted),
(Effect::Pure, Station::Processed),
] {
let (feed, inbox) = channel::<ItemId, u64>(4);
let (out, downstream) = channel::<ItemId, u64>(4);
let resident = Resident::new("node", Doubling, inbox)
.publish(out)
.with_effect(effect)
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[1]);
let answer = settled(&answers, Duration::from_secs(2)).expect("answered");
assert_eq!(answer.observation().station(), Some(station));
drop(feed);
engine.join().expect("the engine thread");
drop(downstream);
}
}
struct TitleDownstream {
titles: Option<Outbox<ItemId, u64>>,
routed: Arc<Mutex<Vec<ItemId>>>,
}
impl Sink<u64> for TitleDownstream {
fn arity(&self) -> usize {
usize::from(self.titles.is_some())
}
fn nudges(&self) -> Vec<Box<dyn crate::Rings>> {
self
.titles
.iter()
.map(|edge| Box::new(edge.nudge()) as Box<dyn crate::Rings>)
.collect()
}
async fn deliver(
&mut self,
_edge: usize,
batch: Vec<Item<u64>>,
ack: crate::AckSender<ItemId>,
) -> bool {
let Some(titles) = self.titles.as_ref() else {
return false;
};
self
.routed
.lock()
.expect("the routed log")
.extend(batch.iter().map(|item| *item.key()));
titles
.deliver_async(Delivery::new(Payload::Batch(batch), ack))
.await
.is_ok()
}
}
#[test]
fn a_node_can_bring_its_own_downstream_set() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
let routed: Arc<Mutex<Vec<ItemId>>> = Arc::default();
let resident = Resident::new("double", Doubling, inbox)
.wire(TitleDownstream {
titles: Some(out),
routed: routed.clone(),
})
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[1, 2]);
let delivery = taken(&downstream, Duration::from_secs(2)).expect("the outputs arrived");
assert_eq!(release_all(delivery), [2, 4]);
let answer = settled(&answers, Duration::from_secs(2)).expect("the input was answered");
assert_eq!(answer.released().len(), 2);
assert_eq!(
routed.lock().expect("the routed log").len(),
2,
"the driver routed through the hand-written sink and not around it"
);
drop(feed);
engine.join().expect("the engine thread");
}
#[test]
fn emitting_stages_and_the_drain_is_where_the_wait_lands() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(0);
let returned: Arc<AtomicUsize> = Arc::default();
let resident = Resident::new(
"fan",
Fanning {
returned: returned.clone(),
},
inbox,
)
.publish(out)
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[1]);
let deadline = Instant::now() + Duration::from_secs(5);
while returned.load(Ordering::SeqCst) == 0 {
assert!(Instant::now() < deadline, "the call never returned");
thread::sleep(Duration::from_millis(2));
}
assert!(
settled(&answers, Duration::from_millis(50)).is_none(),
"the driver is still handing the batch over, so nothing is answered yet"
);
let delivery = taken(&downstream, Duration::from_secs(5)).expect("the outputs arrived");
assert_eq!(release_all(delivery), [1, 2, 3]);
let answer = settled(&answers, Duration::from_secs(5)).expect("answered once the edge took it");
assert_eq!(answer.released().len(), 1);
drop(feed);
engine.join().expect("the engine thread");
}
#[test]
fn an_operator_can_read_whether_anything_downstream_wants_its_product() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
let resident = Resident::new("frugal", Frugal, inbox)
.publish(out)
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[2]);
let delivery = taken(&downstream, Duration::from_secs(2)).expect("the outputs arrived");
assert_eq!(release_all(delivery), [4]);
assert_eq!(
settled(&answers, Duration::from_secs(2))
.expect("answered")
.observation()
.station(),
Some(Station::Processed)
);
drop(feed);
engine.join().expect("the engine thread");
let (feed, inbox) = channel::<ItemId, u64>(8);
let resident = Resident::new("frugal", Frugal, inbox).with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[2]);
let answer = settled(&answers, Duration::from_secs(2)).expect("answered");
assert_eq!(answer.observation().station(), Some(Station::Skipped));
assert_eq!(
answer
.observation()
.skip()
.expect("skipped")
.reason()
.as_str(),
"unwanted"
);
drop(feed);
engine.join().expect("the engine thread");
}
#[test]
fn a_burst_of_sweeps_is_one_look() {
let (feed, inbox) = channel::<ItemId, u64>(256);
let (out, downstream) = channel::<ItemId, u64>(256);
let nudge = feed.nudge();
for _ in 0..100 {
nudge.ring();
}
let answers = send(&feed, &[1]);
drop(feed);
drop(nudge);
let mut resident = Resident::new("double", Doubling, inbox)
.publish(out)
.with_sweep(Duration::from_millis(5));
let report = resident.run();
assert_eq!(report.deliveries(), 1);
assert!(
report.sweeps() <= 2,
"a burst collapsed, saw {}",
report.sweeps()
);
assert!(settled(&answers, Duration::from_millis(50)).is_some());
drop(downstream);
}
#[test]
fn every_arm_of_a_fan_out_gets_its_own_item_id() {
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("split", Splitting, inbox)
.publish(left_out)
.publish(right_out)
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let _answers = send(&feed, &[1]);
let left_delivery = taken(&left, Duration::from_secs(2)).expect("left");
let right_delivery = taken(&right, Duration::from_secs(2)).expect("right");
let left_keys: Vec<ItemId> = left_delivery.payload().keys().copied().collect();
let right_keys: Vec<ItemId> = right_delivery.payload().keys().copied().collect();
assert_eq!(left_keys.len(), 2, "one item per emission, two emissions");
assert_eq!(right_keys.len(), 2);
for key in &left_keys {
assert!(!right_keys.contains(key), "{key} is on both edges");
}
assert_eq!(release_all(left_delivery), [1, 1_001]);
assert_eq!(release_all(right_delivery), [1, 1_001]);
drop(feed);
engine.join().expect("the engine thread");
}
#[test]
fn a_consumer_that_hangs_up_is_observed_and_never_stalls_the_unit() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(1);
drop(downstream);
let tap = Arc::new(RecordingTap::new());
let resident = Resident::new("double", Doubling, inbox)
.publish(out)
.with_ack(AckMode::Chained)
.with_taps(Taps::new().with(tap.clone()))
.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_millis(80)).is_none());
drop(feed);
let report = engine.join().expect("the engine thread");
assert_eq!(report.items(), 1);
assert!(
tap
.seen()
.iter()
.any(|(_, station)| *station == Station::Skipped),
"the refusal reached a tap: {:?}",
tap.seen()
);
}
#[test]
fn a_storing_unit_answers_only_after_its_own_write_commits() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let written: Written = Arc::new(Mutex::new(Vec::new()));
let (report_arrival, arrived) = flume::bounded::<()>(1);
let (open_gate, gate) = flume::bounded::<()>(1);
let resident = Resident::new("titles", Doubling, inbox)
.land(Table::new(written.clone()).gated(report_arrival, gate))
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[1, 2]);
arrived
.recv_timeout(Duration::from_secs(20))
.expect("the lane was called");
assert!(
written.lock().expect("the store").is_empty(),
"the write has not happened yet"
);
assert!(
settled(&answers, Duration::from_millis(60)).is_none(),
"answered before its own write"
);
open_gate.send(()).expect("the lane is waiting");
let answer = settled(&answers, Duration::from_secs(2)).expect("answered after the write");
assert_eq!(answer.released().len(), 2);
assert_eq!(answer.observation().station(), Some(Station::Persisted));
assert_eq!(*written.lock().expect("the store"), [2, 4]);
drop(feed);
engine.join().expect("the engine thread");
}
#[test]
fn a_storing_unit_at_the_end_of_a_lane_is_persisted_and_not_a_refusal() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let written: Written = Arc::new(Mutex::new(Vec::new()));
let resident = Resident::new("titles", Doubling, inbox)
.land(Table::new(written.clone()))
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[1, 2]);
let answer = settled(&answers, Duration::from_secs(2)).expect("answered");
assert_eq!(answer.observation().station(), Some(Station::Persisted));
assert!(answer.observation().skip().is_none());
assert_eq!(answer.released().len(), 2);
assert_eq!(*written.lock().expect("the store"), [2, 4]);
drop(feed);
let report = engine.join().expect("the engine thread");
assert_eq!(report.tally().emitted(), 0, "nothing went on a wire");
assert_eq!(report.tally().refused(), 0);
}
const REFUSED: [usize; 6] = [3, 17, 100, 200, 249, 255];
#[test]
fn a_batch_that_lands_in_part_releases_exactly_what_the_store_took() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let written: Written = Arc::new(Mutex::new(Vec::new()));
let resident = Resident::new("titles", Doubling, inbox)
.land(Table::new(written.clone()).refusing(REFUSED))
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let keys: Vec<u64> = (1..=256).collect();
let answers = send(&feed, &keys);
let answer = settled(&answers, Duration::from_secs(2)).expect("answered");
assert_eq!(answer.released().len(), 250);
for position in REFUSED {
let held = ItemId::new(keys[position]);
assert!(
!answer.released().contains(&held),
"{held} was released with nothing written for it"
);
}
assert_eq!(written.lock().expect("the store").len(), 250);
assert_eq!(answer.observation().station(), Some(Station::Failed));
assert_eq!(
answer
.observation()
.failure()
.expect("a failure")
.to_string(),
"the write lane committed 250 of 256: 6 rows did not go in"
);
drop(feed);
let report = engine.join().expect("the engine thread");
assert_eq!(report.tally().refused(), 6);
assert_eq!(report.tally().failed(), 0, "every operator call succeeded");
}
#[test]
fn a_record_the_store_refused_is_not_emitted_and_its_input_comes_back() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
let written: Written = Arc::new(Mutex::new(Vec::new()));
let resident = Resident::new("titles", Doubling, inbox)
.publish(out)
.land(Table::new(written.clone()).refusing([1]))
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[1, 2]);
let answer = settled(&answers, Duration::from_secs(2)).expect("answered");
assert_eq!(answer.released().len(), 1);
assert!(answer.released().contains(&ItemId::new(1)));
let delivery = taken(&downstream, Duration::from_secs(2)).expect("the committed output");
assert_eq!(release_all(delivery), [2], "only what the store took");
let again = send(&feed, &[2]);
let answer = settled(&again, Duration::from_secs(2)).expect("the re-delivery was answered");
assert_eq!(answer.released().len(), 1);
assert_eq!(answer.observation().station(), Some(Station::Persisted));
let delivery = taken(&downstream, Duration::from_secs(2)).expect("the second output");
assert_eq!(release_all(delivery), [4]);
assert_eq!(*written.lock().expect("the store"), [2, 4]);
drop(feed);
engine.join().expect("the engine thread");
}
#[tokio::test]
async fn the_async_engine_awaits_the_write_the_other_one_blocks_on() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let written: Written = Arc::new(Mutex::new(Vec::new()));
let mut resident = Resident::new("titles", Doubling, inbox).land(Table::new(written.clone()));
let engine = tokio::spawn(async move { resident.run_async().await });
let answers = send(&feed, &[5]);
let answer = tokio::task::spawn_blocking(move || settled(&answers, Duration::from_secs(2)))
.await
.expect("the blocking wait")
.expect("answered");
assert_eq!(answer.observation().station(), Some(Station::Persisted));
assert_eq!(*written.lock().expect("the store"), [10]);
drop(feed);
let report = engine.await.expect("the engine task");
assert_eq!(report.items(), 1);
}
#[test]
fn the_tap_names_the_unit_whose_work_the_answer_describes() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
let tap = Arc::new(RecordingTap::new());
let resident = Resident::new("double", Doubling, inbox)
.publish(out)
.with_taps(Taps::new().with(tap.clone()))
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let _answers = send(&feed, &[1, 2]);
let delivery = taken(&downstream, Duration::from_secs(2)).expect("outputs");
release_all_as("keeper", delivery);
let deadline = Instant::now() + Duration::from_secs(2);
while tap.is_empty() && Instant::now() < deadline {
thread::sleep(Duration::from_millis(5));
}
assert_eq!(tap.seen(), vec![("keeper".to_owned(), Station::Persisted)]);
drop(feed);
engine.join().expect("the engine thread");
}
#[test]
fn the_station_a_verdict_reports_is_the_one_its_lifecycle_reaches() {
use super::{super::Effect, Verdict};
type Shape = (&'static str, bool, bool, usize);
fn build((_, failed, skipped, emitted): Shape) -> Verdict {
Verdict {
tally: Tally {
emitted,
..Tally::default()
},
failure: failed.then(|| super::super::Failure::new(std::io::Error::other("no"))),
skip: skipped.then(|| crate::Skip::new(SkipReason::Filtered)),
}
}
let shapes: [Shape; 5] = [
("plain", false, false, 0),
("failed", true, false, 0),
("skipped", false, true, 0),
("skipped but emitted", false, true, 1),
("failed and skipped", true, true, 0),
];
for shape in shapes {
for effect in [Effect::Lands, Effect::Pure] {
let reported = build(shape).station(effect);
let reached = build(shape).into_lifecycle(effect).station();
assert_eq!(reported, reached, "{} at {effect}", shape.0);
}
}
}
mod sweeping {
use crate::{Context, drive::Drives};
use core::time::Duration;
use std::{
sync::{Arc, Mutex},
thread,
time::Instant,
};
use super::super::super::{
Drained, Effect, ItemId, LifecycleTap, NodeName, Observation, PollUpstream, RecordingTap,
Resident, Rings, Schedule, Seats, Station, Sweeper, Swept, Taps, channel,
};
use crate::{Delivery, Emitter, Operator};
#[derive(Debug, Default)]
struct Counting {
seen: Mutex<Vec<(Station, Option<usize>)>>,
}
impl Counting {
fn seen(&self) -> Vec<(Station, Option<usize>)> {
self.seen.lock().expect("the counting tap").clone()
}
}
impl LifecycleTap for Counting {
fn observe(&self, _unit: &NodeName, observation: &Observation) {
let Some(arrival) = observation.arrival() else {
return;
};
self
.seen
.lock()
.expect("the counting tap")
.push((arrival.station(), arrival.info::<usize>().copied()));
}
}
struct Granted {
rows: Arc<Mutex<Vec<u64>>>,
}
#[derive(Clone)]
struct Watched {
looked: Arc<Mutex<Vec<PollUpstream>>>,
rows: Arc<Mutex<Vec<u64>>>,
}
impl Watched {
fn new(rows: &[u64]) -> Self {
Self {
looked: Arc::new(Mutex::new(Vec::new())),
rows: Arc::new(Mutex::new(rows.to_vec())),
}
}
fn looks(&self, upstream: PollUpstream) -> usize {
self
.looked
.lock()
.expect("the walk's own record")
.iter()
.filter(|&&seen| seen == upstream)
.count()
}
fn rows(&self) -> Vec<u64> {
self.rows.lock().expect("the store").clone()
}
}
struct Reaping {
looked: Arc<Mutex<Vec<PollUpstream>>>,
brittle: Option<usize>,
ticks: usize,
consumed: Arc<Mutex<Vec<u64>>>,
}
impl Reaping {
fn new(watched: &Watched) -> Self {
Self {
looked: watched.looked.clone(),
brittle: None,
ticks: 0,
consumed: Arc::new(Mutex::new(Vec::new())),
}
}
fn failing_on(mut self, tick: usize) -> Self {
self.brittle = Some(tick);
self
}
fn consumed(&self) -> Arc<Mutex<Vec<u64>>> {
self.consumed.clone()
}
}
#[derive(Debug, derive_more::Display)]
#[display("the store would not answer")]
struct Unreachable;
impl core::error::Error for Unreachable {}
impl Operator for Reaping {
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 item = item.into_value();
self.consumed.lock().expect("the walk's record").push(item);
emitter.emit(item);
}
Ok(())
}
}
impl Seats<Reaping> for Granted {
type Error = Unreachable;
type Info = usize;
async fn seat(
&mut self,
operator: &mut Reaping,
upstream: PollUpstream,
) -> Result<Swept<usize>, Self::Error> {
let mine = operator.ticks;
operator.ticks += 1;
operator
.looked
.lock()
.expect("the walk's record")
.push(upstream);
if operator.brittle == Some(mine) {
return Err(Unreachable);
}
let mut rows = self.rows.lock().expect("the granted store");
if upstream == PollUpstream::new(0) {
let before = rows.len();
rows.retain(|row| row % 2 == 0);
return Ok(Swept::Ran(before - rows.len()));
}
Ok(Swept::Ran(0))
}
}
fn until(within: Duration, what: &str, mut check: impl FnMut() -> bool) {
let deadline = Instant::now() + within;
while !check() {
assert!(Instant::now() < deadline, "{what}");
thread::sleep(Duration::from_millis(2));
}
}
fn schedule() -> Schedule {
Schedule::new()
.every(Duration::from_millis(5))
.every(Duration::from_millis(60))
}
#[test]
fn a_tick_invokes_the_sweep_entry_naming_the_source_that_came_due() {
let watched = Watched::new(&[1, 2, 3, 4, 5, 6]);
let (out, inbox) = channel::<ItemId, u64>(8);
let rows = watched.rows.clone();
let resident = Resident::new("reaper", Reaping::new(&watched), inbox)
.polling(schedule(), Granted { rows })
.with_sweep(Duration::from_secs(30));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
until(
Duration::from_secs(20),
"the two clocks never both ran",
|| watched.looks(PollUpstream::new(0)) >= 8 && watched.looks(PollUpstream::new(1)) >= 1,
);
drop(out);
let report: Drained = engine.join().expect("the engine thread");
let fast = watched.looks(PollUpstream::new(0));
let slow = watched.looks(PollUpstream::new(1));
assert!(
fast > slow,
"each upstream keeps its own cadence: {fast}/{slow}"
);
assert_eq!(
report.ticks(),
fast + slow,
"one look per upstream per pass"
);
assert_eq!(report.deliveries(), 0, "no data reached this node at all");
assert_eq!(watched.rows(), [2, 4, 6]);
}
#[test]
fn a_sweep_reports_through_the_same_taps_at_the_units_own_effect_and_says_what_it_removed() {
let watched = Watched::new(&[1, 2, 3, 4]);
let tap = Arc::new(RecordingTap::new());
let counting = Arc::new(Counting::default());
let (out, inbox) = channel::<ItemId, u64>(8);
let rows = watched.rows.clone();
let resident = Resident::new("reaper", Reaping::new(&watched), inbox)
.polling(
Schedule::new().every(Duration::from_millis(5)),
Granted { rows },
)
.with_taps(Taps::new().with(tap.clone()).with(counting.clone()))
.with_sweep(Duration::from_secs(30));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
until(Duration::from_secs(20), "no sweep was observed", || {
tap.len() >= 2
});
drop(out);
engine.join().expect("the engine thread");
assert!(
tap
.seen()
.iter()
.all(|(node, station)| node == "reaper" && *station == Station::Processed),
"{:?}",
tap.seen()
);
let seen = counting.seen();
assert_eq!(
seen[0],
(Station::Processed, Some(2)),
"the first look removed both odd rows"
);
assert!(
seen[1..]
.iter()
.all(|&(station, removed)| station == Station::Processed && removed == Some(0)),
"and every look after it found nothing left: {seen:?}"
);
}
#[test]
fn a_storing_poll_unit_reaches_the_station_its_effect_names() {
let watched = Watched::new(&[1, 2, 3]);
let tap = Arc::new(RecordingTap::new());
let (out, inbox) = channel::<ItemId, u64>(8);
let rows = watched.rows.clone();
let resident = Resident::new("compactor", Reaping::new(&watched), inbox)
.polling(
Schedule::new().every(Duration::from_millis(5)),
Granted { rows },
)
.with_effect(Effect::Lands)
.with_taps(Taps::new().with(tap.clone()))
.with_sweep(Duration::from_secs(30));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
until(Duration::from_secs(20), "no sweep was observed", || {
!tap.is_empty()
});
drop(out);
engine.join().expect("the engine thread");
assert!(
tap
.seen()
.iter()
.all(|(_, station)| *station == Station::Persisted),
"{:?}",
tap.seen()
);
}
#[test]
fn a_refused_sweep_is_observed_and_the_next_tick_still_comes() {
let watched = Watched::new(&[1, 2, 3, 4]);
let tap = Arc::new(RecordingTap::new());
let (out, inbox) = channel::<ItemId, u64>(8);
let rows = watched.rows.clone();
let resident = Resident::new("reaper", Reaping::new(&watched).failing_on(0), inbox)
.polling(
Schedule::new().every(Duration::from_millis(5)),
Granted { rows },
)
.with_taps(Taps::new().with(tap.clone()))
.with_sweep(Duration::from_secs(30));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
until(
Duration::from_secs(20),
"the clock stopped at the failure",
|| tap.len() >= 3,
);
drop(out);
engine.join().expect("the engine thread");
let stations: Vec<Station> = tap.seen().iter().map(|(_, station)| *station).collect();
assert_eq!(stations[0], Station::Failed, "the first look refused");
assert!(
stations[1..].iter().all(|s| *s == Station::Processed),
"the clock carried on: {stations:?}"
);
assert_eq!(watched.rows(), [2, 4]);
}
#[test]
fn a_unit_reading_a_channel_and_a_clock_at_once_serves_both() {
let watched = Watched::new(&[1, 2, 3, 4]);
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
let node = Reaping::new(&watched);
let consumed = node.consumed();
let rows = watched.rows.clone();
let resident = Resident::new("reaper", node, inbox)
.publish(out)
.polling(
Schedule::new().every(Duration::from_millis(5)),
Granted { rows },
)
.with_sweep(Duration::from_secs(30));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = super::send(&feed, &[7, 8]);
let delivery = super::taken(&downstream, Duration::from_secs(20)).expect("the data arm ran");
assert_eq!(super::release_all(delivery), [7, 8]);
assert!(
super::settled(&answers, Duration::from_secs(20)).is_some(),
"the channel upstream was answered"
);
until(Duration::from_secs(20), "the clock arm never ran", || {
watched.looks(PollUpstream::new(0)) >= 2
});
drop(feed);
let report = engine.join().expect("the engine thread");
assert_eq!(
consumed.lock().expect("the walk's record").as_slice(),
[7, 8]
);
assert_eq!(report.items(), 2);
assert!(report.ticks() >= 2, "both arms ran: {}", report.ticks());
}
#[tokio::test]
async fn the_async_engine_is_rung_for_its_ticks_as_it_is_for_its_sweep() {
let watched = Watched::new(&[1, 2, 3, 4, 5, 6]);
let (out, inbox) = channel::<ItemId, u64>(8);
let rows = watched.rows.clone();
let schedule = schedule();
let sweeper = Sweeper::ticking(schedule.cadences(&out));
let mut resident =
Resident::new("reaper", Reaping::new(&watched), inbox).polling(schedule, Granted { rows });
let seen = watched.clone();
let engine = tokio::spawn(async move { resident.run_async().await });
drop(out);
tokio::task::spawn_blocking(move || {
until(
Duration::from_secs(20),
"the rung clocks never both ran",
|| seen.looks(PollUpstream::new(0)) >= 8 && seen.looks(PollUpstream::new(1)) >= 1,
);
})
.await
.expect("the waiter");
drop(sweeper);
let report = engine.await.expect("the engine task");
let fast = watched.looks(PollUpstream::new(0));
let slow = watched.looks(PollUpstream::new(1));
assert!(
fast > slow,
"each upstream keeps its own cadence: {fast}/{slow}"
);
assert_eq!(report.ticks(), fast + slow);
assert_eq!(watched.rows(), [2, 4, 6]);
}
#[test]
fn two_ticks_for_one_source_in_one_pass_are_one_look() {
let watched = Watched::new(&[1, 2, 3, 4]);
let (out, inbox) = channel::<ItemId, u64>(8);
let first = out.ticker(PollUpstream::new(0));
let second = out.ticker(PollUpstream::new(0));
assert!(first.ring());
assert!(second.ring());
assert_eq!(inbox.len(), 2, "two ticks are queued for one upstream");
let rows = watched.rows.clone();
let mut resident = Resident::new("reaper", Reaping::new(&watched), inbox)
.polling(Schedule::new(), Granted { rows })
.with_sweep(Duration::from_millis(5));
drop((out, first, second));
let report = resident.run();
assert_eq!(report.ticks(), 1, "one look at one store");
assert_eq!(watched.looks(PollUpstream::new(0)), 1);
}
}
#[test]
fn the_two_halves_of_a_knob_agree_and_every_wired_part_reads_back() {
let (_feed, inbox) = channel::<ItemId, u64>(8);
let fluent = Resident::new("double", Doubling, inbox)
.with_ack(AckMode::Chained)
.with_effect(Effect::Lands)
.with_sweep(Duration::from_millis(7));
assert_eq!(fluent.name().as_str(), "double");
assert_eq!(fluent.ack(), AckMode::Chained);
assert_eq!(fluent.effect(), Effect::Lands);
assert_eq!(fluent.sweep(), Duration::from_millis(7));
let (_second, inbox) = channel::<ItemId, u64>(8);
let mut in_place = Resident::new("double", Doubling, inbox);
in_place
.set_ack(AckMode::Chained)
.set_effect(Effect::Lands)
.set_sweep(Duration::from_millis(7));
assert_eq!(in_place.ack(), fluent.ack());
assert_eq!(in_place.effect(), fluent.effect());
assert_eq!(in_place.sweep(), fluent.sweep());
let (_third, inbox) = channel::<ItemId, u64>(8);
let plain = Resident::new("plain", Doubling, inbox);
assert_eq!(plain.ack(), AckMode::Local);
assert_eq!(plain.effect(), Effect::Pure);
assert_eq!(plain.sweep(), Resident::<Doubling, ItemId, ()>::SWEEP);
assert!(plain.schedule().is_empty());
assert!(plain.taps().is_empty());
assert_eq!(plain.context(), &Unpolled);
assert_eq!(plain.lane(), &());
assert_eq!(plain.attribution(), &());
assert_eq!(plain.ledger(), &());
assert_eq!(plain.outstanding(), 0);
assert_eq!(plain.holding(), 0);
let (_fourth, inbox) = channel::<ItemId, u64>(8);
let written: Written = Arc::default();
let storing = Resident::new("store", Doubling, inbox).land(Table::new(written.clone()));
assert_eq!(
storing.effect(),
Effect::Lands,
"a lane declares its effect"
);
assert!(
storing
.lane()
.written
.lock()
.expect("the lane's store")
.is_empty(),
"the lane wired is the lane read back"
);
let (_fifth, inbox) = channel::<ItemId, u64>(8);
let tapped = Resident::new("tapped", Doubling, inbox)
.with_taps(Taps::new().with(Arc::new(RecordingTap::default())));
assert_eq!(tapped.taps().len(), 1);
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct SceneId(u64);
struct Seating;
impl Operator for Seating {
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();
emitter.emit(value);
emitter.receipt(SceneId(value * 10 + 7));
}
Ok(())
}
}
#[test]
fn a_staged_receipt_travels_back_on_the_ack_keyed_by_its_own_item() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
let resident = Resident::new("seat", Seating, inbox)
.publish(out)
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[1, 2]);
release_all(taken(&downstream, Duration::from_secs(2)).expect("the outputs arrived"));
let answer = settled(&answers, Duration::from_secs(2)).expect("answered");
assert_eq!(answer.receipt::<SceneId>(&ItemId::new(1)), Ok(&SceneId(17)));
assert_eq!(answer.receipt::<SceneId>(&ItemId::new(2)), Ok(&SceneId(27)));
assert_eq!(answer.released().len(), 2);
drop(feed);
engine.join().expect("the engine thread");
}
#[test]
fn a_unit_that_states_no_receipt_answers_exactly_as_it_always_did() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
let resident = Resident::new("double", Doubling, inbox)
.publish(out)
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[4]);
release_all(taken(&downstream, Duration::from_secs(2)).expect("the outputs arrived"));
let answer = settled(&answers, Duration::from_secs(2)).expect("answered");
assert!(!answer.has_receipt(&ItemId::new(4)));
assert_eq!(
answer.receipt::<SceneId>(&ItemId::new(4)),
Err(crate::Unreceipted::Silent)
);
assert_eq!(answer.released().len(), 1);
assert_eq!(answer.observation().station(), Some(Station::Processed));
drop(feed);
engine.join().expect("the engine thread");
}
#[test]
fn a_receipt_does_not_reach_its_producer_until_the_row_has_landed() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
let written: Written = Arc::new(Mutex::new(Vec::new()));
let (report_arrival, arrived) = flume::bounded::<()>(1);
let (open_gate, gate) = flume::bounded::<()>(1);
let resident = Resident::new("seat", Seating, inbox)
.publish(out)
.land(Table::new(written.clone()).gated(report_arrival, gate))
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[3]);
arrived
.recv_timeout(Duration::from_secs(20))
.expect("the lane was called");
assert!(
written.lock().expect("the store").is_empty(),
"the write has not happened yet"
);
assert!(
settled(&answers, Duration::from_millis(60)).is_none(),
"the receipt reached its producer before the row landed"
);
open_gate.send(()).expect("the lane is waiting");
release_all(taken(&downstream, Duration::from_secs(2)).expect("the outputs arrived"));
let answer = settled(&answers, Duration::from_secs(2)).expect("answered after the write");
assert_eq!(*written.lock().expect("the store"), [3]);
assert_eq!(answer.receipt::<SceneId>(&ItemId::new(3)), Ok(&SceneId(37)));
assert_eq!(answer.observation().station(), Some(Station::Persisted));
drop(feed);
engine.join().expect("the engine thread");
}
#[test]
fn a_chained_unit_carries_its_receipt_across_the_pass_that_holds_it() {
let (feed, inbox) = channel::<ItemId, u64>(8);
let (out, downstream) = channel::<ItemId, u64>(8);
let resident = Resident::new("seat", Seating, inbox)
.publish(out)
.with_ack(AckMode::Chained)
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let answers = send(&feed, &[5]);
let delivery = taken(&downstream, Duration::from_secs(2)).expect("the outputs arrived");
assert!(
settled(&answers, Duration::from_millis(60)).is_none(),
"the input is owed until its output is released"
);
assert_eq!(release_all(delivery), [5]);
let answer = settled(&answers, Duration::from_secs(2)).expect("the join completed");
assert_eq!(answer.receipt::<SceneId>(&ItemId::new(5)), Ok(&SceneId(57)));
drop(feed);
engine.join().expect("the engine thread");
}