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(());