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::time::Duration;

use std::sync::{
  Arc,
  atomic::{AtomicUsize, Ordering},
};

use agnostic_lite::{RuntimeLite, tokio::TokioRuntime};

use super::{
  super::{Cadence, ItemId, Rings, channel, launch::Ignition},
  AfterClock, Unruntimed, engage,
};

/// The After family of the one runtime this crate's tests name.
type After = <TokioRuntime as RuntimeLite>::AfterSpawner;

/// A ringer that counts, and keeps answering `true` — the clock's own liveness read the way a live
/// consumer would answer it.
struct Counting(Arc<AtomicUsize>);

impl Rings for Counting {
  fn ring(&self) -> bool {
    self.0.fetch_add(1, Ordering::SeqCst);
    true
  }
}

/// A ringer whose far end is already gone.
struct Departed;

impl Rings for Departed {
  fn ring(&self) -> bool {
    false
  }
}

fn cadence(every: Duration, ringer: impl Rings + 'static) -> Cadence {
  Cadence::new(every, Box::new(ringer))
}

#[tokio::test]
async fn the_after_clock_rings_each_cadence_on_its_own_interval() {
  let fast = Arc::new(AtomicUsize::new(0));
  let slow = Arc::new(AtomicUsize::new(0));
  let clock = AfterClock::start::<After>(vec![
    cadence(Duration::from_millis(10), Counting(Arc::clone(&fast))),
    cadence(Duration::from_millis(100), Counting(Arc::clone(&slow))),
  ]);
  assert_eq!(clock.hands(), 2);

  tokio::time::sleep(Duration::from_millis(120)).await;

  // The claim is the RATIO and not a count: what a repeating timer owes is that each hand runs at
  // its own interval, and pinning either to an exact number would be pinning the scheduler.
  let (fast, slow) = (fast.load(Ordering::SeqCst), slow.load(Ordering::SeqCst));
  assert!(fast >= 5, "the fast hand re-armed itself: {fast}");
  assert!(fast > slow, "and it outran the slow one: {fast} vs {slow}");
}

#[tokio::test]
async fn dropping_the_after_clock_releases_the_ringers_at_once() {
  // THE SHUTDOWN PROPERTY, and the reason the hands are latched. A ringer holds a sender into a
  // node's inbox, so a clock that released one at its next fire would hold a poll-only node open for
  // a whole interval — an hour, for an hourly compactor.
  let (out, inbox) = channel::<ItemId, ()>(4);
  let clock = AfterClock::start::<After>(vec![cadence(Duration::from_secs(3600), out.nudge())]);
  drop(out);
  assert!(
    !inbox.is_disconnected(),
    "the clock's own hand is holding it open"
  );

  drop(clock);

  assert!(
    inbox.is_disconnected(),
    "the drop released the ringer, rather than the fire an hour from now"
  );
}

#[tokio::test]
async fn a_cadence_whose_far_end_is_gone_stops_re_arming() {
  let live = Arc::new(AtomicUsize::new(0));
  let clock = AfterClock::start::<After>(vec![
    cadence(Duration::from_millis(10), Departed),
    cadence(Duration::from_millis(10), Counting(Arc::clone(&live))),
  ]);

  tokio::time::sleep(Duration::from_millis(60)).await;

  assert!(
    live.load(Ordering::SeqCst) >= 3,
    "the survivor is still rung: cadences registered together do not die together"
  );
  drop(clock);
}

#[tokio::test]
async fn an_engaged_body_is_spawned_and_its_join_awaits_it() {
  let ran = Arc::new(AtomicUsize::new(0));
  let counted = Arc::clone(&ran);
  let (started, running) = flume::bounded::<()>(1);

  let join = engage::<<TokioRuntime as RuntimeLite>::Spawner, _>(async move {
    running.recv_async().await.expect("the go-ahead");
    counted.fetch_add(1, Ordering::SeqCst);
  });

  assert_eq!(
    ran.load(Ordering::SeqCst),
    0,
    "it is spawned and waiting, which is what makes the join worth holding"
  );
  started.send(()).expect("the body is listening");
  join.await;
  assert_eq!(
    ran.load(Ordering::SeqCst),
    1,
    "awaiting the join means the body is finished, not merely started"
  );
}

/// THE PIN a thread-only deployment rides on: `Ignition` and `Ignition<Unruntimed>` are ONE type.
///
/// It is a compile-time claim and it is written as one, because that is the only place it can fail:
/// dropping the default, or defaulting the parameter to anything else, breaks this line rather than
/// quietly asking every hand-wired arrangement in the repository to name a runtime it does not have.
const _: fn(Ignition<Unruntimed>) -> Ignition = core::convert::identity;

#[test]
fn the_absence_of_a_runtime_is_a_marker() {
  // Nothing is stored in it and nothing is read off it, so the whole of its behaviour is that a type
  // mentions it — which is what makes naming one free for a deployment that has no runtime.
  assert_eq!(core::mem::size_of::<Unruntimed>(), 0);
  assert_eq!(Unruntimed, Unruntimed);
}