Skip to content

Commit ea6449d

Browse files
TheSentinel454Elrondclaude
committed
test(relay): prove waiters reached the gate by a start count, not Arc count
The restriction/host shared-read test measured the gate's Arc count as a baseline and waited for eight more. The cache can hold a transient clone of the gate when the baseline is taken, so the target was one too high and the test timed out under parallel cargo test (14/30 runs on Blox at 06850f7). On the current-thread test runtime a waiter's first poll runs uninterrupted from a start count to its wait on the gate, so eight starts prove eight waiters at the gate. The 500 ms watchdog is now 10 s; timing is not what the test checks. Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Luke Tornquist <[email protected]>
1 parent 7079928 commit ea6449d

1 file changed

Lines changed: 39 additions & 30 deletions

File tree

‎crates/buzz-relay/src/state.rs‎

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4404,41 +4404,52 @@ pub(crate) mod tests {
44044404
own_banned: false,
44054405
muted_until: Some(chrono::Utc::now() + chrono::Duration::hours(1)),
44064406
};
4407-
// Every waiter holds a clone of the leader's gate while it waits, so
4408-
// the gate's count proves all of them reached it before the cache
4409-
// is filled; none can take the initial cache-hit path instead.
4410-
async fn enrolled(gate: Option<Arc<tokio::sync::Mutex<()>>>, waiters: usize) {
4411-
let gate = gate.expect("the leader's gate");
4412-
let base = Arc::strong_count(&gate);
4407+
// On this current-thread runtime a waiter's first poll runs without
4408+
// interruption from its count to its first await, the wait on the
4409+
// leader's gate. Once all have counted, all are waiting at the gate,
4410+
// so none can take the initial cache-hit path when the cache is
4411+
// filled. (The gate's `Arc` count is no proof: the cache may hold a
4412+
// transient clone of its own.)
4413+
async fn enrolled(started: &std::sync::atomic::AtomicUsize, waiters: usize) {
44134414
let start = Instant::now();
4414-
while Arc::strong_count(&gate) < base + waiters {
4415+
while started.load(std::sync::atomic::Ordering::SeqCst) < waiters {
4416+
// A watchdog only; timing is not part of what is checked.
44154417
assert!(
4416-
start.elapsed() < std::time::Duration::from_millis(500),
4418+
start.elapsed() < std::time::Duration::from_secs(10),
44174419
"waiters did not reach the gate"
44184420
);
44194421
tokio::time::sleep(std::time::Duration::from_millis(5)).await;
44204422
}
44214423
}
4424+
fn spawn_waiters<T: Send + 'static, F>(
4425+
started: &Arc<std::sync::atomic::AtomicUsize>,
4426+
read: impl Fn() -> F,
4427+
) -> Vec<tokio::task::JoinHandle<T>>
4428+
where
4429+
F: std::future::Future<Output = T> + Send + 'static,
4430+
{
4431+
(0..8)
4432+
.map(|_| {
4433+
let (started, read) = (Arc::clone(started), read());
4434+
tokio::spawn(async move {
4435+
started.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
4436+
read.await
4437+
})
4438+
})
4439+
.collect()
4440+
}
44224441

44234442
let leader = state
44244443
.restriction_reads
44254444
.turn((community, pubkey.clone()))
44264445
.await
44274446
.expect("no read in flight");
4428-
let waiters = (0..8)
4429-
.map(|_| {
4430-
let state = Arc::clone(&state);
4431-
let pubkey = pubkey.clone();
4432-
tokio::spawn(
4433-
async move { state.restriction_state_cached(community, &pubkey).await },
4434-
)
4435-
})
4436-
.collect::<Vec<_>>();
4437-
enrolled(
4438-
state.restriction_reads.gate(&(community, pubkey.clone())),
4439-
waiters.len(),
4440-
)
4441-
.await;
4447+
let started = Arc::new(std::sync::atomic::AtomicUsize::new(0));
4448+
let waiters = spawn_waiters(&started, || {
4449+
let (state, pubkey) = (Arc::clone(&state), pubkey.clone());
4450+
async move { state.restriction_state_cached(community, &pubkey).await }
4451+
});
4452+
enrolled(&started, waiters.len()).await;
44424453
state.seed_restriction((community, pubkey.clone()), muted.clone());
44434454
drop(leader);
44444455
for waiter in waiters {
@@ -4451,14 +4462,12 @@ pub(crate) mod tests {
44514462
.turn(host.clone())
44524463
.await
44534464
.expect("no read in flight");
4454-
let waiters = (0..8)
4455-
.map(|_| {
4456-
let state = Arc::clone(&state);
4457-
let host = host.clone();
4458-
tokio::spawn(async move { state.resolve_host(&host).await })
4459-
})
4460-
.collect::<Vec<_>>();
4461-
enrolled(state.tenant_host_reads.gate(&host), waiters.len()).await;
4465+
let started = Arc::new(std::sync::atomic::AtomicUsize::new(0));
4466+
let waiters = spawn_waiters(&started, || {
4467+
let (state, host) = (Arc::clone(&state), host.clone());
4468+
async move { state.resolve_host(&host).await }
4469+
});
4470+
enrolled(&started, waiters.len()).await;
44624471
state
44634472
.tenant_host_cache
44644473
.insert(host.clone(), (community, Instant::now()));

0 commit comments

Comments
 (0)