Skip to content

Commit cbe098a

Browse files
authored
tests: rewrite drop_client_closes_idle_connection in async (#141)
1 parent 3170807 commit cbe098a

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

tests/legacy_client.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use futures_util::stream::StreamExt;
1515
use futures_util::{self, Stream};
1616
use http_body_util::BodyExt;
1717
use http_body_util::{Empty, Full, StreamBody};
18+
use tokio::io::{AsyncReadExt, AsyncWriteExt};
1819

1920
use hyper::body::Bytes;
2021
use hyper::body::Frame;
@@ -89,33 +90,30 @@ fn drop_body_before_eof_closes_connection() {
8990
async fn drop_client_closes_idle_connections() {
9091
let _ = pretty_env_logger::try_init();
9192

92-
let server = TcpListener::bind("127.0.0.1:0").unwrap();
93+
let server = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
9394
let addr = server.local_addr().unwrap();
9495
let (closes_tx, mut closes) = mpsc::channel(10);
9596

9697
let (tx1, rx1) = oneshot::channel();
97-
let (_client_drop_tx, client_drop_rx) = oneshot::channel::<()>();
9898

99-
thread::spawn(move || {
100-
let mut sock = server.accept().unwrap().0;
101-
sock.set_read_timeout(Some(Duration::from_secs(5))).unwrap();
102-
sock.set_write_timeout(Some(Duration::from_secs(5)))
103-
.unwrap();
99+
let t1 = tokio::spawn(async move {
100+
let mut sock = server.accept().await.unwrap().0;
104101
let mut buf = [0; 4096];
105-
sock.read(&mut buf).expect("read 1");
102+
sock.read(&mut buf).await.expect("read 1");
106103
let body = [b'x'; 64];
107-
write!(
108-
sock,
109-
"HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n",
110-
body.len()
111-
)
112-
.expect("write head");
113-
let _ = sock.write_all(&body);
104+
let headers = format!("HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n", body.len());
105+
sock.write_all(headers.as_bytes())
106+
.await
107+
.expect("write head");
108+
sock.write_all(&body).await.expect("write body");
114109
let _ = tx1.send(());
115110

116111
// prevent this thread from closing until end of test, so the connection
117112
// stays open and idle until Client is dropped
118-
runtime().block_on(client_drop_rx.into_future())
113+
match sock.read(&mut buf).await {
114+
Ok(n) => assert_eq!(n, 0),
115+
Err(_) => (),
116+
}
119117
});
120118

121119
let client = Client::builder(TokioExecutor::new()).build(DebugConnector::with_http_and_closes(
@@ -149,6 +147,7 @@ async fn drop_client_closes_idle_connections() {
149147
futures_util::pin_mut!(t);
150148
let close = closes.into_future().map(|(opt, _)| opt.expect("closes"));
151149
future::select(t, close).await;
150+
t1.await.unwrap();
152151
}
153152

154153
#[cfg(not(miri))]

0 commit comments

Comments
 (0)