ingraph 0.1.0

A declaration-driven indexing framework: records and nodes are written as Rust types, and the storage rows, the dataflow between them and the GraphQL query surface are emitted from the declaration.
Documentation
use std::sync::Arc;

use super::super::{
  Failure, Lifecycle, LifecycleTap, NodeName, Observation, RecordingTap, Station, Taps,
};
use crate::{Skip, SkipReason};

#[derive(Debug, derive_more::Display)]
#[display("the decoder gave up")]
struct Gave;

impl core::error::Error for Gave {}

#[test]
fn every_station_names_itself_without_its_payload() {
  assert_eq!(Lifecycle::Persisted(1u8).station(), Station::Persisted);
  assert_eq!(Lifecycle::Processed(1u8).station(), Station::Processed);
  assert_eq!(
    Lifecycle::<u8>::Skipped(Skip::new(SkipReason::Filtered)).station(),
    Station::Skipped
  );
  assert_eq!(
    Lifecycle::<u8>::Failed(Failure::new(Gave)).station(),
    Station::Failed
  );
}

#[test]
fn a_skip_is_settled_and_a_failure_is_not() {
  // The watermark condition, written once and asserted here: an intentional refusal is a recorded
  // gap and is DONE, which is what keeps a filtered item from being retried forever.
  assert!(Station::Persisted.is_settled());
  assert!(Station::Processed.is_settled());
  assert!(Station::Skipped.is_settled());
  assert!(!Station::Failed.is_settled());
}

#[test]
fn the_station_names_are_stable() {
  assert_eq!(Station::Persisted.as_str(), "persisted");
  assert_eq!(Station::Processed.as_str(), "processed");
  assert_eq!(Station::Skipped.as_str(), "skipped");
  assert_eq!(Station::Failed.as_str(), "failed");
  assert_eq!(Station::Failed.to_string(), "failed");
}

#[test]
fn erasing_keeps_the_station_and_hands_the_info_back_by_type() {
  let observation = Lifecycle::Processed(41usize).erase();
  assert_eq!(observation.station(), Some(Station::Processed));
  assert_eq!(observation.info::<usize>(), Some(&41));
  assert_eq!(observation.info::<u8>(), None);
  assert!(observation.skip().is_none());
  assert!(observation.failure().is_none());
}

#[test]
fn erasing_a_refusal_keeps_both_halves_of_it() {
  let skip = Skip::new(SkipReason::NoConsumers).about("keyframe#9");
  let observation = Lifecycle::<()>::Skipped(skip).erase();
  assert_eq!(observation.station(), Some(Station::Skipped));
  assert!(observation.info::<()>().is_none());
  let skip = observation.skip().expect("skipped");
  assert!(skip.reason().is_no_consumers());
  assert_eq!(skip.content(), Some("keyframe#9"));
}

#[test]
fn erasing_a_failure_keeps_the_error_rather_than_its_rendering() {
  let observation = Lifecycle::<()>::Failed(Failure::new(Gave)).erase();
  let failure = observation.failure().expect("failed");
  assert_eq!(failure.to_string(), "the decoder gave up");
  // The chain survives, so a tap that knows the concrete error can still walk it.
  assert!(failure.error().is::<Gave>());
  assert!(core::error::Error::source(failure).is_some());
}

#[derive(Default)]
struct Marking(std::sync::Mutex<Vec<&'static str>>);

struct Named(&'static str, Arc<Marking>);

impl LifecycleTap for Named {
  fn observe(&self, _unit: &NodeName, _observation: &Observation) {
    self.1.0.lock().expect("the mark's lock").push(self.0);
  }
}

#[test]
fn taps_compose_and_observe_in_registration_order() {
  let marks = Arc::new(Marking::default());
  let taps = Taps::new()
    .with(Arc::new(Named("journal", marks.clone())))
    .with(Arc::new(Named("tracing", marks.clone())))
    .with(Arc::new(Named("app", marks.clone())));
  assert_eq!(taps.len(), 3);
  assert!(!taps.is_empty());

  taps.observe(NodeName::new("decode"), &Lifecycle::Processed(()).erase());
  assert_eq!(
    *marks.0.lock().expect("the mark's lock"),
    ["journal", "tracing", "app"]
  );
}

#[test]
fn a_composite_nests_so_a_driver_holds_exactly_one_seam() {
  let marks = Arc::new(Marking::default());
  let inner = Taps::new().with(Arc::new(Named("inner", marks.clone())));
  let outer = Taps::new()
    .with(Arc::new(inner))
    .with(Arc::new(Named("outer", marks.clone())));

  outer.observe(NodeName::new("decode"), &Lifecycle::Processed(()).erase());
  assert_eq!(
    *marks.0.lock().expect("the mark's lock"),
    ["inner", "outer"]
  );
}

#[test]
fn no_taps_is_a_walk_over_nothing() {
  let taps = Taps::new();
  assert!(taps.is_empty());
  taps.observe(NodeName::new("decode"), &Lifecycle::Processed(()).erase());
}

#[test]
fn the_recording_tap_keeps_the_pair_a_run_record_is_keyed_by() {
  let tap = RecordingTap::new();
  assert!(tap.is_empty());
  tap.observe(NodeName::new("decode"), &Lifecycle::Processed(()).erase());
  tap.observe(NodeName::new("store"), &Lifecycle::Persisted(()).erase());
  assert_eq!(tap.len(), 2);
  assert_eq!(
    tap.seen(),
    vec![
      ("decode".to_owned(), Station::Processed),
      ("store".to_owned(), Station::Persisted),
    ]
  );
}

#[test]
fn a_node_name_is_a_view_and_compares_as_its_string() {
  let name = NodeName::new("decode");
  assert_eq!(name.as_str(), "decode");
  assert_eq!(&**name, "decode");
  assert_eq!(name, NodeName::new("decode"));
  assert_ne!(name, NodeName::new("Decode"));
}