From 55fcafde2b84e5422cc7ec9ff227c6f48f4fd8a0 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Tue, 6 Aug 2024 09:14:49 -0400 Subject: [PATCH] tests: rewrite drop_client_closes_idle_connection in async --- tests/legacy_client.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/legacy_client.rs b/tests/legacy_client.rs index f2fd8b3..543bdd6 100644 --- a/tests/legacy_client.rs +++ b/tests/legacy_client.rs @@ -15,6 +15,7 @@ use futures_util::stream::StreamExt; use futures_util::{self, Stream}; use http_body_util::BodyExt; use http_body_util::{Empty, Full, StreamBody}; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; use hyper::body::Bytes; use hyper::body::Frame; @@ -89,33 +90,31 @@ fn drop_body_before_eof_closes_connection() { async fn drop_client_closes_idle_connections() { let _ = pretty_env_logger::try_init(); - let server = TcpListener::bind("127.0.0.1:0").unwrap(); + let server = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); let addr = server.local_addr().unwrap(); let (closes_tx, mut closes) = mpsc::channel(10); let (tx1, rx1) = oneshot::channel(); - let (_client_drop_tx, client_drop_rx) = oneshot::channel::<()>(); - thread::spawn(move || { - let mut sock = server.accept().unwrap().0; - sock.set_read_timeout(Some(Duration::from_secs(5))).unwrap(); - sock.set_write_timeout(Some(Duration::from_secs(5))) - .unwrap(); + let t1 = tokio::spawn(async move { + let mut sock = server.accept().await.unwrap().0; let mut buf = [0; 4096]; - sock.read(&mut buf).expect("read 1"); + sock.read(&mut buf).await.expect("read 1"); let body = [b'x'; 64]; - write!( - sock, + let headers = format!( "HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n", body.len() - ) - .expect("write head"); - let _ = sock.write_all(&body); + ); + sock.write_all(headers.as_bytes()).await.expect("write head"); + sock.write_all(&body).await.expect("write body"); let _ = tx1.send(()); // prevent this thread from closing until end of test, so the connection // stays open and idle until Client is dropped - runtime().block_on(client_drop_rx.into_future()) + match sock.read(&mut buf).await { + Ok(n) => assert_eq!(n, 0), + Err(_) => (), + } }); let client = Client::builder(TokioExecutor::new()).build(DebugConnector::with_http_and_closes( @@ -149,6 +148,7 @@ async fn drop_client_closes_idle_connections() { futures_util::pin_mut!(t); let close = closes.into_future().map(|(opt, _)| opt.expect("closes")); future::select(t, close).await; + t1.await.unwrap(); } #[cfg(not(miri))]