use crate::Context;
use core::time::Duration;
use std::{
sync::{Arc, Mutex},
thread,
time::Instant,
};
use super::{
super::{
Ack, Answer, Delivery, Item, ItemId, Lifecycle, Payload, RecordingTap, Resident, Station, Taps,
ack_channel, channel,
},
Attributes,
};
use crate::{Arrival, Emitter, Failure, NodeName, Operator, drive::Drives};
type Rows = Arc<Mutex<Vec<(u64, String, Station)>>>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct MediaId(u64);
#[derive(Debug, Clone, PartialEq, Eq)]
struct Keyframe {
media: MediaId,
index: u32,
}
struct Wired {
rows: Rows,
}
impl Attributes<Keyframe> for Wired {
type Root = MediaId;
fn root(&self, record: &Keyframe) -> MediaId {
record.media
}
fn observe(&self, root: &MediaId, worker: &NodeName, arrival: &Arrival) {
self.rows.lock().expect("the rows").push((
root.0,
worker.as_str().to_owned(),
arrival.station(),
));
}
}
struct Framing;
impl Operator for Framing {
type Error = core::convert::Infallible;
type In = (u64, u32);
type Out = Keyframe;
fn accept(
&mut self,
_ctx: &Context,
delivery: Delivery<Self::In>,
emitter: &mut Emitter<Keyframe>,
) -> Result<(), Self::Error> {
for item in delivery.into_items() {
let (media, index) = item.into_value();
emitter.emit(Keyframe {
media: MediaId(media),
index,
});
}
Ok(())
}
}
#[test]
fn a_wired_projection_reads_the_root_off_the_declared_key() {
let rows: Rows = Arc::new(Mutex::new(Vec::new()));
let wired = Wired { rows: rows.clone() };
let keyframe = Keyframe {
media: MediaId(7),
index: 3,
};
assert_eq!(wired.root(&keyframe), MediaId(7));
let arrival = Lifecycle::<()>::Persisted(()).arrive();
wired.observe(&MediaId(7), NodeName::new("thumbs"), &arrival);
assert_eq!(
*rows.lock().expect("the rows"),
[(7, "thumbs".to_owned(), Station::Persisted)]
);
}
#[test]
fn the_absent_attribution_records_nothing_and_refuses_nothing() {
let arrival = Lifecycle::<()>::Processed(()).arrive();
let nothing = ();
let keyframe = Keyframe {
media: MediaId(1),
index: 0,
};
assert_eq!(Attributes::<Keyframe>::root(¬hing, &keyframe), ());
Attributes::<Keyframe>::observe(¬hing, &(), NodeName::new("decode"), &arrival);
}
fn answer(
downstream: &super::super::Inbox<ItemId, Keyframe>,
worker: &str,
released: bool,
lifecycle: Lifecycle<()>,
) {
let deadline = Instant::now() + Duration::from_secs(5);
loop {
match downstream.park(Duration::from_millis(5)) {
Some(super::super::Wake::Delivered(delivery)) => {
let (payload, ack) = delivery.into_parts();
let ack = ack.expect("a delivery off the wire carries its custody token");
let keys = if released {
payload.keys().copied().collect()
} else {
std::collections::HashSet::new()
};
ack.send(Ack::new(keys, worker, lifecycle));
return;
}
Some(super::super::Wake::Sweep | super::super::Wake::Tick(_)) => {
assert!(Instant::now() < deadline, "no delivery arrived");
}
None => panic!("the sender is gone"),
}
}
}
#[test]
fn an_answer_comes_back_attributed_to_the_root_the_unit_projected() {
let rows: Rows = Arc::new(Mutex::new(Vec::new()));
let tap = Arc::new(RecordingTap::new());
let (feed, inbox) = channel::<ItemId, (u64, u32)>(8);
let (out, downstream) = channel::<ItemId, Keyframe>(8);
let resident = Resident::new("thumbs", Framing, inbox)
.publish(out)
.with_taps(Taps::new().with(tap.clone()))
.attributing(Wired { rows: rows.clone() })
.with_sweep(Duration::from_millis(5));
let engine = thread::spawn(move || {
let mut resident = resident;
resident.run()
});
let _held: Vec<_> = [(0u64, 11u64, 0u32), (1, 22, 5)]
.into_iter()
.map(|(seq, media, index)| {
let (ack, answers) = ack_channel();
feed
.deliver(Delivery::new(
Payload::One(Item::new(ItemId::new(seq), (media, index))),
ack,
))
.expect("the resident is listening");
answers
})
.collect();
answer(&downstream, "storer", true, Lifecycle::Persisted(()));
answer(
&downstream,
"storer",
false,
Lifecycle::Failed(Failure::new(std::io::Error::other("the disk"))),
);
let deadline = Instant::now() + Duration::from_secs(5);
while rows.lock().expect("the rows").len() < 2 && Instant::now() < deadline {
thread::sleep(Duration::from_millis(5));
}
drop(feed);
drop(downstream);
engine.join().expect("the engine thread");
let mut seen = rows.lock().expect("the rows").clone();
seen.sort_by_key(|(root, _, _)| *root);
assert_eq!(
seen,
[
(11, "storer".to_owned(), Station::Persisted),
(22, "storer".to_owned(), Station::Failed),
]
);
assert_eq!(tap.len(), 2, "the counter-grade tap sees the same two");
}
#[test]
fn a_unit_with_no_projection_wired_still_runs_and_records_nothing() {
let tap = Arc::new(RecordingTap::new());
let (feed, inbox) = channel::<ItemId, (u64, u32)>(8);
let (out, downstream) = channel::<ItemId, Keyframe>(8);
let resident = Resident::new("thumbs", Framing, 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 (ack, answers) = ack_channel();
feed
.deliver(Delivery::new(
Payload::One(Item::new(ItemId::new(0), (11u64, 0u32))),
ack,
))
.expect("the resident is listening");
assert!(matches!(
answers.take(),
Answer::Waiting | Answer::Settled(_)
));
answer(&downstream, "storer", true, Lifecycle::Persisted(()));
let deadline = Instant::now() + Duration::from_secs(5);
while tap.is_empty() && Instant::now() < deadline {
thread::sleep(Duration::from_millis(5));
}
drop(feed);
drop(downstream);
engine.join().expect("the engine thread");
assert_eq!(tap.seen(), [("storer".to_owned(), Station::Persisted)]);
}
mod queued {
use core::time::Duration;
use std::{
collections::BTreeMap,
sync::{Arc, Mutex},
thread,
time::SystemTime,
};
use super::{
super::{
super::{AsyncLands, Commit, Lifecycle},
Attributes, JournalRow, JournalTap, JournalWriter, journal_queue,
},
Keyframe, MediaId, Rows, Wired,
};
use crate::{Arrival, NodeName, Station};
#[derive(Debug, Clone, PartialEq, Eq)]
struct Journal {
media: u64,
unit: String,
station: Station,
}
impl JournalRow<MediaId> for Journal {
fn of(root: &MediaId, node: &NodeName, arrival: &Arrival, _at: SystemTime) -> Self {
Self {
media: root.0,
unit: node.as_str().to_owned(),
station: arrival.station(),
}
}
}
#[derive(Debug, Clone, Default)]
struct Kept {
rows: Arc<Mutex<BTreeMap<(u64, String), Station>>>,
}
impl AsyncLands<Journal> for Kept {
type Error = core::convert::Infallible;
async fn land(&mut self, items: &[Journal]) -> Commit<Self::Error> {
let mut rows = self.rows.lock().expect("the rows");
for row in items {
rows.insert((row.media, row.unit.clone()), row.station);
}
Commit::all()
}
}
#[test]
fn a_full_queue_drops_the_oldest_and_keeps_the_newest() {
let (queue, drain) = journal_queue::<u8>(2);
assert!(queue.offer(1));
assert!(queue.offer(2));
assert_eq!(queue.len(), 2);
assert!(queue.offer(3), "a full queue still takes the row");
assert_eq!(queue.len(), 2, "bounded, and it stayed bounded");
let batch = drain
.take(Duration::from_millis(50))
.expect("the queue is open");
assert_eq!(batch, [2, 3], "1 went, 3 arrived");
}
#[test]
fn a_dropped_observation_is_harmless_because_the_next_one_overwrites_the_row() {
let (queue, drain) = journal_queue::<Journal>(1);
let tap: JournalTap<Keyframe, MediaId, Journal> = JournalTap::new(|frame| frame.media, queue);
let frame = Keyframe {
media: MediaId(7),
index: 0,
};
assert_eq!(tap.root(&frame), MediaId(7));
for lifecycle in [
Lifecycle::<()>::Processed(()),
Lifecycle::<()>::Failed(crate::Failure::new(std::io::Error::other("a wobble"))),
Lifecycle::<()>::Persisted(()),
] {
tap.observe(&MediaId(7), NodeName::new("thumbs"), &lifecycle.arrive());
}
let kept = Kept::default();
let mut writer =
JournalWriter::new(drain, kept.clone()).with_patience(Duration::from_millis(5));
drop(tap);
let report = writer.run();
assert_eq!(report.written(), 1, "two of the three were dropped");
assert_eq!(report.refused(), 0);
assert_eq!(
kept
.rows
.lock()
.expect("the rows")
.get(&(7, "thumbs".to_owned())),
Some(&Station::Persisted)
);
}
#[test]
fn a_burst_of_observations_becomes_one_write() {
let (queue, drain) = journal_queue::<Journal>(64);
let tap: JournalTap<Keyframe, MediaId, Journal> = JournalTap::new(|frame| frame.media, queue);
for media in 1..=3u64 {
tap.observe(
&MediaId(media),
NodeName::new("thumbs"),
&Lifecycle::<()>::Persisted(()).arrive(),
);
}
drop(tap);
let kept = Kept::default();
let mut writer =
JournalWriter::new(drain, kept.clone()).with_patience(Duration::from_millis(5));
let report = writer.run();
assert_eq!(report.batches(), 1, "one write, not three");
assert_eq!(report.written(), 3);
assert_eq!(kept.rows.lock().expect("the rows").len(), 3);
}
#[test]
fn the_writer_runs_on_a_thread_of_its_own_and_stops_when_the_queue_holders_go() {
let (queue, drain) = journal_queue::<Journal>(8);
let kept = Kept::default();
let mut writer =
JournalWriter::new(drain, kept.clone()).with_patience(Duration::from_millis(5));
let engine = thread::spawn(move || writer.run());
let tap: JournalTap<Keyframe, MediaId, Journal> = JournalTap::new(|frame| frame.media, queue);
tap.observe(
&MediaId(9),
NodeName::new("thumbs"),
&Lifecycle::<()>::Persisted(()).arrive(),
);
drop(tap);
let report = engine.join().expect("the writer thread");
assert_eq!(report.written(), 1);
assert_eq!(
kept
.rows
.lock()
.expect("the rows")
.get(&(9, "thumbs".to_owned())),
Some(&Station::Persisted)
);
}
#[test]
fn the_shipped_composition_answers_what_a_hand_wired_hook_answers() {
let rows: Rows = Arc::new(Mutex::new(Vec::new()));
let wired = Wired { rows };
let (queue, _drain) = journal_queue::<Journal>(4);
let shipped: JournalTap<Keyframe, MediaId, Journal> =
JournalTap::new(|frame| frame.media, queue);
let frame = Keyframe {
media: MediaId(4),
index: 1,
};
assert_eq!(wired.root(&frame), shipped.root(&frame));
}
}