use crate::Context;
use core::{convert::Infallible, time::Duration};
use std::{
collections::HashSet,
rc::Rc,
sync::{Arc, Mutex},
};
use super::{Helm, Runs, Upstreams};
use crate::{
Ack, Async, AsyncAutonomousOperator, AutonomousOperator, Custody, Declared, Delivery, Downstream,
Enablement, Fin, GraphBuilder, Inbox, Item, ItemId, Lifecycle, LifecycleTap, NodeId, NodeWiring,
Payload, Plan, RecordingTap, Station, Taps, Wake, Wired, ack_channel, channel, shutdown_signal,
};
const PATIENCE: Duration = Duration::from_secs(5);
type Trace = Arc<Mutex<Vec<String>>>;
fn note(trace: &Trace, line: impl Into<String>) {
trace.lock().expect("the trace's lock").push(line.into());
}
fn took(edge: &Inbox<ItemId, u64>, sweep: Duration) -> Option<Delivery<u64>> {
match edge.park(sweep)? {
Wake::Delivered(delivery) => Some(delivery),
Wake::Sweep | Wake::Tick(_) => None,
}
}
struct Crew {
jobs: Option<crate::conduit::Sender<u64>>,
hand: Option<std::thread::JoinHandle<usize>>,
}
impl Crew {
fn wind_down(&mut self) -> usize {
drop(self.jobs.take());
self.hand.take().map_or(0, |hand| hand.join().unwrap_or(0))
}
}
impl Drop for Crew {
fn drop(&mut self) {
let _ = self.wind_down();
}
}
struct Priority {
trace: Trace,
seen: usize,
}
const GATE: usize = 3;
const PRIOR: usize = 1;
const DEFERRED: usize = 0;
fn publish(downstream: &Downstream<u64>, seq: &mut u64, value: u64) {
let Some(edge) = downstream.edges().first() else {
return;
};
let (ack, _answers) = ack_channel();
let _ = edge.deliver(Delivery::new(
Payload::One(Item::new(ItemId::new(*seq), value)),
ack,
));
*seq += 1;
}
fn prior(
trace: &Trace,
crew: &Crew,
delivery: Delivery<u64>,
downstream: &Downstream<u64>,
seq: &mut u64,
) {
let (payload, custody) = delivery.into_parts();
let released: HashSet<ItemId> = payload.keys().copied().collect();
for item in payload {
let value = item.into_value();
note(trace, format!("b:{value}"));
if let Some(jobs) = crew.jobs.as_ref() {
let _ = jobs.send(value);
}
publish(downstream, seq, value + 100);
}
if let Some(custody) = custody {
note(trace, "ack:b");
custody.send(Ack::new(released, "priority", Lifecycle::Processed(())));
}
}
type Held = (Custody<ItemId>, HashSet<ItemId>, u64);
fn deferred(
trace: &Trace,
crew: &Crew,
delivery: Delivery<u64>,
downstream: &Downstream<u64>,
seq: &mut u64,
held: &mut Vec<Held>,
) {
let (payload, custody) = delivery.into_parts();
let released: HashSet<ItemId> = payload.keys().copied().collect();
let mut first = 0;
for item in payload {
let value = item.into_value();
first = value;
note(trace, format!("a:{value}"));
if let Some(jobs) = crew.jobs.as_ref() {
let _ = jobs.send(value);
}
publish(downstream, seq, value);
}
if let Some(custody) = custody {
held.push((custody, released, first));
}
}
fn settle(trace: &Trace, held: Vec<Held>) {
for (custody, released, value) in held {
note(trace, format!("ack:a:{value}"));
custody.send(Ack::new(released, "priority", Lifecycle::Processed(())));
}
}
impl AutonomousOperator for Priority {
type Error = Infallible;
type In = u64;
type Out = u64;
fn run(
self,
ctx: Context,
upstreams: Upstreams<u64>,
downstream: Downstream<u64>,
) -> Result<Fin, Self::Error> {
let Self { trace, mut seen } = self;
let (jobs, to_do) = crate::conduit::bounded::<u64>(16);
let hand = std::thread::spawn(move || {
let mut taken = 0;
while to_do.recv().is_some() {
taken += 1;
}
taken
});
let mut crew = Crew {
jobs: Some(jobs),
hand: Some(hand),
};
let mut held: Vec<Held> = Vec::new();
let mut seq = 0;
let fin = loop {
if ctx.shutdown().is_shutdown() {
break Fin::Stopped;
}
let open = seen >= GATE;
if !open {
note(&trace, "holding");
}
if let Some(edge) = upstreams.edge(PRIOR)
&& !edge.is_empty()
&& let Some(delivery) = took(edge, PATIENCE)
{
prior(&trace, &crew, delivery, &downstream, &mut seq);
seen += 1;
continue;
}
if open
&& let Some(edge) = upstreams.edge(DEFERRED)
&& !edge.is_empty()
&& let Some(delivery) = took(edge, PATIENCE)
{
deferred(&trace, &crew, delivery, &downstream, &mut seq, &mut held);
continue;
}
if upstreams.is_spent() {
break Fin::Spent;
}
if let Some(edge) = upstreams.edge(PRIOR)
&& let Some(delivery) = took(edge, PATIENCE)
{
prior(&trace, &crew, delivery, &downstream, &mut seq);
seen += 1;
}
};
let counted = crew.wind_down();
note(&trace, format!("joined:{counted}"));
settle(&trace, held);
Ok(fin)
}
}
fn one(seq: u64, value: u64) -> (Delivery<u64>, crate::AckReceiver<ItemId>) {
let (ack, answers) = ack_channel();
(
Delivery::new(Payload::One(Item::new(ItemId::new(seq), value)), ack),
answers,
)
}
fn all_processed(answers: &[crate::AckReceiver<ItemId>]) -> bool {
answers.iter().all(|answers| match answers.take() {
crate::Answer::Settled(settled) => settled.observation().station() == Some(Station::Processed),
crate::Answer::Waiting | crate::Answer::HungUp => false,
})
}
#[test]
fn a_node_that_owns_its_loop_reads_its_edges_in_its_own_order_and_keeps_its_own_books() {
let (into_a, from_a) = channel::<ItemId, u64>(8);
let (into_b, from_b) = channel::<ItemId, u64>(8);
let upstreams = Upstreams::from_iter([from_a, from_b]);
assert_eq!(upstreams.len(), 2, "two edges, kept apart");
let (out, collected) = channel::<ItemId, u64>(64);
let mut downstream = Downstream::new();
downstream.attach(out);
let mut a_answers = Vec::new();
let mut b_answers = Vec::new();
for value in 1..=3 {
let (delivery, answers) = one(value, value);
into_a.deliver(delivery).expect("the edge is open");
a_answers.push(answers);
let (delivery, answers) = one(value, value);
into_b.deliver(delivery).expect("the edge is open");
b_answers.push(answers);
}
drop(into_a);
drop(into_b);
let trace: Trace = Arc::new(Mutex::new(Vec::new()));
let node = Priority {
trace: Arc::clone(&trace),
seen: 0,
};
let (_stopper, halt) = shutdown_signal();
assert_eq!(
node.run(
Context::new("priority").halting(halt),
upstreams,
downstream
),
Ok(Fin::Spent),
"every edge hung up and drained, which is the spent ending"
);
assert_eq!(
trace.lock().expect("the trace's lock").as_slice(),
[
"holding", "b:1", "ack:b", "holding", "b:2", "ack:b", "holding", "b:3", "ack:b",
"a:1", "a:2", "a:3",
"joined:6",
"ack:a:1", "ack:a:2", "ack:a:3",
],
"the order is the NODE's — no driver decided any part of it"
);
assert_eq!(
collected.drain().count(),
6,
"everything the body published crossed its own downstream set"
);
assert!(
all_processed(&b_answers),
"the prior edge, answered at once"
);
assert!(
all_processed(&a_answers),
"the deferred edge, answered at the end — held is not lost"
);
}
#[test]
fn a_stop_said_mid_life_is_the_stopped_ending_and_the_held_books_are_settled() {
let (into_a, from_a) = channel::<ItemId, u64>(8);
let (_into_b, from_b) = channel::<ItemId, u64>(8);
let upstreams = Upstreams::from_iter([from_a, from_b]);
let (out, _collected) = channel::<ItemId, u64>(8);
let mut downstream = Downstream::new();
downstream.attach(out);
let (delivery, answers) = one(0, 7);
into_a.deliver(delivery).expect("the edge is open");
let trace: Trace = Arc::new(Mutex::new(Vec::new()));
let node = Priority {
trace: Arc::clone(&trace),
seen: GATE,
};
let (stopper, halt) = shutdown_signal();
stopper.announce();
assert_eq!(
node.run(
Context::new("priority").halting(halt),
upstreams,
downstream
),
Ok(Fin::Stopped),
"a body that heard the stop wound down"
);
assert_eq!(
trace.lock().expect("the trace's lock").as_slice(),
["joined:0"],
"it took nothing, because it looked at the stop first — and it still ran its epilogue, which \
is what makes a wound-down node's crew wound down too"
);
drop(into_a);
assert!(matches!(answers.take(), crate::Answer::HungUp));
}
struct Pinned {
seed: u64,
taken: Arc<Mutex<Vec<u64>>>,
}
impl AsyncAutonomousOperator for Pinned {
type Error = Infallible;
type In = u64;
type Out = u64;
async fn run(
self,
_ctx: Context,
upstreams: Upstreams<u64>,
_downstream: Downstream<u64>,
) -> Result<Fin, Self::Error> {
let Self { seed, taken } = self;
let here = Rc::new(seed);
let Some(edge) = upstreams.edge(0) else {
return Ok(Fin::Spent);
};
while let Some(wake) = edge.park_async().await {
if let Wake::Delivered(delivery) = wake {
for item in delivery.into_items() {
taken
.lock()
.expect("the record's lock")
.push(item.into_value() + *here);
}
}
}
Ok(Fin::Spent)
}
}
#[test]
fn the_awaited_row_is_driven_pinned_on_the_calling_thread() {
let (into, from) = channel::<ItemId, u64>(8);
let upstreams = Upstreams::from_iter([from]);
let (_stopper, halt) = shutdown_signal();
let (delivery, _answers) = one(0, 1);
into.deliver(delivery).expect("the edge is open");
let (delivery, _answers) = one(1, 2);
into.deliver(delivery).expect("the edge is open");
drop(into);
let taken: Arc<Mutex<Vec<u64>>> = Arc::new(Mutex::new(Vec::new()));
let node = Pinned {
seed: 10,
taken: Arc::clone(&taken),
};
let ended = Runs::<Async>::drive(
node,
Context::new("awaited").halting(halt),
upstreams,
Downstream::new(),
);
assert_eq!(ended, Ok(Fin::Spent));
assert_eq!(*taken.lock().expect("the record's lock"), vec![11, 12]);
}
#[derive(Debug)]
struct DeviceGone;
impl core::fmt::Display for DeviceGone {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("the device went away")
}
}
impl core::error::Error for DeviceGone {}
struct Waiting;
impl AutonomousOperator for Waiting {
type Error = DeviceGone;
type In = u64;
type Out = u64;
fn run(
self,
ctx: Context,
_upstreams: Upstreams<u64>,
_downstream: Downstream<u64>,
) -> Result<Fin, Self::Error> {
ctx.shutdown().heard();
Ok(Fin::Stopped)
}
}
impl Declared for Waiting {
const EFFECT: crate::Effect = crate::Effect::Pure;
type Channels = crate::Unchanneled;
type Mode = crate::Block;
type Options = ();
type Pair = crate::Autonomous;
type Wires<O> = crate::Downstream<O>;
type Attribution<O> = ();
}
struct Dying;
impl AutonomousOperator for Dying {
type Error = DeviceGone;
type In = u64;
type Out = u64;
fn run(
self,
_ctx: Context,
_upstreams: Upstreams<u64>,
_downstream: Downstream<u64>,
) -> Result<Fin, Self::Error> {
Err(DeviceGone)
}
}
impl Declared for Dying {
const EFFECT: crate::Effect = crate::Effect::Pure;
type Channels = crate::Unchanneled;
type Mode = crate::Block;
type Options = ();
type Pair = crate::Autonomous;
type Wires<O> = crate::Downstream<O>;
type Attribution<O> = ();
}
fn watching() -> (Arc<RecordingTap>, Taps) {
let tap = Arc::new(RecordingTap::new());
let taps = Taps::new().with(Arc::clone(&tap) as Arc<dyn LifecycleTap>);
(tap, taps)
}
fn wired() -> Wired {
let mut builder = GraphBuilder::new();
let _ = builder.source::<Keeping>("keeping", crate::DriverKind::Thread);
let graph = builder.validate().expect("one node validates");
Plan::new(graph)
.with_enablement(Enablement::new().launching("keeping"))
.wiring(
NodeId::new(0),
NodeWiring::new(crate::Effect::Lands).landing(),
)
.validate()
.expect("the deployment checks out")
}
struct Keeping;
impl crate::Operator for Keeping {
type Error = Infallible;
type In = u64;
type Out = u64;
fn accept(
&mut self,
_ctx: &Context,
_delivery: Delivery<u64>,
_emitter: &mut crate::Emitter<u64>,
) -> Result<(), Self::Error> {
Ok(())
}
}
impl Declared for Keeping {
const EFFECT: crate::Effect = crate::Effect::Lands;
type Channels = crate::Unchanneled;
type Mode = crate::Block;
type Options = ();
type Pair = crate::Buffered;
type Wires<O> = crate::Downstream<O>;
type Attribution<O> = ();
}
#[test]
fn the_wind_down_reaches_a_body_that_is_inside_run_for_its_whole_life() {
let (tap, taps) = watching();
let plan = wired();
let mut wiring = plan.create().with_taps(taps).wire();
let enrolled = wiring.enrol("waiting");
let mut ignition = wiring.into_assembly().ignite();
let (stopper, halt) = shutdown_signal();
let helm = Helm::new(
Context::new("waiting").halting(halt),
Waiting,
Upstreams::new(),
Downstream::new(),
);
ignition
.autonomous_declared::<Waiting>(helm, stopper, enrolled)
.expect("its engine");
let stopped = ignition.into_app::<()>().shutdown(None);
assert_eq!(stopped.settled(), 1, "a wound-down body settles its party");
assert!(stopped.is_clean(), "{stopped}");
assert_eq!(tap.seen(), vec![("waiting".to_owned(), Station::Processed)]);
}
#[test]
fn a_launched_autonomous_node_that_dies_is_observed_and_settles_anyway() {
let (tap, taps) = watching();
let plan = wired();
let mut wiring = plan.create().with_taps(taps).wire();
let enrolled = wiring.enrol("dying");
let mut ignition = wiring.into_assembly().ignite();
let (stopper, halt) = shutdown_signal();
let helm = Helm::new(
Context::new("dying").halting(halt),
Dying,
Upstreams::new(),
Downstream::new(),
);
ignition
.autonomous_declared::<Dying>(helm, stopper, enrolled)
.expect("its engine");
let stopped = ignition.into_app::<()>().shutdown(None);
assert_eq!(stopped.settled(), 1);
assert!(stopped.is_clean(), "{stopped}");
assert_eq!(tap.seen(), vec![("dying".to_owned(), Station::Failed)]);
}
#[test]
fn an_empty_seat_is_spent_and_a_live_one_is_not() {
let empty: Upstreams<u64> = Upstreams::new();
assert!(empty.is_empty());
assert!(
empty.is_spent(),
"nothing can arrive where there is no edge to arrive on"
);
assert!(empty.edge(0).is_none());
assert_eq!(empty.drain(0).count(), 0);
assert!(empty.park(0, Duration::ZERO).is_none());
let (into, from) = channel::<ItemId, u64>(4);
let upstreams = Upstreams::from_iter([from]);
assert!(!upstreams.is_spent(), "its sender is alive");
let (delivery, _answers) = one(0, 1);
into.deliver(delivery).expect("the edge is open");
assert_eq!(upstreams.queued(), 1);
drop(into);
assert!(
!upstreams.is_spent(),
"hung up, but a queued delivery is still work"
);
assert_eq!(upstreams.drain(0).count(), 1);
assert!(upstreams.is_spent());
}