From 264a7890494a298a932cef3d44e2149af26b9c75 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Wed, 23 Sep 2026 17:35:50 +0200 Subject: [PATCH] test(shutdown): wait for the handler to start instead of 100 ms in_flight_request_finishes_after_shutdown_fires failed twice on CI (#350, #352) with hyper's IncompleteMessage. It fired shutdown a fixed 100 ms after sending the request. On a busy runner the connection could be accepted before its request was read; graceful shutdown rightly closes such an idle connection, and the test failed although no in-flight request was dropped. The handler now signals when it has started, and shutdown fires only then, so the test checks what its name says: a request being handled finishes during the drain. Verified: 40 runs, four at a time, all pass. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p --- compliance-core/src/shutdown.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/compliance-core/src/shutdown.rs b/compliance-core/src/shutdown.rs index e63f0df..eaded6a 100644 --- a/compliance-core/src/shutdown.rs +++ b/compliance-core/src/shutdown.rs @@ -148,16 +148,19 @@ mod tests { #[tokio::test] async fn in_flight_request_finishes_after_shutdown_fires() { - // Arrange: a handler that blocks until the test releases it. + // Arrange: a handler that says when it has started, then blocks until + // the test releases it. + let (started_tx, started_rx) = oneshot::channel::<()>(); let (release_tx, release_rx) = oneshot::channel::<()>(); - let release_rx = std::sync::Arc::new(tokio::sync::Mutex::new(Some(release_rx))); + let handles = std::sync::Arc::new(tokio::sync::Mutex::new(Some((started_tx, release_rx)))); let router = axum::Router::new().route( "/slow", get(move || { - let release_rx = release_rx.clone(); + let handles = handles.clone(); async move { - if let Some(rx) = release_rx.lock().await.take() { - let _ = rx.await; + if let Some((started, release)) = handles.lock().await.take() { + let _ = started.send(()); + let _ = release.await; } "done" } @@ -167,10 +170,15 @@ mod tests { let (trigger, shutdown) = channel(); let server = tokio::spawn(serve(listener, router, shutdown, Duration::from_secs(5))); - // Act: start the request, fire shutdown while it is in flight, then - // let the handler finish. + // Act: start the request and fire shutdown once the handler is running. + // Waiting a fixed time instead raced a slow runner: a connection + // accepted but not yet read is idle, and graceful shutdown rightly + // closes it, which failed the test without any request being dropped. let request = tokio::spawn(reqwest::get(format!("http://{addr}/slow"))); - tokio::time::sleep(Duration::from_millis(100)).await; + tokio::time::timeout(Duration::from_secs(5), started_rx) + .await + .expect("the request reaches the handler") + .expect("handler started"); trigger.fire(); tokio::time::sleep(Duration::from_millis(100)).await; let _ = release_tx.send(());