use std::{
collections::{BTreeSet, HashMap, HashSet},
ffi::OsString,
path::{Path, PathBuf},
vec::Vec,
};
use proptest::prelude::*;
use super::{Subscription, Subsumer, UnwatchOutcome, WatchOutcome};
use crate::interest::Interest;
type S = Subsumer<OsString, (), u32>;
fn key(path: &str) -> Vec<OsString> {
key_of(Path::new(path))
}
fn key_of(path: &Path) -> Vec<OsString> {
path
.components()
.map(|c| c.as_os_str().to_os_string())
.collect()
}
#[derive(Default)]
struct Handles {
next: u32,
}
impl Handles {
fn mint(&mut self) -> u32 {
self.next += 1;
self.next
}
}
fn watch(s: &mut S, handles: &mut Handles, path: &str, interest: Interest) -> (u32, Subscription) {
let k = key(path);
let outcome = s.plan_watch(&k, (), interest);
let (fs_root, sub) = match &outcome {
WatchOutcome::Covered { fs_root, sub, .. } => (*fs_root, *sub),
WatchOutcome::Widen { sub, .. } | WatchOutcome::Disjoint { sub, .. } => (handles.mint(), *sub),
};
s.commit_watch(&outcome, fs_root, &k);
(fs_root, sub)
}
fn root_keys(s: &S) -> BTreeSet<Vec<OsString>> {
s.roots().map(|(k, _)| k.to_vec()).collect()
}
#[test]
fn disjoint_paths_stay_separate() {
let mut s = S::new();
let mut h = Handles::default();
let (ra, sa) = watch(&mut s, &mut h, "/a", Interest::all());
let (rb, sb) = watch(&mut s, &mut h, "/b", Interest::all());
assert_ne!(ra, rb, "unrelated paths get distinct roots");
assert_ne!(sa, sb, "each watch mints a distinct subscription");
assert_eq!(root_keys(&s), BTreeSet::from([key("/a"), key("/b")]));
assert_eq!(s.entry(ra).unwrap().subscribers, vec![sa]);
assert_eq!(s.entry(rb).unwrap().subscribers, vec![sb]);
assert_eq!(s.subscription_key(sa), Some(key("/a").as_slice()));
assert_eq!(s.subscription_key(sb), Some(key("/b").as_slice()));
}
#[test]
fn descendant_is_covered() {
let mut s = S::new();
let mut h = Handles::default();
let (ra, sa) = watch(&mut s, &mut h, "/a", Interest::all());
let outcome = s.plan_watch(&key("/a/b"), (), Interest::all());
let sb = match &outcome {
WatchOutcome::Covered {
fs_root,
sub,
outside_cover,
} => {
assert_eq!(*fs_root, ra, "covered by the /a root");
assert!(
!outside_cover,
"a Covered newcomer under a full-coverage (never-narrowed) root is not outside_cover"
);
*sub
}
other => panic!("expected Covered by /a, got {other:?}"),
};
s.commit_watch(&outcome, ra, &key("/a/b"));
assert_eq!(root_keys(&s), BTreeSet::from([key("/a")]));
assert_eq!(s.entry(ra).unwrap().subscribers, vec![sa, sb]);
assert_eq!(s.subscription_key(sb), Some(key("/a/b").as_slice()));
assert_eq!(s.subscription_interest(sb), Some(Interest::all()));
}
#[test]
fn ancestor_widens_and_repoints() {
let mut s = S::new();
let mut h = Handles::default();
let (narrow, s_narrow) = watch(&mut s, &mut h, "/a/b", Interest::all());
let outcome = s.plan_watch(&key("/a"), (), Interest::all());
let s_wide = match &outcome {
WatchOutcome::Widen {
new_root_key,
repointed,
unwatch,
sub,
} => {
assert_eq!(new_root_key, &key("/a"));
assert_eq!(
repointed,
&vec![s_narrow],
"the /a/b subscriber is re-pointed"
);
assert_eq!(unwatch, &vec![narrow], "the old /a/b root is released");
*sub
}
other => panic!("expected Widen, got {other:?}"),
};
let wide = h.mint();
s.commit_watch(&outcome, wide, &key("/a"));
assert_ne!(wide, narrow);
assert_eq!(root_keys(&s), BTreeSet::from([key("/a")]));
assert!(s.entry(narrow).is_none());
assert_eq!(s.entry(wide).unwrap().subscribers, vec![s_narrow, s_wide]);
assert_eq!(s.entry(wide).unwrap().key, key("/a"));
}
#[test]
fn unwatch_last_subscriber_empties_root() {
let mut s = S::new();
let mut h = Handles::default();
let (ra, sa) = watch(&mut s, &mut h, "/a", Interest::all());
let (_covered_root, sb) = watch(&mut s, &mut h, "/a/b", Interest::all());
assert!(matches!(
s.plan_unwatch(sb),
Some(UnwatchOutcome::Dropped { shrink: None })
));
assert_eq!(s.entry(ra).unwrap().subscribers, vec![sa]);
assert!(matches!(
s.plan_unwatch(sa),
Some(UnwatchOutcome::RootEmptied { fs_root, .. }) if fs_root == ra,
));
assert!(s.entry(ra).is_none());
assert!(root_keys(&s).is_empty());
assert!(s.plan_unwatch(sa).is_none());
}
#[test]
fn side_table_records_per_subscription_interest() {
let mut s = S::new();
let mut h = Handles::default();
let created = Interest::none().with_created();
let removed = Interest::none().with_removed();
let (_ra, sa) = watch(&mut s, &mut h, "/a", created);
let (_rb, sb) = watch(&mut s, &mut h, "/a/b", removed);
assert_eq!(s.subscription_interest(sa), Some(created));
assert_eq!(s.subscription_interest(sb), Some(removed));
let modified = Interest::none().with_modified();
let (_root, sroot) = watch(&mut s, &mut h, "/", modified);
assert_eq!(
s.subscription_interest(sa),
Some(created),
"a re-pointed sub keeps its own interest across a widen"
);
assert_eq!(s.subscription_interest(sb), Some(removed));
assert_eq!(s.subscription_interest(sroot), Some(modified));
assert_eq!(root_keys(&s), BTreeSet::from([key("/")]));
}
#[test]
fn fs_path_preserves_plan_detects_subsumption_divergence() {
let mut s = S::new();
let mut h = Handles::default();
let (_ra, _sa) = watch(&mut s, &mut h, "/a", Interest::all());
assert!(
s.fs_path_preserves_plan(&key("/b"), &[]),
"a still-disjoint fs key preserves a Disjoint plan"
);
assert!(
!s.fs_path_preserves_plan(&key("/a/x"), &[]),
"a fs key covered by an existing root invalidates a Disjoint plan"
);
let (rb, _sb) = watch(&mut s, &mut h, "/c/d", Interest::all());
assert!(
s.fs_path_preserves_plan(&key("/c"), &[rb]),
"a fs key subsuming exactly the planned root preserves the Widen plan"
);
assert!(
!s.fs_path_preserves_plan(&key("/c"), &[]),
"a fs key that now subsumes a root the plan did not drain invalidates it"
);
}
#[test]
fn stale_broad_root_does_not_over_report_is_watched() {
let mut s = S::new();
let mut h = Handles::default();
let view = s.view();
let (_narrow, _s_narrow) = watch(&mut s, &mut h, "/a/b", Interest::all());
let outcome = s.plan_watch(&key("/a"), (), Interest::all());
let s_wide = match &outcome {
WatchOutcome::Widen { sub, .. } => *sub,
other => panic!("expected Widen, got {other:?}"),
};
let wide = h.mint();
s.commit_watch(&outcome, wide, &key("/a"));
match s.plan_unwatch(s_wide) {
Some(UnwatchOutcome::Dropped {
shrink: Some((handle, retained)),
}) => {
assert_eq!(
handle, wide,
"the over-broad wide root /a is reported for shrink"
);
assert_eq!(
retained,
vec![key("/a/b")],
"the retained cover is the /a/b survivor"
);
}
other => panic!("expected Dropped with a shrink to /a/b, got {other:?}"),
}
assert!(
view.contains(&key("/a")),
"the armed root /a is deliberately left broad (no re-narrow)"
);
assert!(
view.is_watched(&key("/a/b")),
"the live /a/b subscription is watched (fan-out delivers under it)"
);
assert!(
!view.is_watched(&key("/a/c")),
"/a/c is NOT watched: no live subscription covers it, so fan-out delivers it to \
nobody — is_watched must not over-report the stale broad root",
);
assert!(
view.resolve(&key("/a/c")).is_none(),
"attribution follows coverage: /a/c resolves to nothing, not a departed watch"
);
let re = s.plan_watch(&key("/a/c"), (), Interest::all());
assert!(
matches!(re, WatchOutcome::Covered { .. }),
"re-installing /a/c is Covered under the still-armed broad root — no re-arm",
);
s.commit_watch(&re, wide, &key("/a/c"));
assert!(
view.is_watched(&key("/a/c")),
"after re-install /a/c is watched again (self-healing, no silent loss)"
);
let mut s = S::new();
let mut h = Handles::default();
let view = s.view();
let (rx, s_x) = watch(&mut s, &mut h, "/x", Interest::all());
let (_covered, _s_xy) = watch(&mut s, &mut h, "/x/y", Interest::all());
assert_eq!(_covered, rx, "/x/y is covered by the /x root (no new arm)");
match s.plan_unwatch(s_x) {
Some(UnwatchOutcome::Dropped {
shrink: Some((handle, retained)),
}) => {
assert_eq!(
handle, rx,
"the over-broad wide root /x is reported for shrink"
);
assert_eq!(
retained,
vec![key("/x/y")],
"the retained cover is the /x/y survivor"
);
}
other => panic!("expected Dropped with a shrink to /x/y, got {other:?}"),
}
assert!(
view.is_watched(&key("/x/y")),
"the live /x/y subscription is still watched"
);
assert!(
!view.is_watched(&key("/x/z")),
"/x/z is NOT watched: only /x/y is live, and it does not cover /x/z (same class)",
);
}
#[test]
fn over_broad_drop_reports_survivor_antichain() {
let mut s = S::new();
let mut h = Handles::default();
let (_rb, _s_b) = watch(&mut s, &mut h, "/a/b", Interest::all());
let (_rc, _s_c) = watch(&mut s, &mut h, "/a/c", Interest::all());
let outcome = s.plan_watch(&key("/a"), (), Interest::all());
let s_a = match &outcome {
WatchOutcome::Widen { sub, .. } => *sub,
other => panic!("expected Widen, got {other:?}"),
};
let wide = h.mint();
s.commit_watch(&outcome, wide, &key("/a"));
match s.plan_unwatch(s_a) {
Some(UnwatchOutcome::Dropped {
shrink: Some((handle, retained)),
}) => {
assert_eq!(
handle, wide,
"the over-broad wide root is reported for shrink"
);
assert_eq!(
retained,
vec![key("/a/b"), key("/a/c")],
"the retained cover is the two survivor subtrees (a minimal antichain)"
);
}
other => panic!("expected an over-broad Dropped, got {other:?}"),
}
}
#[test]
fn drop_with_survivor_at_root_key_is_not_over_broad() {
let mut s = S::new();
let mut h = Handles::default();
let (ra, s_a1) = watch(&mut s, &mut h, "/a", Interest::all());
let (ra2, _s_a2) = watch(&mut s, &mut h, "/a", Interest::all()); assert_eq!(ra2, ra, "the second /a shares the one root");
assert!(
matches!(
s.plan_unwatch(s_a1),
Some(UnwatchOutcome::Dropped { shrink: None })
),
"a survivor still at the root key keeps it legitimately watched — not over-broad"
);
}
#[test]
fn over_broad_antichain_collapses_nested_survivors() {
let mut s = S::new();
let mut h = Handles::default();
let (_rb, _s_b) = watch(&mut s, &mut h, "/a/b", Interest::all());
let (_rbc, _s_bc) = watch(&mut s, &mut h, "/a/b/c", Interest::all());
let outcome = s.plan_watch(&key("/a"), (), Interest::all());
let s_a = match &outcome {
WatchOutcome::Widen { sub, .. } => *sub,
other => panic!("expected Widen, got {other:?}"),
};
let wide = h.mint();
s.commit_watch(&outcome, wide, &key("/a"));
match s.plan_unwatch(s_a) {
Some(UnwatchOutcome::Dropped {
shrink: Some((handle, retained)),
}) => {
assert_eq!(handle, wide, "the over-broad wide root is reported");
assert_eq!(
retained,
vec![key("/a/b")],
"nested survivors /a/b and /a/b/c collapse to the single cover /a/b"
);
}
other => panic!("expected an over-broad Dropped, got {other:?}"),
}
}
#[test]
fn narrowed_cover_non_root_unwatch_reprunes() {
let mut s = S::new();
let mut h = Handles::default();
let (_rb, _s_b) = watch(&mut s, &mut h, "/a/b", Interest::all());
let (_rc, s_c) = watch(&mut s, &mut h, "/a/c", Interest::all());
let outcome = s.plan_watch(&key("/a"), (), Interest::all());
let s_a = match &outcome {
WatchOutcome::Widen { sub, .. } => *sub,
other => panic!("expected Widen, got {other:?}"),
};
let wide = h.mint();
s.commit_watch(&outcome, wide, &key("/a"));
s.plan_unwatch(s_a).expect("unwatch the widening /a");
s.set_retained_cover(wide, Some(vec![key("/a/b"), key("/a/c")]));
match s.plan_unwatch(s_c) {
Some(UnwatchOutcome::Dropped {
shrink: Some((handle, retained)),
}) => {
assert_eq!(
handle, wide,
"the narrowed wide root is reported for the re-prune"
);
assert_eq!(
retained,
vec![key("/a/b")],
"the re-prune carries the shrunken survivor antichain"
);
}
other => panic!("expected a re-prune Dropped, got {other:?}"),
}
}
#[test]
fn equal_survivor_antichain_reports_no_reprune() {
let mut s = S::new();
let mut h = Handles::default();
let (_rb, _s_b) = watch(&mut s, &mut h, "/a/b", Interest::all());
let (_rdeep, s_deep) = watch(&mut s, &mut h, "/a/b/deep", Interest::all()); let (_rc, _s_c) = watch(&mut s, &mut h, "/a/c", Interest::all());
let outcome = s.plan_watch(&key("/a"), (), Interest::all());
let s_a = match &outcome {
WatchOutcome::Widen { sub, .. } => *sub,
other => panic!("expected Widen, got {other:?}"),
};
let wide = h.mint();
s.commit_watch(&outcome, wide, &key("/a"));
s.plan_unwatch(s_a).expect("unwatch the widening /a");
s.set_retained_cover(wide, Some(vec![key("/a/b"), key("/a/c")]));
assert!(
matches!(
s.plan_unwatch(s_deep),
Some(UnwatchOutcome::Dropped { shrink: None })
),
"a survivor covered by a shallower sibling reclaims nothing — no re-prune"
);
}
#[test]
fn retained_cover_for_tracks_current_membership() {
let mut s = S::new();
let mut h = Handles::default();
let (_rb, _s_b) = watch(&mut s, &mut h, "/a/b", Interest::all());
let (_rc, _s_c) = watch(&mut s, &mut h, "/a/c", Interest::all());
let outcome = s.plan_watch(&key("/a"), (), Interest::all());
let s_a = match &outcome {
WatchOutcome::Widen { sub, .. } => *sub,
other => panic!("expected Widen, got {other:?}"),
};
let wide = h.mint();
s.commit_watch(&outcome, wide, &key("/a"));
assert_eq!(
s.retained_cover_for(wide, None),
None,
"a subscriber at the root key pins it — not over-broad, so the re-issue is the cancel-equivalent"
);
s.plan_unwatch(s_a).expect("unwatch the widening /a");
assert_eq!(
s.retained_cover_for(wide, None),
Some(vec![key("/a/b"), key("/a/c")]),
"the recomputed cover is the CURRENT survivor antichain"
);
assert_eq!(
s.retained_cover_for(9999, None),
None,
"an unknown handle has no cover"
);
}
#[test]
fn retained_cover_for_collapses_nested_survivors() {
let mut s = S::new();
let mut h = Handles::default();
let (_rb, _s_b) = watch(&mut s, &mut h, "/a/b", Interest::all());
let (_rbc, _s_bc) = watch(&mut s, &mut h, "/a/b/c", Interest::all()); let outcome = s.plan_watch(&key("/a"), (), Interest::all());
let s_a = match &outcome {
WatchOutcome::Widen { sub, .. } => *sub,
other => panic!("expected Widen, got {other:?}"),
};
let wide = h.mint();
s.commit_watch(&outcome, wide, &key("/a"));
s.plan_unwatch(s_a).expect("unwatch the widening /a");
assert_eq!(
s.retained_cover_for(wide, None),
Some(vec![key("/a/b")]),
"nested survivors /a/b and /a/b/c collapse to the single cover /a/b"
);
}
#[test]
fn retained_cover_for_includes_a_newly_covered_subscriber() {
let mut s = S::new();
let mut h = Handles::default();
let (_rb, _s_b) = watch(&mut s, &mut h, "/a/b", Interest::all());
let outcome = s.plan_watch(&key("/a"), (), Interest::all());
let s_a = match &outcome {
WatchOutcome::Widen { sub, .. } => *sub,
other => panic!("expected Widen, got {other:?}"),
};
let wide = h.mint();
s.commit_watch(&outcome, wide, &key("/a"));
s.plan_unwatch(s_a).expect("unwatch the widening /a");
assert_eq!(
s.retained_cover_for(wide, None),
Some(vec![key("/a/b")]),
"before the covered newcomer, the cover is just the /a/b survivor"
);
let (rc, _s_c) = watch(&mut s, &mut h, "/a/c", Interest::all());
assert_eq!(rc, wide, "the covered /a/c rides the wide root");
assert_eq!(
s.retained_cover_for(wide, None),
Some(vec![key("/a/b"), key("/a/c")]),
"the covered /a/c JOINS the recomputed cover — the fresh membership, not the stale {{/a/b}}"
);
}
#[test]
fn retained_cover_for_joins_the_explicit_newcomer_before_commit() {
let mut s = S::new();
let mut h = Handles::default();
let (_rb, _s_b) = watch(&mut s, &mut h, "/a/b", Interest::all());
let outcome = s.plan_watch(&key("/a"), (), Interest::all());
let s_a = match &outcome {
WatchOutcome::Widen { sub, .. } => *sub,
other => panic!("expected Widen, got {other:?}"),
};
let wide = h.mint();
s.commit_watch(&outcome, wide, &key("/a"));
s.plan_unwatch(s_a).expect("unwatch the widening /a");
assert_eq!(
s.retained_cover_for(wide, Some(&key("/a/c"))),
Some(vec![key("/a/b"), key("/a/c")]),
"the pre-commit newcomer's key joins the survivors' antichain"
);
assert_eq!(
s.retained_cover_for(wide, Some(&key("/a/b/deep"))),
Some(vec![key("/a/b")]),
"a newcomer under a survivor prefix collapses into the existing cover"
);
assert_eq!(
s.retained_cover_for(wide, Some(&key("/a"))),
None,
"a newcomer at the root key reports None — grow back to full coverage"
);
assert_eq!(
s.retained_cover_for(wide, None),
Some(vec![key("/a/b")]),
"the explicit-newcomer query committed nothing"
);
}
#[test]
fn retained_cover_bookkeeping_round_trips() {
let mut s = S::new();
let mut h = Handles::default();
let (ra, _sa) = watch(&mut s, &mut h, "/a", Interest::all());
assert_eq!(
s.retained_cover_of(ra),
None,
"a fresh root is never narrowed — full coverage"
);
s.set_retained_cover(ra, Some(vec![key("/a/b"), key("/a/c")]));
assert_eq!(
s.retained_cover_of(ra),
Some(vec![key("/a/b"), key("/a/c")]),
"the issued cover round-trips through the bookkeeping"
);
s.set_retained_cover(ra, None);
assert_eq!(
s.retained_cover_of(ra),
None,
"resetting to None restores the full-coverage bookkeeping (the cancel-equivalent)"
);
assert_eq!(
s.retained_cover_of(9999),
None,
"an unknown handle has no recorded cover"
);
}
#[test]
fn plan_watch_flags_covered_outside_the_narrowed_cover() {
let mut s = S::new();
let mut h = Handles::default();
let (ra, _sa) = watch(&mut s, &mut h, "/a", Interest::all());
let outcome = s.plan_watch(&key("/a/x"), (), Interest::all());
match &outcome {
WatchOutcome::Covered { outside_cover, .. } => assert!(
!outside_cover,
"under a full-coverage (never-narrowed) root no newcomer is outside_cover"
),
other => panic!("expected Covered, got {other:?}"),
}
s.abort_watch(&outcome);
s.set_retained_cover(ra, Some(vec![key("/a/b")]));
let outcome = s.plan_watch(&key("/a/c"), (), Interest::all());
match &outcome {
WatchOutcome::Covered {
fs_root,
outside_cover,
..
} => {
assert_eq!(*fs_root, ra, "still Covered by the wide /a root");
assert!(
*outside_cover,
"a newcomer under NONE of the narrowed cover's prefixes is outside_cover"
);
}
other => panic!("expected Covered, got {other:?}"),
}
s.abort_watch(&outcome);
let outcome = s.plan_watch(&key("/a/b/deep"), (), Interest::all());
match &outcome {
WatchOutcome::Covered { outside_cover, .. } => assert!(
!outside_cover,
"a newcomer under a retained prefix is already backed by the source — not outside_cover"
),
other => panic!("expected Covered, got {other:?}"),
}
s.abort_watch(&outcome);
let outcome = s.plan_watch(&key("/a/b"), (), Interest::all());
match &outcome {
WatchOutcome::Covered { outside_cover, .. } => assert!(
!outside_cover,
"a newcomer exactly at a retained prefix is covered — not outside_cover"
),
other => panic!("expected Covered, got {other:?}"),
}
s.abort_watch(&outcome);
}
fn is_ancestor_or_equal(ancestor: &[OsString], descendant: &[OsString]) -> bool {
descendant.starts_with(ancestor)
}
#[derive(Debug, Clone)]
enum Op {
Watch(PathBuf),
Unwatch(usize),
}
fn path_strategy() -> impl Strategy<Value = PathBuf> {
proptest::collection::vec(prop_oneof!["a", "b", "c"], 1..=4)
.prop_map(|parts| PathBuf::from(format!("/{}", parts.join("/"))))
}
fn op_strategy() -> impl Strategy<Value = Op> {
prop_oneof![
path_strategy().prop_map(Op::Watch),
any::<usize>().prop_map(Op::Unwatch),
]
}
fn run(ops: &[Op]) -> (S, HashMap<Subscription, PathBuf>) {
let mut s = S::new();
let mut h = Handles::default();
let mut live: Vec<(Subscription, PathBuf)> = Vec::new();
for op in ops {
match op {
Op::Watch(path) => {
let (_root, sub) = watch(&mut s, &mut h, &path.to_string_lossy(), Interest::all());
live.push((sub, path.clone()));
}
Op::Unwatch(idx) => {
if live.is_empty() {
continue;
}
let (sub, _) = live.remove(idx % live.len());
assert!(
s.plan_unwatch(sub).is_some(),
"a live subscription must be dropped",
);
}
}
}
(s, live.into_iter().collect())
}
const CASES: u32 = if cfg!(miri) { 4 } else { 256 };
proptest! {
#![proptest_config(ProptestConfig { cases: CASES, failure_persistence: None, ..ProptestConfig::default() })]
#[test]
fn roots_are_pairwise_disjoint(ops in proptest::collection::vec(op_strategy(), 0..40)) {
let (s, _live) = run(&ops);
let roots: Vec<Vec<OsString>> = s.roots().map(|(k, _)| k.to_vec()).collect();
for (i, a) in roots.iter().enumerate() {
for (j, b) in roots.iter().enumerate() {
if i != j {
prop_assert!(
!is_ancestor_or_equal(a, b),
"roots {a:?} and {b:?} overlap",
);
}
}
}
}
#[test]
fn every_sub_covered_by_exactly_one_root(ops in proptest::collection::vec(op_strategy(), 0..40)) {
let (s, live) = run(&ops);
let roots: Vec<Vec<OsString>> = s.roots().map(|(k, _)| k.to_vec()).collect();
for (sub, expected_path) in &live {
let recorded = s
.subscription_key(*sub)
.expect("a live subscription has a recorded key");
let expected = key_of(expected_path);
prop_assert_eq!(recorded, expected.as_slice(), "side-table key drifted");
let covering = roots.iter().filter(|r| is_ancestor_or_equal(r, recorded)).count();
prop_assert_eq!(
covering, 1,
"sub {} at {:?} is covered by {} roots, want exactly 1",
sub, recorded, covering,
);
}
}
#[test]
fn no_zero_subscriber_root(ops in proptest::collection::vec(op_strategy(), 0..40)) {
let (s, _live) = run(&ops);
for (_key, handle) in s.roots() {
let entry = s.entry(handle).expect("indexed root has an entry");
prop_assert!(!entry.subscribers.is_empty(), "root {handle} has no subscribers");
}
}
#[test]
fn live_sub_set_matches_model(ops in proptest::collection::vec(op_strategy(), 0..40)) {
let (s, live) = run(&ops);
let engine_subs: HashSet<Subscription> = s
.roots()
.flat_map(|(_k, h)| s.entry(h).unwrap().subscribers.clone())
.collect();
let model_subs: HashSet<Subscription> = live.keys().copied().collect();
prop_assert_eq!(engine_subs, model_subs);
}
}
#[test]
fn cohort_retirement_clones_values_linearly() {
use std::sync::{
Arc,
atomic::{AtomicUsize, Ordering},
};
#[derive(Debug)]
struct CountedValue(Arc<AtomicUsize>);
impl Clone for CountedValue {
fn clone(&self) -> Self {
self.0.fetch_add(1, Ordering::Relaxed);
Self(Arc::clone(&self.0))
}
}
const COHORT: usize = 256;
let clones = Arc::new(AtomicUsize::new(0));
let mut s: Subsumer<OsString, CountedValue, u32> = Subsumer::new();
let k = key("/cohort");
let mut root_handle = 0u32;
for i in 0..COHORT {
let outcome = s.plan_watch(&k, CountedValue(Arc::clone(&clones)), Interest::all());
let fs_root = match &outcome {
WatchOutcome::Covered { fs_root, .. } => *fs_root,
WatchOutcome::Widen { .. } | WatchOutcome::Disjoint { .. } => {
root_handle = 1;
1
}
};
let _ = i;
s.commit_watch(&outcome, fs_root, &k);
}
clones.store(0, Ordering::Relaxed);
let retired = s.force_remove_root(root_handle);
assert_eq!(retired.len(), COHORT, "the whole cohort retired");
let spent = clones.load(Ordering::Relaxed);
assert!(
spent <= COHORT * 2,
"cohort retirement is linear in value clones (spent {spent} for {COHORT} — \
the old per-subscriber path spent ~{})",
COHORT * COHORT / 2
);
}