From 74d87b46a09e097c06961bee246b7f205ba40a1a Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 29 Nov 2026 07:07:11 +0100 Subject: [PATCH] cultmesh-rs: wake tests pin what they name (wake-tests-pin-less-than-named) Steward decision (coordinator, 2026-11-29). The reconnect test counts the final wake apart from the abandoned reconnect's Disconnected wake; the not-lost test reads what the wake drained from the client's own queue; a new test pins the event counted before its wake, through a test-hooks accessor beside pump_liveness. Co-Authored-By: Claude Opus 5.5 --- packages/cultmesh-rs/src/rudp_subscription.rs | 7 +++ .../cultmesh-rs/tests/rudp_subscription.rs | 55 ++++++++++++++++--- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/packages/cultmesh-rs/src/rudp_subscription.rs b/packages/cultmesh-rs/src/rudp_subscription.rs index c5824a5c..d5db7cf4 100644 --- a/packages/cultmesh-rs/src/rudp_subscription.rs +++ b/packages/cultmesh-rs/src/rudp_subscription.rs @@ -328,6 +328,13 @@ impl CultMeshRudpSubscriptionClient { self.pump_alive.clone() } + /// The weight (documents and changes) delivered and not yet drained, which `max_undrained` bounds. Only under + /// the `test-hooks` feature, which this crate's own tests enable: not part of the API a consumer builds against. + #[cfg(feature = "test-hooks")] + pub fn undrained_weight(&self) -> usize { + self.undrained.load(Ordering::SeqCst) + } + /// Queues a subscribe with a snapshot and returns; the snapshot arrives as an event. Subscribing an id that is /// live replaces it. While the session is disconnected or faulted the subscription is kept and sent by /// `resubscribe_all`. diff --git a/packages/cultmesh-rs/tests/rudp_subscription.rs b/packages/cultmesh-rs/tests/rudp_subscription.rs index fc80cb28..0d2a6052 100644 --- a/packages/cultmesh-rs/tests/rudp_subscription.rs +++ b/packages/cultmesh-rs/tests/rudp_subscription.rs @@ -1344,6 +1344,11 @@ struct Wakes { } impl Wakes { + fn bump(&self) { + *self.count.lock().unwrap() += 1; + self.changed.notify_all(); + } + fn get(&self) -> usize { *self.count.lock().unwrap() } @@ -1363,8 +1368,7 @@ fn counting_wake() -> (impl Fn() + Send + Sync + 'static, Arc, Receiver<( let counted = wakes.clone(); let wake = move || { let _ = token.try_send(()); - *counted.count.lock().unwrap() += 1; - counted.changed.notify_all(); + counted.bump(); }; (wake, wakes, tokens) } @@ -1432,21 +1436,42 @@ fn a_wake_runs_after_the_event_is_queued() { #[test] fn a_wake_before_the_wait_is_not_lost() { let peer = Peer::start(); - let (wake, wakes, tokens) = counting_wake(); - let client = CultMeshRudpSubscriptionClient::connect_with_wake(options(&peer), wake).unwrap(); + // The wake drains what the pump has queued into an inbox and then stores a permit: what a consumer that waits + // on the permit and then reads the inbox sees is exactly what the queue held when the wake ran. + let inbox: Arc>> = Arc::default(); + let (token, tokens) = mpsc::sync_channel::<()>(1); + let wakes = Arc::new(Wakes::default()); + let slot: Arc>> = Arc::new(OnceLock::new()); + let (wake_inbox, wake_wakes, wake_slot) = (inbox.clone(), wakes.clone(), slot.clone()); + let client = Arc::new( + CultMeshRudpSubscriptionClient::connect_with_wake(options(&peer), move || { + let events = { + let Some(client) = wake_slot.get().and_then(Weak::upgrade) else { return }; + client.try_drain() + }; + wake_inbox.lock().unwrap().extend(events); + let _ = token.try_send(()); + wake_wakes.bump(); + }) + .unwrap(), + ); + slot.set(Arc::downgrade(&client)).unwrap(); client.subscribe("sub-1", vector_selection()).unwrap(); let (message_id, _) = peer.subscribe_id(); peer.send(&snapshot_answering(&message_id)); serve_rows(&peer); tokens.recv_timeout(Duration::from_secs(5)).expect("the snapshot woke"); - assert_eq!(drain_events(&client, 1).len(), 1); - // The next event lands, and its wake has run, before the consumer waits at all: the token is stored. + let held = std::mem::take(&mut *inbox.lock().unwrap()); + assert!(matches!(held.as_slice(), [CultMeshSubscriptionEvent::Snapshot { .. }]), "{held:?}"); + // The next event lands, and its wake has run, before the consumer waits at all: the permit is stored, and the + // inbox the wake filled holds the event. peer.send(&single_change("later", "sub-1")); wakes.wait_for(2); let started = Instant::now(); tokens.recv_timeout(Duration::from_secs(5)).expect("the stored wake is still there"); assert!(started.elapsed() < Duration::from_millis(500), "{:?}", started.elapsed()); - assert_eq!(keys(&drain_events(&client, 1)[0]), ["k"]); + let held = std::mem::take(&mut *inbox.lock().unwrap()); + assert_eq!(held.iter().map(keys).collect::>(), [["k"]]); } #[test] @@ -1562,12 +1587,14 @@ fn close_during_a_reconnect_wakes_a_blocked_waiter_within_the_same_bound() { // The snapshot and the disconnect have each woken and been drained; their stored token is not this test's wake. wakes.wait_for(2); while tokens.try_recv().is_ok() {} + let before = wakes.get(); let (ready, waiter) = blocked_waiter(tokens); ready.recv_timeout(Duration::from_secs(5)).unwrap(); let started = Instant::now(); assert!(!client.close()); let released = waiter.join().unwrap().expect("the pump's last wake released the waiter"); assert!(released.duration_since(started) < Duration::from_millis(300), "{:?}", released.duration_since(started)); + assert_eq!(wakes.get(), before + 2, "the abandoned reconnect's Disconnected woke, and then the pump's last wake"); } #[test] @@ -1677,3 +1704,17 @@ fn a_wake_that_panics_with_a_payload_whose_drop_panics_does_not_stop_the_pump() assert!(pump.upgrade().is_some(), "the pump is alive"); assert_eq!(keys(&drain_events(&client, 1)[0]), ["k"]); } + +#[test] +fn the_event_is_counted_before_its_wake_runs() { + let peer = Peer::start(); + let (weights, seen) = mpsc::channel(); + let (client, reports) = draining_client(options(&peer), move |client| { + let _ = weights.send(client.undrained_weight()); + }); + subscribed_via_wake(&peer, &client, &reports); + assert_eq!(seen.recv_timeout(Duration::from_secs(5)).unwrap(), 2, "the snapshot's two documents"); + peer.send(&single_change("later", "sub-1")); + assert_eq!(keys(&report(&reports)[0]), ["k"]); + assert_eq!(seen.recv_timeout(Duration::from_secs(5)).unwrap(), 1, "the one change"); +}