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
//! The citizens' own floor: the executors detach, and the cell admits exactly one.

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

use super::{AtMostOne, Spawn as _, Thread};

/// [`Thread`] runs the work SOMEWHERE ELSE, and the caller does not wait for it.
///
/// The barrier is what makes "somewhere else" checkable rather than asserted: a spawn that ran the
/// closure inline would deadlock on it — the caller cannot reach its own wait — so the test hanging
/// IS the failure, and the assertion below is what makes the pass explicit.
#[test]
fn the_thread_citizen_detaches() {
  let gate = Arc::new(Barrier::new(2));
  let ran = Arc::new(AtomicUsize::new(0));
  let (opened, counted) = (gate.clone(), ran.clone());

  Thread::new().spawn(move || {
    counted.fetch_add(1, Ordering::Release);
    opened.wait();
  });

  gate.wait();
  assert_eq!(ran.load(Ordering::Acquire), 1);
}

/// The cell admits ONE claim, and the second asks in vain until the first drops.
#[test]
fn the_cell_admits_one_and_the_drop_frees_it() {
  let cell = AtMostOne::new();
  assert!(!cell.claimed());

  let held = cell.claim().expect("a free cell admits the first claim");
  assert!(cell.claimed());
  assert!(
    cell.claim().is_none(),
    "a claimed cell admits no second tick"
  );

  drop(held);
  assert!(!cell.claimed());
  assert!(
    cell.claim().is_some(),
    "the drop is what frees it, so the next tick is admitted"
  );
}

/// A CLONE of the cell is the SAME cell, which is what lets the guard travel with detached work.
#[test]
fn a_cloned_cell_is_one_cell() {
  let cell = AtMostOne::new();
  let travelling = cell.clone();
  let held = travelling.claim().expect("the first claim");

  assert!(cell.claimed(), "the clone's claim is the original's");
  assert!(cell.claim().is_none());

  drop(held);
  assert!(cell.claim().is_some());
}