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 core::{future::pending, time::Duration};

use std::{thread, time::Instant};

use super::{Arm, Intake, arm, park_on, select};

use crate::{Delivery, Item, ItemId, Payload, Wake, ack_channel, channel};

/// A ready arm, for the select's own ordering questions.
async fn ready<T>(value: T) -> T {
  value
}

#[test]
fn a_select_over_nothing_is_the_honest_no_source_left() {
  // Not a future that hangs: an intake whose every arm has closed has nothing to wait on, and the
  // answer to that is shutdown rather than a park nobody ever ends.
  let mut arms: [Arm<'_, u8>; 0] = [];
  assert_eq!(
    park_on(select(0, &mut arms), Duration::from_millis(50)),
    Some(None)
  );
}

#[test]
fn a_select_answers_the_first_ready_arm_and_says_which() {
  let mut first = core::pin::pin!(pending::<u8>());
  let mut second = core::pin::pin!(ready(7u8));
  let mut third = core::pin::pin!(ready(9u8));
  let mut arms: [Arm<'_, u8>; 3] = [first.as_mut(), second.as_mut(), third.as_mut()];
  assert_eq!(
    park_on(select(0, &mut arms), Duration::ZERO),
    Some(Some((1, 7)))
  );
}

#[test]
fn the_turn_is_what_keeps_a_busy_arm_from_starving_a_quiet_one() {
  // Both are ready. Starting at zero takes zero; starting at one takes one — which is the whole of
  // the rotation, and the reason the caller advances the cursor past whoever just won.
  let mut left = core::pin::pin!(ready(1u8));
  let mut right = core::pin::pin!(ready(2u8));
  let mut arms: [Arm<'_, u8>; 2] = [left.as_mut(), right.as_mut()];
  assert_eq!(
    park_on(select(0, &mut arms), Duration::ZERO),
    Some(Some((0, 1)))
  );

  let mut left = core::pin::pin!(ready(1u8));
  let mut right = core::pin::pin!(ready(2u8));
  let mut arms: [Arm<'_, u8>; 2] = [left.as_mut(), right.as_mut()];
  assert_eq!(
    park_on(select(1, &mut arms), Duration::ZERO),
    Some(Some((1, 2)))
  );
}

#[test]
fn a_closed_arm_never_answers_so_a_hung_up_source_cannot_spin_the_select() {
  // A disconnected channel answers its receive IMMEDIATELY, so an arm re-offered after it has hung
  // up would make the select a busy loop rather than a wait. `arm`'s `open` flag is what stops it.
  let (outbox, inbox) = channel::<ItemId, u8>(4);
  drop(outbox);
  let mut closed = core::pin::pin!(arm(false, &inbox, core::convert::identity));
  let mut arms: [Arm<'_, Option<Wake<ItemId, u8>>>; 1] = [closed.as_mut()];
  assert!(
    park_on(select(0, &mut arms), Duration::from_millis(30)).is_none(),
    "the deadline expires, which is what a park with nothing to hear does"
  );
}

#[test]
fn an_arm_answers_none_when_its_own_source_hangs_up() {
  let (outbox, inbox) = channel::<ItemId, u8>(4);
  drop(outbox);
  let mut open = core::pin::pin!(arm(true, &inbox, core::convert::identity));
  let mut arms: [Arm<'_, Option<Wake<ItemId, u8>>>; 1] = [open.as_mut()];
  let answered = park_on(select(0, &mut arms), Duration::from_millis(200));
  assert!(matches!(answered, Some(Some((0, None)))));
}

#[test]
fn a_park_that_times_out_leaves_the_message_where_the_next_park_finds_it() {
  // The deadline DROPS an unfinished receive. What it must not do is take the message with it: the
  // whole per-upstream select rests on a dropped arm losing nothing.
  let (outbox, inbox) = channel::<ItemId, u8>(4);
  let (ack, _answers) = ack_channel::<ItemId>();
  let waited = park_on(inbox.park_async(), Duration::from_millis(30));
  assert!(waited.is_none(), "nothing had arrived yet");

  outbox
    .deliver(Delivery::new(
      Payload::One(Item::new(ItemId::new(0), 42u8)),
      ack,
    ))
    .expect("the consumer is here");
  let Some(Some(Wake::Delivered(delivery))) = park_on(inbox.park_async(), Duration::from_secs(1))
  else {
    panic!("the next park sees it");
  };
  assert_eq!(delivery.payload().len(), 1);
}

#[test]
fn a_park_is_woken_by_a_send_from_another_thread_rather_than_by_its_deadline() {
  let (outbox, inbox) = channel::<ItemId, u8>(4);
  let (ack, _answers) = ack_channel::<ItemId>();
  let sender = thread::spawn(move || {
    thread::sleep(Duration::from_millis(20));
    outbox
      .deliver(Delivery::new(
        Payload::One(Item::new(ItemId::new(1), 7u8)),
        ack,
      ))
      .expect("the consumer is here");
  });

  let started = Instant::now();
  let woken = park_on(inbox.park_async(), Duration::from_secs(30));
  assert!(matches!(woken, Some(Some(Wake::Delivered(_)))));
  assert!(
    started.elapsed() < Duration::from_secs(5),
    "the wake resumed it, not the clock"
  );
  sender.join().expect("the sender");
}

#[test]
fn the_one_source_intake_is_the_inbox_itself() {
  // The zero-churn pin, stated as a type: the framework's own intake is the inbox a node has always
  // carried, so a single-upstream node names nothing and pays nothing for the seam.
  let (outbox, mut inbox) = channel::<ItemId, u8>(4);
  let (ack, _answers) = ack_channel::<ItemId>();
  outbox
    .deliver(Delivery::new(
      Payload::One(Item::new(ItemId::new(0), 5u8)),
      ack,
    ))
    .expect("the consumer is here");

  let woken = Intake::park(&mut inbox, Duration::from_secs(1));
  assert!(matches!(woken, Some(Wake::Delivered(_))));

  // A drain APPENDS, which is what lets a multi-upstream intake walk several queues into one buffer.
  let mut into = Vec::new();
  Intake::drain(&mut inbox, &mut into);
  assert!(into.is_empty(), "nothing else was queued");

  drop(outbox);
  assert!(
    Intake::park(&mut inbox, Duration::from_secs(1)).is_none(),
    "every sender gone is shutdown"
  );
}