From 762ddde39ae03128cba8b507b411e4e6a60a8300 Mon Sep 17 00:00:00 2001 From: Robert Morrison Date: Fri, 27 Sep 2024 11:36:38 -0700 Subject: [PATCH] tests: integration test for pool idle timeout --- tests/client.rs | 18 ++++++++++++++++++ tests/support/server.rs | 22 ++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/tests/client.rs b/tests/client.rs index 18aaf4e99..d43885a9d 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -572,3 +572,21 @@ async fn highly_concurrent_requests_to_slow_http2_server_with_low_max_concurrent server.shutdown().await; } + +#[tokio::test] +async fn close_connection_after_idle_timeout() { + let mut server = server::http(move |_| async move { http::Response::default() }); + + let client = reqwest::Client::builder() + .pool_idle_timeout(std::time::Duration::from_secs(1)) + .build() + .unwrap(); + + let url = format!("http://{}", server.addr()); + + client.get(&url).send().await.unwrap(); + + tokio::time::sleep(std::time::Duration::from_secs(2)).await; + + assert!(server.events().iter().any(|e| matches!(e, server::Event::ConnectionClosed))); +} diff --git a/tests/support/server.rs b/tests/support/server.rs index 43742b60e..4ad250bdc 100644 --- a/tests/support/server.rs +++ b/tests/support/server.rs @@ -7,18 +7,33 @@ use std::thread; use std::time::Duration; use tokio::runtime; +use tokio::sync::mpsc as tokio_mpsc; use tokio::sync::oneshot; pub struct Server { addr: net::SocketAddr, panic_rx: std_mpsc::Receiver<()>, + events_rx: tokio_mpsc::UnboundedReceiver, shutdown_tx: Option>, } +#[non_exhaustive] +pub enum Event { + ConnectionClosed, +} + impl Server { pub fn addr(&self) -> net::SocketAddr { self.addr } + + pub fn events(&mut self) -> Vec { + let mut events = Vec::new(); + while let Ok(event) = self.events_rx.try_recv() { + events.push(event); + } + events + } } impl Drop for Server { @@ -67,6 +82,7 @@ where let (shutdown_tx, mut shutdown_rx) = oneshot::channel(); let (panic_tx, panic_rx) = std_mpsc::channel(); + let (events_tx, events_rx) = tokio_mpsc::unbounded_channel(); let tname = format!( "test({})-support-server", test_name, @@ -92,8 +108,10 @@ where async move { Ok::<_, Infallible>(fut.await) } }); let builder = builder.clone(); + let events_tx = events_tx.clone(); tokio::spawn(async move { let _ = builder.serve_connection_with_upgrades(hyper_util::rt::TokioIo::new(io), svc).await; + let _ = events_tx.send(Event::ConnectionClosed); }); } } @@ -105,6 +123,7 @@ where Server { addr, panic_rx, + events_rx, shutdown_tx: Some(shutdown_tx), } }) @@ -152,6 +171,7 @@ where let (shutdown_tx, mut shutdown_rx) = oneshot::channel(); let (panic_tx, panic_rx) = std_mpsc::channel(); + let (events_tx, events_rx) = tokio_mpsc::unbounded_channel(); let tname = format!( "test({})-support-server", test_name, @@ -198,6 +218,7 @@ where } } tx.finish().await.unwrap(); + events_tx.send(Event::ConnectionClosed).unwrap(); }); } }); @@ -211,6 +232,7 @@ where Server { addr, panic_rx, + events_rx, shutdown_tx: Some(shutdown_tx), } })