use core::time::Duration;
use std::{
sync::{
Arc,
atomic::{AtomicUsize, Ordering},
},
time::Instant,
};
use agnostic_lite::tokio::TokioRuntime;
use crate::{
Ack, Feed, ItemId, Lifecycle, RegistryOptions, RetireOrigin, RootState, Stamp, Wake, Yielded,
drive::{Inbox, Outbox, channel},
};
use super::{DriverEngine, Registry, RegistryDriver};
const QUIET: Duration = Duration::from_millis(30);
const CEILING: Duration = Duration::from_secs(10);
struct Head {
inbox: Inbox<ItemId, u64>,
taken: Vec<u64>,
}
impl Head {
fn open(depth: usize) -> (Outbox<ItemId, u64>, Self) {
let (outbox, inbox) = channel::<ItemId, u64>(depth);
(
outbox,
Self {
inbox,
taken: Vec::new(),
},
)
}
fn drained(&mut self) -> usize {
let mut took = 0;
for wake in self.inbox.drain() {
let Wake::Delivered(delivery) = wake else {
continue;
};
let (payload, custody) = delivery.into_parts();
let released = payload.keys().copied().collect();
for item in payload {
self.taken.push(item.into_value());
took += 1;
}
if let Some(custody) = custody {
custody.send(Ack::new(released, "head", Lifecycle::Processed(())));
}
}
took
}
fn held(&mut self) -> Vec<(std::collections::HashSet<ItemId>, crate::Custody<ItemId>)> {
let mut holding = Vec::new();
for wake in self.inbox.drain() {
let Wake::Delivered(delivery) = wake else {
continue;
};
let (payload, custody) = delivery.into_parts();
let released = payload.keys().copied().collect();
for item in payload {
self.taken.push(item.into_value());
}
if let Some(custody) = custody {
holding.push((released, custody));
}
}
holding
}
fn taken(&self) -> &[u64] {
&self.taken
}
fn awaiting(&mut self, count: usize) {
let deadline = Instant::now() + CEILING;
while self.taken.len() < count {
let _took = self.drained();
assert!(
Instant::now() < deadline,
"the head waited {CEILING:?} for {count} keys and saw {}: {:?}",
self.taken.len(),
self.taken,
);
std::thread::sleep(Duration::from_millis(1));
}
}
}
fn family(options: RegistryOptions) -> Arc<Registry<u64>> {
Arc::new(Registry::new(options))
}
fn awaited(mut settled: impl FnMut() -> bool, whining: &str) {
let deadline = Instant::now() + CEILING;
while !settled() {
assert!(Instant::now() < deadline, "{whining} (waited {CEILING:?})");
std::thread::sleep(Duration::from_millis(1));
}
}
fn knobs() -> RegistryOptions {
RegistryOptions::new()
.with_debounce(QUIET)
.with_batch_max_wait(QUIET * 2)
}
#[test]
fn three_touches_in_one_window_are_one_head_delivery() {
let registry = family(knobs());
let (outbox, mut head) = Head::open(16);
let (push, keys) = crate::feed::<u64>(16);
let driver = RegistryDriver::spawn(Arc::clone(®istry), keys, outbox);
for _ in 0..3 {
push.give(7).expect("the driver is listening");
}
head.awaiting(1);
std::thread::sleep(QUIET * 4);
let _took = head.drained();
assert_eq!(head.taken(), [7], "one intake for the whole burst");
drop(driver);
}
#[test]
fn ten_keys_close_one_batch_on_the_ceiling() {
let registry = family(
RegistryOptions::new()
.with_debounce(QUIET)
.with_batch_max_wait(Duration::from_secs(30)),
);
let (outbox, mut head) = Head::open(64);
let (push, keys) = crate::feed::<u64>(64);
let driver = RegistryDriver::spawn(Arc::clone(®istry), keys, outbox);
for key in 0..10 {
push.give(key).expect("the driver is listening");
}
head.awaiting(10);
let mut arrived = head.taken().to_vec();
arrived.sort_unstable();
assert_eq!(arrived, (0..10).collect::<Vec<_>>(), "all ten, one batch");
drop(driver);
}
#[test]
fn a_partial_batch_flushes_at_the_deadline() {
let registry = family(
RegistryOptions::new()
.with_debounce(QUIET)
.with_batch_max_wait(Duration::from_millis(60)),
);
let (outbox, mut head) = Head::open(16);
let (push, keys) = crate::feed::<u64>(16);
let driver = RegistryDriver::spawn(Arc::clone(®istry), keys, outbox);
for key in 0..3 {
push.give(key).expect("the driver is listening");
}
head.awaiting(3);
let mut arrived = head.taken().to_vec();
arrived.sort_unstable();
assert_eq!(
arrived,
[0, 1, 2],
"the wait closed what the count could not"
);
drop(driver);
}
#[test]
fn an_ended_line_flushes_at_once() {
let registry = family(
RegistryOptions::new()
.with_debounce(Duration::from_secs(30))
.with_batch_max_wait(Duration::from_secs(30)),
);
let (outbox, mut head) = Head::open(16);
let (push, keys) = crate::feed::<u64>(16);
let driver = RegistryDriver::spawn(Arc::clone(®istry), keys, outbox);
for key in 0..3 {
push.give(key).expect("the driver is listening");
}
drop(push);
head.awaiting(3);
let mut arrived = head.taken().to_vec();
arrived.sort_unstable();
assert_eq!(arrived, [0, 1, 2], "every pending key, however young");
drop(driver);
}
#[test]
fn a_stop_drops_what_was_pending() {
let registry = family(
RegistryOptions::new()
.with_debounce(Duration::from_secs(30))
.with_batch_max_wait(Duration::from_secs(30)),
);
let (outbox, mut head) = Head::open(16);
let (push, keys) = crate::feed::<u64>(16);
let driver = RegistryDriver::spawn(Arc::clone(®istry), keys, outbox);
for key in 0..3 {
push.give(key).expect("the driver is listening");
}
drop(driver);
let _took = head.drained();
assert!(
head.taken().is_empty(),
"a stop pushes nothing into a graph that is winding down: {:?}",
head.taken()
);
for key in 0..3 {
assert_eq!(
registry.state(&key),
RootState::Idle,
"the intake is taken at the give, so a dropped batch leaves the table untouched"
);
}
assert!(registry.is_empty(), "nothing is held for a key never sent");
drop(push);
}
#[test]
fn the_count_is_held_across_the_heads_ack() {
let registry = family(knobs());
let (outbox, mut head) = Head::open(16);
let (push, keys) = crate::feed::<u64>(16);
let driver = RegistryDriver::spawn(Arc::clone(®istry), keys, outbox);
push.give(4).expect("the driver is listening");
let deadline = Instant::now() + CEILING;
let holding = loop {
let holding = head.held();
if !holding.is_empty() {
break holding;
}
assert!(
Instant::now() < deadline,
"the head was never given the key"
);
std::thread::sleep(Duration::from_millis(1));
};
assert_eq!(
registry.inflight(&4),
1,
"the driver's own dispatch, held across the gap the head has not yet filled"
);
assert!(matches!(registry.state(&4), RootState::Analyzing { .. }));
for (released, custody) in holding {
custody.send(Ack::new(released, "head", Lifecycle::Processed(())));
}
awaited(
|| registry.state(&4) == RootState::Idle,
"the key never came back from the ack",
);
drop(driver);
}
#[test]
fn a_retired_key_never_reaches_the_head() {
let registry = family(knobs());
let (outbox, mut head) = Head::open(16);
let (push, keys) = crate::feed::<u64>(16);
let driver = RegistryDriver::spawn(Arc::clone(®istry), keys, outbox);
let _retired = registry.retire(&11, RetireOrigin::User, Stamp::now());
push.give(11).expect("the driver is listening");
push.give(12).expect("the driver is listening");
head.awaiting(1);
std::thread::sleep(QUIET * 4);
let _took = head.drained();
assert_eq!(
head.taken(),
[12],
"the retired key was refused at the gate"
);
assert!(
matches!(registry.state(&11), RootState::Retired { .. }),
"and stayed retired: {:?}",
registry.state(&11)
);
drop(driver);
}
#[test]
fn the_live_dial_is_re_read_when_nothing_is_pending() {
let registry = family(
RegistryOptions::new()
.with_debounce(Duration::from_secs(30))
.with_batch_max_wait(Duration::from_secs(30)),
);
let (outbox, mut head) = Head::open(16);
let (push, keys) = crate::feed::<u64>(16);
let driver = RegistryDriver::spawn(Arc::clone(®istry), keys, outbox);
registry.apply_knobs(
&crate::RegistryOverride::new()
.with_debounce(Duration::from_millis(1))
.with_batch_max_wait(Duration::from_millis(5)),
);
push.give(3).expect("the driver is listening");
head.awaiting(1);
assert_eq!(head.taken(), [3], "the turned dial, not the started one");
drop(driver);
}
#[test]
fn the_thread_road_names_its_engine() {
let registry = family(knobs());
let (outbox, _head) = Head::open(4);
let (push, keys) = crate::feed::<u64>(4);
let driver = RegistryDriver::spawn(registry, keys, outbox);
assert_eq!(driver.engine(), DriverEngine::Thread);
assert_eq!(driver.engine().as_str(), "thread");
drop(push);
drop(driver);
}
#[tokio::test]
async fn the_async_road_takes_a_task_and_no_thread() {
let registry = family(knobs());
let (outbox, mut head) = Head::open(16);
let (push, keys) = crate::feed::<u64>(16);
let driver = RegistryDriver::spawn_on::<TokioRuntime, u64>(Arc::clone(®istry), keys, outbox);
assert_eq!(
driver.engine(),
DriverEngine::Task,
"no thread was asked for: the only line in the module that asks is the other road's"
);
assert_eq!(driver.engine().as_str(), "task");
for _ in 0..3 {
push.give_async(9).await.expect("the driver is listening");
}
let deadline = tokio::time::Instant::now() + CEILING;
while head.taken().is_empty() {
let _took = head.drained();
assert!(
tokio::time::Instant::now() < deadline,
"the task never coalesced the burst"
);
tokio::time::sleep(Duration::from_millis(2)).await;
}
tokio::time::sleep(QUIET * 4).await;
let _took = head.drained();
assert_eq!(head.taken(), [9], "one intake for the whole burst");
drop(driver);
}
#[test]
fn a_give_the_graph_refused_balances_its_own_count() {
let registry = family(knobs());
let (outbox, head) = Head::open(4);
let (push, keys) = crate::feed::<u64>(4);
drop(head);
let driver = RegistryDriver::spawn(Arc::clone(®istry), keys, outbox);
push.give(5).expect("the driver is listening");
awaited(|| push.is_empty(), "the driver never took the key");
std::thread::sleep(QUIET * 4);
assert_eq!(
registry.state(&5),
RootState::Idle,
"the `+1` for a give that never landed is balanced where it was taken"
);
assert!(registry.is_empty(), "and the table holds nothing for it");
drop(driver);
drop(push);
}
#[test]
fn the_deadline_is_the_earlier_of_the_two_machines() {
let registry = family(
RegistryOptions::new()
.with_debounce(Duration::from_millis(10))
.with_batch_max_wait(Duration::from_secs(30)),
);
let (outbox, _head) = Head::open(4);
let mut machine = super::Machine::new(registry, outbox);
assert!(machine.deadline().is_none(), "nothing pending, nothing due");
let now = Instant::now();
machine.touched(1, now);
let quiet = machine.deadline().expect("the debounce is holding one");
assert_eq!(quiet, now + Duration::from_millis(10));
let closed = machine.ripened(now + Duration::from_millis(10));
assert!(closed.is_empty(), "one key does not close a ten-item batch");
assert_eq!(
machine.deadline(),
Some(now + Duration::from_millis(10) + Duration::from_secs(30)),
"the batch's, the window being empty"
);
}
#[test]
fn one_ripening_can_close_several_batches() {
let registry = family(RegistryOptions::new().with_debounce(Duration::from_millis(10)));
let (outbox, _head) = Head::open(4);
let mut machine = super::Machine::new(registry, outbox);
let now = Instant::now();
for key in 0..25 {
machine.touched(key, now);
}
let closed = machine.ripened(now + Duration::from_millis(10));
assert_eq!(closed.len(), 2, "twenty-five keys at ten a batch");
assert!(closed.iter().all(|batch| batch.len() == 10));
assert_eq!(machine.due(now), None, "five are still open and not due");
}
#[test]
fn the_punctuation_door_takes_everything() {
let registry = family(
RegistryOptions::new()
.with_debounce(Duration::from_secs(30))
.with_batch_max_wait(Duration::from_secs(30)),
);
let (outbox, _head) = Head::open(4);
let mut machine = super::Machine::new(registry, outbox);
let now = Instant::now();
for key in 0..3 {
machine.touched(key, now);
}
assert!(machine.ripened(now).is_empty(), "nothing has fallen quiet");
let closed = machine.ended(now);
assert_eq!(closed.len(), 1);
assert_eq!(closed[0].len(), 3, "every pending key, however young");
assert!(machine.ended(now).is_empty(), "and nothing twice");
}
#[test]
fn the_engine_word_is_stable() {
let counted = Arc::new(AtomicUsize::new(0));
assert_eq!(counted.load(Ordering::SeqCst), 0);
assert_eq!(DriverEngine::Task.as_str(), "task");
assert_eq!(DriverEngine::Thread.as_str(), "thread");
assert_ne!(DriverEngine::Task, DriverEngine::Thread);
}
#[test]
fn the_hand_road_pushes_through_the_same_feed() {
let registry = family(knobs());
let (outbox, mut head) = Head::open(16);
let (push, keys): (Feed<u64>, Yielded<u64>) = crate::feed(16);
let driver = RegistryDriver::spawn(Arc::clone(®istry), keys, outbox);
let second = push.clone();
push.give(1).expect("the driver is listening");
second.give(2).expect("the driver is listening");
head.awaiting(2);
let mut arrived = head.taken().to_vec();
arrived.sort_unstable();
assert_eq!(arrived, [1, 2]);
drop(driver);
}
#[test]
fn a_full_head_edge_is_waited_for_rather_than_shed() {
let registry = family(
RegistryOptions::new()
.with_debounce(Duration::from_millis(1))
.with_batch_max_wait(Duration::from_millis(5)),
);
let (outbox, mut head) = Head::open(1);
let (push, keys) = crate::feed::<u64>(16);
let driver = RegistryDriver::spawn(Arc::clone(®istry), keys, outbox);
for key in 0..4 {
push.give(key).expect("the driver is listening");
}
head.awaiting(4);
let mut arrived = head.taken().to_vec();
arrived.sort_unstable();
assert_eq!(arrived, [0, 1, 2, 3], "waited for, not shed");
drop(driver);
}
#[test]
fn dropping_the_handle_stops_and_joins() {
let registry = family(knobs());
let (outbox, _head) = Head::open(4);
let (push, keys) = crate::feed::<u64>(4);
let driver = RegistryDriver::spawn(registry, keys, outbox);
let at = Instant::now();
drop(driver);
assert!(
at.elapsed() < CEILING,
"the join waited for a body nothing had told to go"
);
drop(push);
}