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 super::{Downstream, Sink, Wanted};

use crate::{Ack, Item, ItemId, Lifecycle, Wake, channel};

#[test]
fn an_empty_downstream_set_wants_nothing() {
  let downstream: Downstream<u64> = Downstream::new();
  assert_eq!(downstream.arity(), 0);
  assert!(
    !downstream.wanted(),
    "nothing subscribes, so nothing is wanted"
  );
  assert_eq!(downstream.connected(), Wanted::none());
  assert!(downstream.nudges().is_empty());
}

#[test]
fn a_one_product_downstream_set_answers_the_mask_at_bit_zero() {
  let (out, _inbox) = channel::<ItemId, u64>(4);
  let downstream: Downstream<u64> = [out].into_iter().collect();
  let connected = downstream.connected();
  assert!(connected.any());
  assert!(connected.at(0), "the sole product is product zero");
  assert!(!connected.at(1), "there is no second product to want");
}

#[test]
fn every_edge_of_a_one_product_set_carries_every_output() {
  let (first, _one) = channel::<ItemId, u64>(4);
  let (second, _two) = channel::<ItemId, u64>(4);
  let downstream: Downstream<u64> = [first, second].into_iter().collect();
  assert!(downstream.carries(0, &7));
  assert!(downstream.carries(1, &7));
  assert!(
    !downstream.carries(2, &7),
    "an edge past the arity carries nothing"
  );
}

#[test]
fn the_mask_is_one_bit_per_product_and_the_ceiling_holds() {
  let mask = Wanted::none().with(0).with(3);
  assert!(mask.any());
  assert!(mask.at(0) && mask.at(3));
  assert!(!mask.at(1) && !mask.at(2));

  assert!(!Wanted::none().any());
  assert!(Wanted::sole(true).at(0));
  assert!(!Wanted::sole(false).any());

  // Past the ceiling is DROPPED and reads false, never wrapped onto another product's bit.
  let over = Wanted::none().with(Wanted::CEILING);
  assert!(!over.any(), "a product past the ceiling sets nothing");
  assert!(!over.at(Wanted::CEILING));
  assert!(!over.at(0), "and it did not wrap onto product zero");
}

#[tokio::test]
async fn a_delivered_batch_reaches_the_edge_it_was_addressed_to() {
  let (first, one) = channel::<ItemId, u64>(4);
  let (second, two) = channel::<ItemId, u64>(4);
  let mut downstream: Downstream<u64> = [first, second].into_iter().collect();
  assert_eq!(downstream.arity(), 2);
  assert!(downstream.wanted());
  assert_eq!(downstream.nudges().len(), 2, "one ringer per edge");

  let (ack, answers) = crate::ack_channel();
  assert!(
    downstream
      .deliver(1, vec![Item::new(ItemId::new(7), 7u64)], ack)
      .await
  );
  assert!(one.is_empty(), "edge 0 was not addressed");
  let Some(Wake::Delivered(delivery)) = two.drain().next() else {
    panic!("edge 1 took the batch")
  };
  let (payload, ack) = delivery.into_parts();
  assert_eq!(payload.len(), 1);
  ack
    .expect("a delivery off the wire carries its custody token")
    .send(Ack::<ItemId, ()>::holding(
      "consumer",
      Lifecycle::Processed(()),
    ));
  assert!(answers.take().is_settled());
}

#[tokio::test]
async fn a_consumer_that_hung_up_is_reported_rather_than_waited_on() {
  let (out, inbox) = channel::<ItemId, u64>(4);
  let mut downstream: Downstream<u64> = [out].into_iter().collect();
  drop(inbox);

  let (ack, _answers) = crate::ack_channel();
  assert!(
    !downstream
      .deliver(0, vec![Item::new(ItemId::new(0), 0u64)], ack)
      .await,
    "a hung-up consumer answers `false` rather than blocking"
  );
}