Skip to content

Commit da8126c

Browse files
committed
test: make dispatch concurrency check deterministic
Signed-off-by: Jeremi Joslin <jeremi@joslin.fr>
1 parent 8da0cc7 commit da8126c

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

‎crates/registry-platform-dispatch/tests/postgres_dispatch.rs‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use registry_platform_dispatch::{
3131
idempotency_key, AttemptTimeoutBound, Backoff, DispatchError, FailureCode, Jitter, JobPolicy,
3232
ReceiverReference, RetrySchedule, SendOutcome, Sent, UncertainOutcome,
3333
};
34-
use tokio::sync::{oneshot, watch, Notify};
34+
use tokio::sync::{oneshot, watch, Barrier, Notify};
3535
use tokio_postgres::{NoTls, Row, Transaction};
3636
use uuid::Uuid;
3737

@@ -1018,6 +1018,7 @@ type Script = Box<dyn Fn(&LeasedJob<TestJob>) -> SendOutcome + Send + Sync>;
10181018
struct ScriptedTransport {
10191019
script: Script,
10201020
pause: Duration,
1021+
rendezvous: Option<Arc<Barrier>>,
10211022
sends: Mutex<Vec<(JobKey, i64, i16)>>,
10221023
in_flight: AtomicUsize,
10231024
peak: AtomicUsize,
@@ -1028,6 +1029,7 @@ impl ScriptedTransport {
10281029
Self {
10291030
script: Box::new(script),
10301031
pause: Duration::ZERO,
1032+
rendezvous: None,
10311033
sends: Mutex::new(Vec::new()),
10321034
in_flight: AtomicUsize::new(0),
10331035
peak: AtomicUsize::new(0),
@@ -1039,6 +1041,11 @@ impl ScriptedTransport {
10391041
self
10401042
}
10411043

1044+
fn rendezvous(mut self, participants: usize) -> Self {
1045+
self.rendezvous = Some(Arc::new(Barrier::new(participants)));
1046+
self
1047+
}
1048+
10421049
fn sends(&self) -> Vec<(JobKey, i64, i16)> {
10431050
self.sends.lock().expect("sends lock").clone()
10441051
}
@@ -1060,6 +1067,9 @@ impl DispatchTransport for ScriptedTransport {
10601067
.lock()
10611068
.expect("sends lock")
10621069
.push((job.key.clone(), job.generation, job.attempt));
1070+
if let Some(rendezvous) = &self.rendezvous {
1071+
rendezvous.wait().await;
1072+
}
10631073
if !self.pause.is_zero() {
10641074
tokio::time::sleep(self.pause).await;
10651075
}
@@ -2267,8 +2277,7 @@ async fn the_worker_drains_a_batch_under_bounded_concurrency() {
22672277
for _ in 0..12 {
22682278
enqueue_message(&harness, &Message::default()).await;
22692279
}
2270-
let transport =
2271-
Arc::new(ScriptedTransport::new(|_| accepted()).pausing(Duration::from_millis(100)));
2280+
let transport = Arc::new(ScriptedTransport::new(|_| accepted()).rendezvous(3));
22722281
let worker = DispatchWorker::new(
22732282
dispatcher(&harness),
22742283
Arc::clone(&transport),
@@ -2278,7 +2287,10 @@ async fn the_worker_drains_a_batch_under_bounded_concurrency() {
22782287
},
22792288
);
22802289
let (_shutdown_tx, shutdown) = watch::channel(false);
2281-
assert_eq!(worker.drain(&shutdown).await, 12);
2290+
let moved = tokio::time::timeout(Duration::from_secs(10), worker.drain(&shutdown))
2291+
.await
2292+
.expect("all three lanes reach each send batch before the bounded deadline");
2293+
assert_eq!(moved, 12);
22822294
let peak = transport.peak.load(Ordering::SeqCst);
22832295
assert!(peak <= 3, "at most three sends run at once, saw {peak}");
22842296
assert!(peak >= 2, "the lanes send in parallel, saw {peak}");

0 commit comments

Comments
 (0)