Skip to content

Commit 65d8bdb

Browse files
committed
more tests
1 parent ef21699 commit 65d8bdb

File tree

3 files changed

+114
-103
lines changed

3 files changed

+114
-103
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ http-body-util = "0.1.0"
3737
tokio = { version = "1", features = ["macros", "test-util", "tracing"] }
3838
tokio-test = "0.4"
3939
pretty_env_logger = "0.5"
40-
console-subscriber = "*"
4140

4241
[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dev-dependencies]
4342
pnet_datalink = "0.34.0"

tests/legacy_client.rs

Lines changed: 114 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use futures_channel::{mpsc, oneshot};
1212
use futures_util::future::{self, FutureExt, TryFutureExt};
1313
use futures_util::stream::StreamExt;
1414
use futures_util::{self, Stream};
15-
use http_body_util::{BodyStream, Empty, Full};
15+
use http_body_util::BodyExt;
16+
use http_body_util::{Empty, Full};
1617

1718
use hyper::body::Bytes;
1819
use hyper::Request;
@@ -148,6 +149,7 @@ async fn drop_client_closes_idle_connections() {
148149
future::select(t, close).await;
149150
}
150151

152+
#[cfg(not(miri))]
151153
#[tokio::test]
152154
async fn drop_response_future_closes_in_progress_connection() {
153155
let _ = pretty_env_logger::try_init();
@@ -196,6 +198,7 @@ async fn drop_response_future_closes_in_progress_connection() {
196198
future::select(t, close).await;
197199
}
198200

201+
#[cfg(not(miri))]
199202
#[tokio::test]
200203
async fn drop_response_body_closes_in_progress_connection() {
201204
let _ = pretty_env_logger::try_init();
@@ -251,6 +254,7 @@ async fn drop_response_body_closes_in_progress_connection() {
251254
future::select(t, close).await;
252255
}
253256

257+
#[cfg(not(miri))]
254258
#[tokio::test]
255259
async fn no_keep_alive_closes_connection() {
256260
// https://github.com/hyperium/hyper/issues/1383
@@ -303,6 +307,7 @@ async fn no_keep_alive_closes_connection() {
303307
future::select(close, t).await;
304308
}
305309

310+
#[cfg(not(miri))]
306311
#[tokio::test]
307312
async fn socket_disconnect_closes_idle_conn() {
308313
// notably when keep-alive is enabled
@@ -349,6 +354,7 @@ async fn socket_disconnect_closes_idle_conn() {
349354
future::select(t, close).await;
350355
}
351356

357+
#[cfg(not(miri))]
352358
#[test]
353359
fn connect_call_is_lazy() {
354360
// We especially don't want connects() triggered if there's
@@ -372,6 +378,7 @@ fn connect_call_is_lazy() {
372378
assert_eq!(connects.load(Ordering::Relaxed), 0);
373379
}
374380

381+
#[cfg(not(miri))]
375382
#[test]
376383
fn client_keep_alive_0() {
377384
let _ = pretty_env_logger::try_init();
@@ -438,6 +445,7 @@ fn client_keep_alive_0() {
438445
drop(client);
439446
}
440447

448+
#[cfg(not(miri))]
441449
#[test]
442450
fn client_keep_alive_extra_body() {
443451
let _ = pretty_env_logger::try_init();
@@ -509,8 +517,9 @@ fn client_keep_alive_extra_body() {
509517
//
510518
// let connector = DebugConnector::new();
511519
// let connects = connector.connects.clone();
520+
// let executor = TokioExecutor::new();
512521
//
513-
// let client = Client::builder(TokioExecutor::new()).build(connector);
522+
// let client = Client::builder(executor.clone()).build(connector.clone());
514523
//
515524
// let (tx1, rx1) = oneshot::channel();
516525
// let (tx2, rx2) = oneshot::channel();
@@ -542,126 +551,128 @@ fn client_keep_alive_extra_body() {
542551
// assert_eq!(connects.load(Ordering::Relaxed), 0);
543552
//
544553
// let delayed_body = rx1
545-
// .then(|_| tokio::time::sleep(Duration::from_millis(200)))
546-
// .map(|_| Ok::<_, ()>("hello a"))
554+
// .then(|_| Box::pin(tokio::time::sleep(Duration::from_millis(200))))
555+
// .map(|_| Ok::<_, ()>(Frame::data(&b"hello a"[..])))
547556
// .map_err(|_| -> hyper::Error { panic!("rx1") })
548557
// .into_stream();
549558
//
550-
// let rx = rx2;
551559
// let req = Request::builder()
552560
// .method("POST")
553561
// .uri(&*format!("http://{}/a", addr))
554-
// .body(BodyStream::new(delayed_body))
562+
// .body(StreamBody::new(delayed_body))
555563
// .unwrap();
556-
// let client2 = client.clone();
557-
//
564+
// let client2 = Client::builder(executor.clone()).build(connector.clone());
565+
// let res = client.request(req).map_ok(move |res| {
566+
// assert_eq!(res.status(), hyper::StatusCode::OK);
567+
// });
558568
// // req 1
559-
// let fut = future::join(client.request(req), rx)
569+
// let fut = future::join(res, rx2)
560570
// .then(|_| tokio::time::sleep(Duration::from_millis(200)))
561-
// // req 2
571+
// // req 2
562572
// .then(move |()| {
563-
// let rx = rx3.expect("thread panicked");
573+
// let rx = rx3;
564574
// let req = Request::builder()
565575
// .uri(&*format!("http://{}/b", addr))
566576
// .body(Empty::<Bytes>::new())
567577
// .unwrap();
568-
// future::join(client2.request(req), rx).map(|r| r.0)
578+
// let res = client2.request(req).map_ok(move |res| {
579+
// assert_eq!(res.status(), hyper::StatusCode::OK);
580+
// });
581+
// future::join(res, rx).map(|r| r.0)
569582
// });
570583
//
571584
// rt.block_on(fut).unwrap();
572585
//
573586
// assert_eq!(connects.load(Ordering::Relaxed), 1);
574587
// }
575588

576-
// #[tokio::test]
577-
// async fn client_keep_alive_eager_when_chunked() {
578-
// // If a response body has been read to completion, with completion
579-
// // determined by some other factor, like decompression, and thus
580-
// // it is in't polled a final time to clear the final 0-len chunk,
581-
// // try to eagerly clear it so the connection can still be used.
582-
//
583-
// let _ = pretty_env_logger::try_init();
584-
// let server = TcpListener::bind("127.0.0.1:0").unwrap();
585-
// let addr = server.local_addr().unwrap();
586-
// let connector = DebugConnector::new();
587-
// let connects = connector.connects.clone();
588-
//
589-
// let client = Client::builder(TokioExecutor::new()).build(connector);
590-
//
591-
// let (tx1, rx1) = oneshot::channel();
592-
// let (tx2, rx2) = oneshot::channel();
593-
// thread::spawn(move || {
594-
// let mut sock = server.accept().unwrap().0;
595-
// //drop(server);
596-
// sock.set_read_timeout(Some(Duration::from_secs(5))).unwrap();
597-
// sock.set_write_timeout(Some(Duration::from_secs(5)))
598-
// .unwrap();
599-
// let mut buf = [0; 4096];
600-
// sock.read(&mut buf).expect("read 1");
601-
// sock.write_all(
602-
// b"\
603-
// HTTP/1.1 200 OK\r\n\
604-
// transfer-encoding: chunked\r\n\
605-
// \r\n\
606-
// 5\r\n\
607-
// hello\r\n\
608-
// 0\r\n\r\n\
609-
// ",
610-
// )
611-
// .expect("write 1");
612-
// let _ = tx1.send(());
613-
//
614-
// let n2 = sock.read(&mut buf).expect("read 2");
615-
// assert_ne!(n2, 0, "bytes of second request");
616-
// let second_get = "GET /b HTTP/1.1\r\n";
617-
// assert_eq!(s(&buf[..second_get.len()]), second_get);
618-
// sock.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n")
619-
// .expect("write 2");
620-
// let _ = tx2.send(());
621-
// });
622-
//
623-
// assert_eq!(connects.load(Ordering::SeqCst), 0);
624-
//
625-
// let rx = rx1;
626-
// let req = Request::builder()
627-
// .uri(&*format!("http://{}/a", addr))
628-
// .body(Empty::<Bytes>::new())
629-
// .unwrap();
630-
// let fut = client.request(req);
631-
//
632-
// let mut resp = future::join(fut, rx).map(|r| r.0).await.unwrap();
633-
// assert_eq!(connects.load(Ordering::SeqCst), 1);
634-
// assert_eq!(resp.status(), 200);
635-
// assert_eq!(resp.headers()["transfer-encoding"], "chunked");
636-
//
637-
// // Read the "hello" chunk...
638-
// let chunk = resp.body_mut().data().await.unwrap().unwrap();
639-
// assert_eq!(chunk, "hello");
640-
//
641-
// // With our prior knowledge, we know that's the end of the body.
642-
// // So just drop the body, without polling for the `0\r\n\r\n` end.
643-
// drop(resp);
644-
//
645-
// // sleep real quick to let the threadpool put connection in ready
646-
// // state and back into client pool
647-
// tokio::time::sleep(Duration::from_millis(50)).await;
648-
//
649-
// let rx = rx2;
650-
// let req = Request::builder()
651-
// .uri(&*format!("http://{}/b", addr))
652-
// .body(Empty::<Bytes>::new())
653-
// .unwrap();
654-
// let fut = client.request(req);
655-
// future::join(fut, rx).map(|r| r.0).await.unwrap();
656-
//
657-
// assert_eq!(
658-
// connects.load(Ordering::SeqCst),
659-
// 1,
660-
// "second request should still only have 1 connect"
661-
// );
662-
// drop(client);
663-
// }
589+
#[cfg(not(miri))]
590+
#[tokio::test]
591+
async fn client_keep_alive_eager_when_chunked() {
592+
// If a response body has been read to completion, with completion
593+
// determined by some other factor, like decompression, and thus
594+
// it is in't polled a final time to clear the final 0-len chunk,
595+
// try to eagerly clear it so the connection can still be used.
596+
597+
let _ = pretty_env_logger::try_init();
598+
let server = TcpListener::bind("127.0.0.1:0").unwrap();
599+
let addr = server.local_addr().unwrap();
600+
let connector = DebugConnector::new();
601+
let connects = connector.connects.clone();
602+
603+
let client = Client::builder(TokioExecutor::new()).build(connector);
604+
605+
let (tx1, rx1) = oneshot::channel();
606+
let (tx2, rx2) = oneshot::channel();
607+
thread::spawn(move || {
608+
let mut sock = server.accept().unwrap().0;
609+
//drop(server);
610+
sock.set_read_timeout(Some(Duration::from_secs(5))).unwrap();
611+
sock.set_write_timeout(Some(Duration::from_secs(5)))
612+
.unwrap();
613+
let mut buf = [0; 4096];
614+
sock.read(&mut buf).expect("read 1");
615+
sock.write_all(
616+
b"\
617+
HTTP/1.1 200 OK\r\n\
618+
transfer-encoding: chunked\r\n\
619+
\r\n\
620+
5\r\n\
621+
hello\r\n\
622+
0\r\n\r\n\
623+
",
624+
)
625+
.expect("write 1");
626+
let _ = tx1.send(());
627+
628+
let n2 = sock.read(&mut buf).expect("read 2");
629+
assert_ne!(n2, 0, "bytes of second request");
630+
let second_get = "GET /b HTTP/1.1\r\n";
631+
assert_eq!(s(&buf[..second_get.len()]), second_get);
632+
sock.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n")
633+
.expect("write 2");
634+
let _ = tx2.send(());
635+
});
636+
637+
assert_eq!(connects.load(Ordering::SeqCst), 0);
638+
639+
let rx = rx1;
640+
let req = Request::builder()
641+
.uri(&*format!("http://{}/a", addr))
642+
.body(Empty::<Bytes>::new())
643+
.unwrap();
644+
let fut = client.request(req);
645+
646+
let mut resp = future::join(fut, rx).map(|r| r.0).await.unwrap();
647+
assert_eq!(connects.load(Ordering::SeqCst), 1);
648+
assert_eq!(resp.status(), 200);
649+
assert_eq!(resp.headers()["transfer-encoding"], "chunked");
650+
651+
// Read the "hello" chunk...
652+
let chunk = resp.collect().await.unwrap().to_bytes();
653+
assert_eq!(chunk, "hello");
664654

655+
// sleep real quick to let the threadpool put connection in ready
656+
// state and back into client pool
657+
tokio::time::sleep(Duration::from_millis(50)).await;
658+
659+
let rx = rx2;
660+
let req = Request::builder()
661+
.uri(&*format!("http://{}/b", addr))
662+
.body(Empty::<Bytes>::new())
663+
.unwrap();
664+
let fut = client.request(req);
665+
future::join(fut, rx).map(|r| r.0).await.unwrap();
666+
667+
assert_eq!(
668+
connects.load(Ordering::SeqCst),
669+
1,
670+
"second request should still only have 1 connect"
671+
);
672+
drop(client);
673+
}
674+
675+
#[cfg(not(miri))]
665676
#[test]
666677
fn connect_proxy_sends_absolute_uri() {
667678
let _ = pretty_env_logger::try_init();
@@ -701,6 +712,7 @@ fn connect_proxy_sends_absolute_uri() {
701712
rt.block_on(future::join(res, rx).map(|r| r.0)).unwrap();
702713
}
703714

715+
#[cfg(not(miri))]
704716
#[test]
705717
fn connect_proxy_http_connect_sends_authority_form() {
706718
let _ = pretty_env_logger::try_init();
@@ -741,6 +753,7 @@ fn connect_proxy_http_connect_sends_authority_form() {
741753
rt.block_on(future::join(res, rx).map(|r| r.0)).unwrap();
742754
}
743755

756+
#[cfg(not(miri))]
744757
#[test]
745758
fn client_upgrade() {
746759
use tokio::io::{AsyncReadExt, AsyncWriteExt};
@@ -802,6 +815,7 @@ fn client_upgrade() {
802815
assert_eq!(vec, b"bar=foo");
803816
}
804817

818+
#[cfg(not(miri))]
805819
#[test]
806820
fn alpn_h2() {
807821
use http::Response;

tests/test_utils/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use tokio::io::{self, AsyncRead, AsyncWrite, ReadBuf};
1111
use tokio::net::TcpStream;
1212

1313
use hyper::rt::ReadBufCursor;
14-
use hyper::rt::{Read, Write};
1514

1615
use hyper_util::client::legacy::connect::HttpConnector;
1716
use hyper_util::client::legacy::connect::{Connected, Connection};
@@ -82,7 +81,6 @@ pub struct DebugStream {
8281

8382
impl Drop for DebugStream {
8483
fn drop(&mut self) {
85-
println!("DROPPING STREAM");
8684
let _ = self.on_drop.try_send(());
8785
}
8886
}

0 commit comments

Comments
 (0)