Tokio 0.3 Upgrade (#2319)

Co-authored-by: Urhengulas <johann.hemmann@code.berlin>
Co-authored-by: Eliza Weisman <eliza@buoyant.io>
This commit is contained in:
Sean McArthur
2020-11-05 17:17:21 -08:00
committed by GitHub
parent cc7d3058e8
commit 1b9af22fa0
24 changed files with 467 additions and 472 deletions

View File

@@ -18,7 +18,7 @@ use futures_channel::oneshot;
use futures_core::{Future, Stream, TryFuture};
use futures_util::future::{self, FutureExt, TryFutureExt};
use tokio::net::TcpStream;
use tokio::runtime::Runtime;
mod support;
fn s(buf: &[u8]) -> &str {
std::str::from_utf8(buf).expect("from_utf8")
@@ -115,12 +115,12 @@ macro_rules! test {
#[test]
fn $name() {
let _ = pretty_env_logger::try_init();
let mut rt = Runtime::new().expect("runtime new");
let rt = support::runtime();
let res = test! {
INNER;
name: $name,
runtime: &mut rt,
runtime: &rt,
server:
expected: $server_expected,
reply: $server_reply,
@@ -169,12 +169,12 @@ macro_rules! test {
#[test]
fn $name() {
let _ = pretty_env_logger::try_init();
let mut rt = Runtime::new().expect("runtime new");
let rt = support::runtime();
let err: ::hyper::Error = test! {
INNER;
name: $name,
runtime: &mut rt,
runtime: &rt,
server:
expected: $server_expected,
reply: $server_reply,
@@ -963,10 +963,10 @@ mod dispatch_impl {
use futures_util::future::{FutureExt, TryFutureExt};
use futures_util::stream::StreamExt;
use http::Uri;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio::net::TcpStream;
use tokio::runtime::Runtime;
use super::support;
use hyper::body::HttpBody;
use hyper::client::connect::{Connected, Connection, HttpConnector};
use hyper::Client;
@@ -978,7 +978,7 @@ mod dispatch_impl {
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let mut rt = Runtime::new().unwrap();
let rt = support::runtime();
let (closes_tx, closes) = mpsc::channel(10);
let client = Client::builder().build(DebugConnector::with_http_and_closes(
HttpConnector::new(),
@@ -1016,7 +1016,7 @@ mod dispatch_impl {
rt.block_on(async move {
let (res, ()) = future::join(res, rx).await;
res.unwrap();
tokio::time::delay_for(Duration::from_secs(1)).await;
tokio::time::sleep(Duration::from_secs(1)).await;
});
rt.block_on(closes.into_future()).0.expect("closes");
@@ -1029,7 +1029,7 @@ mod dispatch_impl {
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let mut rt = Runtime::new().unwrap();
let rt = support::runtime();
let (closes_tx, closes) = mpsc::channel(10);
let (tx1, rx1) = oneshot::channel();
@@ -1075,7 +1075,7 @@ mod dispatch_impl {
rt.block_on(async move {
let (res, ()) = future::join(res, rx).await;
res.unwrap();
tokio::time::delay_for(Duration::from_secs(1)).await;
tokio::time::sleep(Duration::from_secs(1)).await;
});
rt.block_on(closes.into_future()).0.expect("closes");
@@ -1113,9 +1113,7 @@ mod dispatch_impl {
// prevent this thread from closing until end of test, so the connection
// stays open and idle until Client is dropped
Runtime::new()
.unwrap()
.block_on(client_drop_rx.into_future())
support::runtime().block_on(client_drop_rx.into_future())
});
let client = Client::builder().build(DebugConnector::with_http_and_closes(
@@ -1147,7 +1145,7 @@ mod dispatch_impl {
drop(client);
// and wait a few ticks for the connections to close
let t = tokio::time::delay_for(Duration::from_millis(100)).map(|_| panic!("time out"));
let t = tokio::time::sleep(Duration::from_millis(100)).map(|_| panic!("time out"));
let close = closes.into_future().map(|(opt, _)| opt.expect("closes"));
future::select(t, close).await;
}
@@ -1195,7 +1193,7 @@ mod dispatch_impl {
future::select(res, rx1).await;
// res now dropped
let t = tokio::time::delay_for(Duration::from_millis(100)).map(|_| panic!("time out"));
let t = tokio::time::sleep(Duration::from_millis(100)).map(|_| panic!("time out"));
let close = closes.into_future().map(|(opt, _)| opt.expect("closes"));
future::select(t, close).await;
}
@@ -1250,7 +1248,7 @@ mod dispatch_impl {
res.unwrap();
// and wait a few ticks to see the connection drop
let t = tokio::time::delay_for(Duration::from_millis(100)).map(|_| panic!("time out"));
let t = tokio::time::sleep(Duration::from_millis(100)).map(|_| panic!("time out"));
let close = closes.into_future().map(|(opt, _)| opt.expect("closes"));
future::select(t, close).await;
}
@@ -1300,7 +1298,7 @@ mod dispatch_impl {
let (res, ()) = future::join(res, rx).await;
res.unwrap();
let t = tokio::time::delay_for(Duration::from_millis(100)).map(|_| panic!("time out"));
let t = tokio::time::sleep(Duration::from_millis(100)).map(|_| panic!("time out"));
let close = closes.into_future().map(|(opt, _)| opt.expect("closes"));
future::select(t, close).await;
}
@@ -1346,7 +1344,7 @@ mod dispatch_impl {
let (res, ()) = future::join(res, rx).await;
res.unwrap();
let t = tokio::time::delay_for(Duration::from_millis(100)).map(|_| panic!("time out"));
let t = tokio::time::sleep(Duration::from_millis(100)).map(|_| panic!("time out"));
let close = closes.into_future().map(|(opt, _)| opt.expect("closes"));
future::select(t, close).await;
}
@@ -1357,7 +1355,7 @@ mod dispatch_impl {
// idle connections that the Checkout would have found
let _ = pretty_env_logger::try_init();
let _rt = Runtime::new().unwrap();
let _rt = support::runtime();
let connector = DebugConnector::new();
let connects = connector.connects.clone();
@@ -1379,7 +1377,7 @@ mod dispatch_impl {
let _ = pretty_env_logger::try_init();
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let mut rt = Runtime::new().unwrap();
let rt = support::runtime();
let connector = DebugConnector::new();
let connects = connector.connects.clone();
@@ -1445,7 +1443,7 @@ mod dispatch_impl {
let _ = pretty_env_logger::try_init();
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let mut rt = Runtime::new().unwrap();
let rt = support::runtime();
let connector = DebugConnector::new();
let connects = connector.connects.clone();
@@ -1507,7 +1505,7 @@ mod dispatch_impl {
let _ = pretty_env_logger::try_init();
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let mut rt = Runtime::new().unwrap();
let rt = support::runtime();
let connector = DebugConnector::new();
let connects = connector.connects.clone();
@@ -1544,7 +1542,7 @@ mod dispatch_impl {
assert_eq!(connects.load(Ordering::Relaxed), 0);
let delayed_body = rx1
.then(|_| tokio::time::delay_for(Duration::from_millis(200)))
.then(|_| tokio::time::sleep(Duration::from_millis(200)))
.map(|_| Ok::<_, ()>("hello a"))
.map_err(|_| -> hyper::Error { panic!("rx1") })
.into_stream();
@@ -1559,7 +1557,7 @@ mod dispatch_impl {
// req 1
let fut = future::join(client.request(req), rx)
.then(|_| tokio::time::delay_for(Duration::from_millis(200)))
.then(|_| tokio::time::sleep(Duration::from_millis(200)))
// req 2
.then(move |()| {
let rx = rx3.expect("thread panicked");
@@ -1646,7 +1644,7 @@ mod dispatch_impl {
// sleep real quick to let the threadpool put connection in ready
// state and back into client pool
tokio::time::delay_for(Duration::from_millis(50)).await;
tokio::time::sleep(Duration::from_millis(50)).await;
let rx = rx2.expect("thread panicked");
let req = Request::builder()
@@ -1669,7 +1667,7 @@ mod dispatch_impl {
let _ = pretty_env_logger::try_init();
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let mut rt = Runtime::new().unwrap();
let rt = support::runtime();
let connector = DebugConnector::new().proxy();
let client = Client::builder().build(connector);
@@ -1708,7 +1706,7 @@ mod dispatch_impl {
let _ = pretty_env_logger::try_init();
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let mut rt = Runtime::new().unwrap();
let rt = support::runtime();
let connector = DebugConnector::new().proxy();
let client = Client::builder().build(connector);
@@ -1750,7 +1748,7 @@ mod dispatch_impl {
let _ = pretty_env_logger::try_init();
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let mut rt = Runtime::new().unwrap();
let rt = support::runtime();
let connector = DebugConnector::new();
@@ -1814,8 +1812,8 @@ mod dispatch_impl {
use tokio::net::TcpListener;
let _ = pretty_env_logger::try_init();
let mut rt = Runtime::new().unwrap();
let mut listener = rt
let rt = support::runtime();
let listener = rt
.block_on(TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 0))))
.unwrap();
let addr = listener.local_addr().unwrap();
@@ -1963,8 +1961,8 @@ mod dispatch_impl {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<Result<usize, io::Error>> {
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
Pin::new(&mut self.tcp).poll_read(cx, buf)
}
}
@@ -1993,19 +1991,18 @@ mod conn {
use futures_channel::oneshot;
use futures_util::future::{self, poll_fn, FutureExt, TryFutureExt};
use futures_util::StreamExt;
use tokio::io::{AsyncRead, AsyncReadExt as _, AsyncWrite, AsyncWriteExt as _};
use tokio::io::{AsyncRead, AsyncReadExt as _, AsyncWrite, AsyncWriteExt as _, ReadBuf};
use tokio::net::{TcpListener as TkTcpListener, TcpStream};
use tokio::runtime::Runtime;
use hyper::client::conn;
use hyper::{self, Body, Method, Request};
use super::{concat, s, tcp_connect, FutureHyperExt};
use super::{concat, s, support, tcp_connect, FutureHyperExt};
#[tokio::test]
async fn get() {
let _ = ::pretty_env_logger::try_init();
let mut listener = TkTcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 0)))
let listener = TkTcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 0)))
.await
.unwrap();
let addr = listener.local_addr().unwrap();
@@ -2052,7 +2049,7 @@ mod conn {
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let mut rt = Runtime::new().unwrap();
let rt = support::runtime();
let (tx1, rx1) = oneshot::channel();
@@ -2090,7 +2087,7 @@ mod conn {
});
let rx = rx1.expect("thread panicked");
let rx = rx.then(|_| tokio::time::delay_for(Duration::from_millis(200)));
let rx = rx.then(|_| tokio::time::sleep(Duration::from_millis(200)));
let chunk = rt.block_on(future::join(res, rx).map(|r| r.0)).unwrap();
assert_eq!(chunk.len(), 5);
}
@@ -2100,7 +2097,7 @@ mod conn {
let _ = ::pretty_env_logger::try_init();
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let mut rt = Runtime::new().unwrap();
let rt = support::runtime();
let (tx, rx) = oneshot::channel();
let server = thread::spawn(move || {
@@ -2127,7 +2124,7 @@ mod conn {
let (mut sender, body) = Body::channel();
let sender = thread::spawn(move || {
sender.try_send_data("hello".into()).expect("try_send_data");
Runtime::new().unwrap().block_on(rx).unwrap();
support::runtime().block_on(rx).unwrap();
sender.abort();
});
@@ -2147,7 +2144,7 @@ mod conn {
fn uri_absolute_form() {
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let mut rt = Runtime::new().unwrap();
let rt = support::runtime();
let (tx1, rx1) = oneshot::channel();
@@ -2185,7 +2182,7 @@ mod conn {
concat(res)
});
let rx = rx1.expect("thread panicked");
let rx = rx.then(|_| tokio::time::delay_for(Duration::from_millis(200)));
let rx = rx.then(|_| tokio::time::sleep(Duration::from_millis(200)));
rt.block_on(future::join(res, rx).map(|r| r.0)).unwrap();
}
@@ -2193,7 +2190,7 @@ mod conn {
fn http1_conn_coerces_http2_request() {
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let mut rt = Runtime::new().unwrap();
let rt = support::runtime();
let (tx1, rx1) = oneshot::channel();
@@ -2231,7 +2228,7 @@ mod conn {
concat(res)
});
let rx = rx1.expect("thread panicked");
let rx = rx.then(|_| tokio::time::delay_for(Duration::from_millis(200)));
let rx = rx.then(|_| tokio::time::sleep(Duration::from_millis(200)));
rt.block_on(future::join(res, rx).map(|r| r.0)).unwrap();
}
@@ -2239,7 +2236,7 @@ mod conn {
fn pipeline() {
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let mut rt = Runtime::new().unwrap();
let rt = support::runtime();
let (tx1, rx1) = oneshot::channel();
@@ -2283,20 +2280,18 @@ mod conn {
});
let rx = rx1.expect("thread panicked");
let rx = rx.then(|_| tokio::time::delay_for(Duration::from_millis(200)));
let rx = rx.then(|_| tokio::time::sleep(Duration::from_millis(200)));
rt.block_on(future::join3(res1, res2, rx).map(|r| r.0))
.unwrap();
}
#[test]
fn upgrade() {
use tokio::io::{AsyncReadExt, AsyncWriteExt};
let _ = ::pretty_env_logger::try_init();
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let mut rt = Runtime::new().unwrap();
let rt = support::runtime();
let (tx1, rx1) = oneshot::channel();
@@ -2346,7 +2341,7 @@ mod conn {
});
let rx = rx1.expect("thread panicked");
let rx = rx.then(|_| tokio::time::delay_for(Duration::from_millis(200)));
let rx = rx.then(|_| tokio::time::sleep(Duration::from_millis(200)));
rt.block_on(future::join3(until_upgrade, res, rx).map(|r| r.0))
.unwrap();
@@ -2379,13 +2374,11 @@ mod conn {
#[test]
fn connect_method() {
use tokio::io::{AsyncReadExt, AsyncWriteExt};
let _ = ::pretty_env_logger::try_init();
let server = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = server.local_addr().unwrap();
let mut rt = Runtime::new().unwrap();
let rt = support::runtime();
let (tx1, rx1) = oneshot::channel();
@@ -2439,7 +2432,7 @@ mod conn {
});
let rx = rx1.expect("thread panicked");
let rx = rx.then(|_| tokio::time::delay_for(Duration::from_millis(200)));
let rx = rx.then(|_| tokio::time::sleep(Duration::from_millis(200)));
rt.block_on(future::join3(until_tunneled, res, rx).map(|r| r.0))
.unwrap();
@@ -2529,7 +2522,7 @@ mod conn {
let _ = shdn_tx.send(());
// Allow time for graceful shutdown roundtrips...
tokio::time::delay_for(Duration::from_millis(100)).await;
tokio::time::sleep(Duration::from_millis(100)).await;
// After graceful shutdown roundtrips, the client should be closed...
future::poll_fn(|ctx| client.poll_ready(ctx))
@@ -2541,7 +2534,7 @@ mod conn {
async fn http2_keep_alive_detects_unresponsive_server() {
let _ = pretty_env_logger::try_init();
let mut listener = TkTcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 0)))
let listener = TkTcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 0)))
.await
.unwrap();
let addr = listener.local_addr().unwrap();
@@ -2581,7 +2574,7 @@ mod conn {
let _ = pretty_env_logger::try_init();
let mut listener = TkTcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 0)))
let listener = TkTcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 0)))
.await
.unwrap();
let addr = listener.local_addr().unwrap();
@@ -2606,7 +2599,7 @@ mod conn {
});
// sleep longer than keepalive would trigger
tokio::time::delay_for(Duration::from_secs(4)).await;
tokio::time::sleep(Duration::from_secs(4)).await;
future::poll_fn(|ctx| client.poll_ready(ctx))
.await
@@ -2617,7 +2610,7 @@ mod conn {
async fn http2_keep_alive_closes_open_streams() {
let _ = pretty_env_logger::try_init();
let mut listener = TkTcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 0)))
let listener = TkTcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 0)))
.await
.unwrap();
let addr = listener.local_addr().unwrap();
@@ -2667,7 +2660,7 @@ mod conn {
let _ = pretty_env_logger::try_init();
let mut listener = TkTcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 0)))
let listener = TkTcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 0)))
.await
.unwrap();
let addr = listener.local_addr().unwrap();
@@ -2711,7 +2704,7 @@ mod conn {
let _resp = client.send_request(req1).await.expect("send_request");
// sleep longer than keepalive would trigger
tokio::time::delay_for(Duration::from_secs(4)).await;
tokio::time::sleep(Duration::from_secs(4)).await;
future::poll_fn(|ctx| client.poll_ready(ctx))
.await
@@ -2763,8 +2756,8 @@ mod conn {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<Result<usize, io::Error>> {
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
Pin::new(&mut self.tcp).poll_read(cx, buf)
}
}

View File

@@ -18,9 +18,8 @@ use futures_util::future::{self, Either, FutureExt, TryFutureExt};
#[cfg(feature = "stream")]
use futures_util::stream::StreamExt as _;
use http::header::{HeaderName, HeaderValue};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf};
use tokio::net::{TcpListener, TcpStream as TkTcpStream};
use tokio::runtime::Runtime;
use hyper::body::HttpBody as _;
use hyper::client::Client;
@@ -29,6 +28,8 @@ use hyper::server::Server;
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, StatusCode, Version};
mod support;
#[test]
fn get_should_ignore_body() {
let server = serve();
@@ -788,7 +789,7 @@ fn expect_continue_but_no_body_is_ignored() {
#[tokio::test]
async fn expect_continue_waits_for_body_poll() {
let _ = pretty_env_logger::try_init();
let mut listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();
let child = thread::spawn(move || {
@@ -821,7 +822,7 @@ async fn expect_continue_waits_for_body_poll() {
service_fn(|req| {
assert_eq!(req.headers()["expect"], "100-continue");
// But! We're never going to poll the body!
tokio::time::delay_for(Duration::from_millis(50)).map(move |_| {
tokio::time::sleep(Duration::from_millis(50)).map(move |_| {
// Move and drop the req, so we don't auto-close
drop(req);
Response::builder()
@@ -956,7 +957,7 @@ fn http_10_request_receives_http_10_response() {
#[tokio::test]
async fn disable_keep_alive_mid_request() {
let mut listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();
let (tx1, rx1) = oneshot::channel();
@@ -994,7 +995,7 @@ async fn disable_keep_alive_mid_request() {
#[tokio::test]
async fn disable_keep_alive_post_request() {
let _ = pretty_env_logger::try_init();
let mut listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();
let (tx1, rx1) = oneshot::channel();
@@ -1046,7 +1047,7 @@ async fn disable_keep_alive_post_request() {
#[tokio::test]
async fn empty_parse_eof_does_not_return_error() {
let _ = pretty_env_logger::try_init();
let mut listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();
thread::spawn(move || {
@@ -1062,7 +1063,7 @@ async fn empty_parse_eof_does_not_return_error() {
#[tokio::test]
async fn nonempty_parse_eof_returns_error() {
let mut listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();
thread::spawn(move || {
@@ -1080,7 +1081,7 @@ async fn nonempty_parse_eof_returns_error() {
#[tokio::test]
async fn http1_allow_half_close() {
let _ = pretty_env_logger::try_init();
let mut listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();
let t1 = thread::spawn(move || {
@@ -1100,7 +1101,7 @@ async fn http1_allow_half_close() {
.serve_connection(
socket,
service_fn(|_| {
tokio::time::delay_for(Duration::from_millis(500))
tokio::time::sleep(Duration::from_millis(500))
.map(|_| Ok::<_, hyper::Error>(Response::new(Body::empty())))
}),
)
@@ -1113,7 +1114,7 @@ async fn http1_allow_half_close() {
#[tokio::test]
async fn disconnect_after_reading_request_before_responding() {
let _ = pretty_env_logger::try_init();
let mut listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();
thread::spawn(move || {
@@ -1127,7 +1128,7 @@ async fn disconnect_after_reading_request_before_responding() {
.serve_connection(
socket,
service_fn(|_| {
tokio::time::delay_for(Duration::from_secs(2)).map(
tokio::time::sleep(Duration::from_secs(2)).map(
|_| -> Result<Response<Body>, hyper::Error> {
panic!("response future should have been dropped");
},
@@ -1140,7 +1141,7 @@ async fn disconnect_after_reading_request_before_responding() {
#[tokio::test]
async fn returning_1xx_response_is_error() {
let mut listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();
thread::spawn(move || {
@@ -1193,7 +1194,7 @@ async fn upgrades() {
use tokio::io::{AsyncReadExt, AsyncWriteExt};
let _ = pretty_env_logger::try_init();
let mut listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();
let (tx, rx) = oneshot::channel();
@@ -1252,7 +1253,7 @@ async fn http_connect() {
use tokio::io::{AsyncReadExt, AsyncWriteExt};
let _ = pretty_env_logger::try_init();
let mut listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();
let (tx, rx) = oneshot::channel();
@@ -1308,7 +1309,7 @@ async fn upgrades_new() {
use tokio::io::{AsyncReadExt, AsyncWriteExt};
let _ = pretty_env_logger::try_init();
let mut listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();
let (read_101_tx, read_101_rx) = oneshot::channel();
@@ -1375,7 +1376,7 @@ async fn upgrades_new() {
#[tokio::test]
async fn upgrades_ignored() {
let _ = pretty_env_logger::try_init();
let mut listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move {
@@ -1417,7 +1418,7 @@ async fn http_connect_new() {
use tokio::io::{AsyncReadExt, AsyncWriteExt};
let _ = pretty_env_logger::try_init();
let mut listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();
let (read_200_tx, read_200_rx) = oneshot::channel();
@@ -1480,7 +1481,7 @@ async fn http_connect_new() {
#[tokio::test]
async fn parse_errors_send_4xx_response() {
let mut listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();
thread::spawn(move || {
@@ -1502,7 +1503,7 @@ async fn parse_errors_send_4xx_response() {
#[tokio::test]
async fn illegal_request_length_returns_400_response() {
let mut listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();
thread::spawn(move || {
@@ -1538,7 +1539,7 @@ fn max_buf_size_no_panic() {
#[tokio::test]
async fn max_buf_size() {
let _ = pretty_env_logger::try_init();
let mut listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();
const MAX: usize = 16_000;
@@ -1592,7 +1593,7 @@ fn http1_response_with_http2_version() {
let server = serve();
let addr_str = format!("http://{}", server.addr());
let mut rt = Runtime::new().expect("runtime new");
let rt = support::runtime();
server.reply().version(hyper::Version::HTTP_2);
@@ -1609,7 +1610,7 @@ fn try_h2() {
let server = serve();
let addr_str = format!("http://{}", server.addr());
let mut rt = Runtime::new().expect("runtime new");
let rt = support::runtime();
rt.block_on({
let client = Client::builder()
@@ -1629,7 +1630,7 @@ fn http1_only() {
let server = serve_opts().http1_only().serve();
let addr_str = format!("http://{}", server.addr());
let mut rt = Runtime::new().expect("runtime new");
let rt = support::runtime();
rt.block_on({
let client = Client::builder()
@@ -1684,7 +1685,7 @@ fn http2_body_user_error_sends_reset_reason() {
server.reply().body_stream(b);
let mut rt = Runtime::new().expect("runtime new");
let rt = support::runtime();
let err: hyper::Error = rt
.block_on(async move {
@@ -1823,7 +1824,7 @@ fn skips_content_length_and_body_for_304_responses() {
async fn http2_keep_alive_detects_unresponsive_client() {
let _ = pretty_env_logger::try_init();
let mut listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();
// Spawn a "client" conn that only reads until EOF
@@ -1871,7 +1872,7 @@ async fn http2_keep_alive_detects_unresponsive_client() {
async fn http2_keep_alive_with_responsive_client() {
let _ = pretty_env_logger::try_init();
let mut listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move {
@@ -1897,7 +1898,7 @@ async fn http2_keep_alive_with_responsive_client() {
conn.await.expect("client conn");
});
tokio::time::delay_for(Duration::from_secs(4)).await;
tokio::time::sleep(Duration::from_secs(4)).await;
let req = http::Request::new(hyper::Body::empty());
client.send_request(req).await.expect("client.send_request");
@@ -1938,7 +1939,7 @@ async fn write_pong_frame(conn: &mut TkTcpStream) {
async fn http2_keep_alive_count_server_pings() {
let _ = pretty_env_logger::try_init();
let mut listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let listener = tcp_bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move {
@@ -2294,38 +2295,32 @@ impl ServeOptions {
let thread = thread::Builder::new()
.name(thread_name)
.spawn(move || {
let mut rt = tokio::runtime::Builder::new()
.enable_io()
.enable_time()
.basic_scheduler()
.build()
.expect("rt new");
support::runtime()
.block_on(async move {
let service = make_service_fn(|_| {
let msg_tx = msg_tx.clone();
let reply_rx = reply_rx.clone();
future::ok::<_, BoxError>(TestService {
tx: msg_tx,
reply: reply_rx,
})
});
rt.block_on(async move {
let service = make_service_fn(|_| {
let msg_tx = msg_tx.clone();
let reply_rx = reply_rx.clone();
future::ok::<_, BoxError>(TestService {
tx: msg_tx,
reply: reply_rx,
})
});
let server = Server::bind(&addr)
.http1_only(options.http1_only)
.http1_keepalive(options.keep_alive)
.http1_pipeline_flush(options.pipeline)
.serve(service);
let server = Server::bind(&addr)
.http1_only(options.http1_only)
.http1_keepalive(options.keep_alive)
.http1_pipeline_flush(options.pipeline)
.serve(service);
addr_tx.send(server.local_addr()).expect("server addr tx");
addr_tx.send(server.local_addr()).expect("server addr tx");
server
.with_graceful_shutdown(async {
let _ = shutdown_rx.await;
})
.await
})
.expect("serve()");
server
.with_graceful_shutdown(async {
let _ = shutdown_rx.await;
})
.await
})
.expect("serve()");
})
.expect("thread spawn");
@@ -2353,6 +2348,7 @@ fn has_header(msg: &str, name: &str) -> bool {
fn tcp_bind(addr: &SocketAddr) -> ::tokio::io::Result<TcpListener> {
let std_listener = StdTcpListener::bind(addr).unwrap();
std_listener.set_nonblocking(true).unwrap();
TcpListener::from_std(std_listener)
}
@@ -2429,8 +2425,8 @@ impl<T: AsyncRead + Unpin, D: Unpin> AsyncRead for DebugStream<T, D> {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
Pin::new(&mut self.stream).poll_read(cx, buf)
}
}

View File

@@ -1,3 +1,4 @@
#![allow(dead_code)]
use std::future::Future;
use std::pin::Pin;
use std::sync::{
@@ -15,6 +16,7 @@ pub use futures_util::{
pub use hyper::{HeaderMap, StatusCode};
pub use std::net::SocketAddr;
#[allow(unused_macros)]
macro_rules! t {
(
$name:ident,
@@ -303,15 +305,16 @@ pub struct __TestConfig {
pub proxy: bool,
}
pub fn __run_test(cfg: __TestConfig) {
let _ = pretty_env_logger::try_init();
tokio::runtime::Builder::new()
.enable_io()
.enable_time()
.basic_scheduler()
pub fn runtime() -> tokio::runtime::Runtime {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("new rt")
.block_on(async_test(cfg));
}
pub fn __run_test(cfg: __TestConfig) {
let _ = pretty_env_logger::try_init();
runtime().block_on(async_test(cfg));
}
async fn async_test(cfg: __TestConfig) {