From 9f5a54fe2356004f408eee48cf50c1da1b19c1ca Mon Sep 17 00:00:00 2001 From: Jacob Rothstein Date: Mon, 20 Apr 2020 17:43:11 -0700 Subject: [PATCH 1/2] bind to an unused port in integration tests --- tests/chunked-encode-large.rs | 12 +++++++----- tests/chunked-encode-small.rs | 10 ++++++---- tests/server.rs | 28 ++++++++++++++++------------ tests/test_utils.rs | 7 +++++++ 4 files changed, 36 insertions(+), 21 deletions(-) create mode 100644 tests/test_utils.rs diff --git a/tests/chunked-encode-large.rs b/tests/chunked-encode-large.rs index 3f8d443d3..240f36f68 100644 --- a/tests/chunked-encode-large.rs +++ b/tests/chunked-encode-large.rs @@ -1,3 +1,4 @@ +mod test_utils; use async_std::io::Cursor; use async_std::prelude::*; use async_std::task; @@ -67,22 +68,23 @@ const TEXT: &'static str = concat![ #[async_std::test] async fn chunked_large() -> Result<(), http_types::Error> { - let server = task::spawn(async { + let bind = test_utils::determine_port_to_bind().await; + let server = task::spawn(async move { let mut app = tide::new(); - app.at("/").get(|mut _req: tide::Request<()>| async move { + app.at("/").get(|mut _req: tide::Request<()>| async { let body = Cursor::new(TEXT.to_owned()); let res = Response::new(StatusCode::Ok) .body(body) .set_header(headers::CONTENT_TYPE, "text/plain; charset=utf-8"); Ok(res) }); - app.listen("localhost:8080").await?; + app.listen(&bind).await?; Result::<(), http_types::Error>::Ok(()) }); - let client = task::spawn(async { + let client = task::spawn(async move { task::sleep(Duration::from_millis(100)).await; - let mut res = surf::get("http://localhost:8080").await?; + let mut res = surf::get(format!("http://{}", bind)).await?; assert_eq!(res.status(), 200); assert_eq!( res.header(&"transfer-encoding".parse().unwrap()), diff --git a/tests/chunked-encode-small.rs b/tests/chunked-encode-small.rs index 673f3ee9c..783a9d47b 100644 --- a/tests/chunked-encode-small.rs +++ b/tests/chunked-encode-small.rs @@ -1,3 +1,4 @@ +mod test_utils; use async_std::io::Cursor; use async_std::prelude::*; use async_std::task; @@ -16,7 +17,8 @@ const TEXT: &'static str = concat![ #[async_std::test] async fn chunked_large() -> Result<(), http_types::Error> { - let server = task::spawn(async { + let bind = test_utils::determine_port_to_bind().await; + let server = task::spawn(async move { let mut app = tide::new(); app.at("/").get(|mut _req: tide::Request<()>| async move { let body = Cursor::new(TEXT.to_owned()); @@ -25,13 +27,13 @@ async fn chunked_large() -> Result<(), http_types::Error> { .set_header(headers::CONTENT_TYPE, "text/plain; charset=utf-8"); Ok(res) }); - app.listen("localhost:8080").await?; + app.listen(&bind).await?; Result::<(), http_types::Error>::Ok(()) }); - let client = task::spawn(async { + let client = task::spawn(async move { task::sleep(Duration::from_millis(100)).await; - let mut res = surf::get("http://localhost:8080").await?; + let mut res = surf::get(format!("http://{}", bind)).await?; assert_eq!(res.status(), 200); assert_eq!( res.header(&"transfer-encoding".parse().unwrap()), diff --git a/tests/server.rs b/tests/server.rs index d7e3030af..3ddef6559 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -1,3 +1,4 @@ +mod test_utils; use async_std::prelude::*; use async_std::task; use http_types::StatusCode; @@ -8,20 +9,21 @@ use serde::{Deserialize, Serialize}; #[test] fn hello_world() -> Result<(), http_types::Error> { task::block_on(async { - let server = task::spawn(async { + let bind = test_utils::determine_port_to_bind().await; + let server = task::spawn(async move { let mut app = tide::new(); app.at("/").get(|mut req: tide::Request<()>| async move { assert_eq!(req.body_string().await.unwrap(), "nori".to_string()); let res = tide::Response::new(StatusCode::Ok).body_string("says hello".to_string()); Ok(res) }); - app.listen("localhost:8080").await?; + app.listen(&bind).await?; Result::<(), http_types::Error>::Ok(()) }); - let client = task::spawn(async { + let client = task::spawn(async move { task::sleep(Duration::from_millis(100)).await; - let string = surf::get("http://localhost:8080") + let string = surf::get(format!("http://{}", bind)) .body_string("nori".to_string()) .recv_string() .await?; @@ -36,17 +38,18 @@ fn hello_world() -> Result<(), http_types::Error> { #[test] fn echo_server() -> Result<(), http_types::Error> { task::block_on(async { - let server = task::spawn(async { + let bind = test_utils::determine_port_to_bind().await; + let server = task::spawn(async move { let mut app = tide::new(); app.at("/").get(|req| async move { Ok(req) }); - app.listen("localhost:8081").await?; + app.listen(&bind).await?; Result::<(), http_types::Error>::Ok(()) }); - let client = task::spawn(async { + let client = task::spawn(async move { task::sleep(Duration::from_millis(100)).await; - let string = surf::get("http://localhost:8081") + let string = surf::get(format!("http://{}", bind)) .body_string("chashu".to_string()) .recv_string() .await?; @@ -66,7 +69,8 @@ fn json() -> Result<(), http_types::Error> { } task::block_on(async { - let server = task::spawn(async { + let bind = test_utils::determine_port_to_bind().await; + let server = task::spawn(async move { let mut app = tide::new(); app.at("/").get(|mut req: tide::Request<()>| async move { let mut counter: Counter = req.body_json().await.unwrap(); @@ -75,13 +79,13 @@ fn json() -> Result<(), http_types::Error> { let res = tide::Response::new(StatusCode::Ok).body_json(&counter)?; Ok(res) }); - app.listen("localhost:8082").await?; + app.listen(&bind).await?; Result::<(), http_types::Error>::Ok(()) }); - let client = task::spawn(async { + let client = task::spawn(async move { task::sleep(Duration::from_millis(100)).await; - let counter: Counter = surf::get("http://localhost:8082") + let counter: Counter = surf::get(format!("http://{}", &bind)) .body_json(&Counter { count: 0 })? .recv_json() .await?; diff --git a/tests/test_utils.rs b/tests/test_utils.rs new file mode 100644 index 000000000..1d3e9392a --- /dev/null +++ b/tests/test_utils.rs @@ -0,0 +1,7 @@ +pub async fn determine_port_to_bind() -> async_std::net::SocketAddr { + async_std::net::TcpListener::bind("localhost:0") + .await + .unwrap() + .local_addr() + .unwrap() +} From d7ab2d94012e25d0df30a553b47154e2630287d3 Mon Sep 17 00:00:00 2001 From: Jacob Rothstein Date: Thu, 23 Apr 2020 13:25:03 -0700 Subject: [PATCH 2/2] rename `determine_port_to_bind` to `find_port` and rename `bind` variable names to `port` --- tests/chunked-encode-large.rs | 2 +- tests/chunked-encode-small.rs | 6 +++--- tests/server.rs | 18 +++++++++--------- tests/test_utils.rs | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/chunked-encode-large.rs b/tests/chunked-encode-large.rs index 240f36f68..a3d894945 100644 --- a/tests/chunked-encode-large.rs +++ b/tests/chunked-encode-large.rs @@ -68,7 +68,7 @@ const TEXT: &'static str = concat![ #[async_std::test] async fn chunked_large() -> Result<(), http_types::Error> { - let bind = test_utils::determine_port_to_bind().await; + let bind = test_utils::find_port().await; let server = task::spawn(async move { let mut app = tide::new(); app.at("/").get(|mut _req: tide::Request<()>| async { diff --git a/tests/chunked-encode-small.rs b/tests/chunked-encode-small.rs index 783a9d47b..bcf17957f 100644 --- a/tests/chunked-encode-small.rs +++ b/tests/chunked-encode-small.rs @@ -17,7 +17,7 @@ const TEXT: &'static str = concat![ #[async_std::test] async fn chunked_large() -> Result<(), http_types::Error> { - let bind = test_utils::determine_port_to_bind().await; + let port = test_utils::find_port().await; let server = task::spawn(async move { let mut app = tide::new(); app.at("/").get(|mut _req: tide::Request<()>| async move { @@ -27,13 +27,13 @@ async fn chunked_large() -> Result<(), http_types::Error> { .set_header(headers::CONTENT_TYPE, "text/plain; charset=utf-8"); Ok(res) }); - app.listen(&bind).await?; + app.listen(&port).await?; Result::<(), http_types::Error>::Ok(()) }); let client = task::spawn(async move { task::sleep(Duration::from_millis(100)).await; - let mut res = surf::get(format!("http://{}", bind)).await?; + let mut res = surf::get(format!("http://{}", port)).await?; assert_eq!(res.status(), 200); assert_eq!( res.header(&"transfer-encoding".parse().unwrap()), diff --git a/tests/server.rs b/tests/server.rs index 3ddef6559..d64f2fb25 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize}; #[test] fn hello_world() -> Result<(), http_types::Error> { task::block_on(async { - let bind = test_utils::determine_port_to_bind().await; + let port = test_utils::find_port().await; let server = task::spawn(async move { let mut app = tide::new(); app.at("/").get(|mut req: tide::Request<()>| async move { @@ -17,13 +17,13 @@ fn hello_world() -> Result<(), http_types::Error> { let res = tide::Response::new(StatusCode::Ok).body_string("says hello".to_string()); Ok(res) }); - app.listen(&bind).await?; + app.listen(&port).await?; Result::<(), http_types::Error>::Ok(()) }); let client = task::spawn(async move { task::sleep(Duration::from_millis(100)).await; - let string = surf::get(format!("http://{}", bind)) + let string = surf::get(format!("http://{}", port)) .body_string("nori".to_string()) .recv_string() .await?; @@ -38,18 +38,18 @@ fn hello_world() -> Result<(), http_types::Error> { #[test] fn echo_server() -> Result<(), http_types::Error> { task::block_on(async { - let bind = test_utils::determine_port_to_bind().await; + let port = test_utils::find_port().await; let server = task::spawn(async move { let mut app = tide::new(); app.at("/").get(|req| async move { Ok(req) }); - app.listen(&bind).await?; + app.listen(&port).await?; Result::<(), http_types::Error>::Ok(()) }); let client = task::spawn(async move { task::sleep(Duration::from_millis(100)).await; - let string = surf::get(format!("http://{}", bind)) + let string = surf::get(format!("http://{}", port)) .body_string("chashu".to_string()) .recv_string() .await?; @@ -69,7 +69,7 @@ fn json() -> Result<(), http_types::Error> { } task::block_on(async { - let bind = test_utils::determine_port_to_bind().await; + let port = test_utils::find_port().await; let server = task::spawn(async move { let mut app = tide::new(); app.at("/").get(|mut req: tide::Request<()>| async move { @@ -79,13 +79,13 @@ fn json() -> Result<(), http_types::Error> { let res = tide::Response::new(StatusCode::Ok).body_json(&counter)?; Ok(res) }); - app.listen(&bind).await?; + app.listen(&port).await?; Result::<(), http_types::Error>::Ok(()) }); let client = task::spawn(async move { task::sleep(Duration::from_millis(100)).await; - let counter: Counter = surf::get(format!("http://{}", &bind)) + let counter: Counter = surf::get(format!("http://{}", &port)) .body_json(&Counter { count: 0 })? .recv_json() .await?; diff --git a/tests/test_utils.rs b/tests/test_utils.rs index 1d3e9392a..e9fceab05 100644 --- a/tests/test_utils.rs +++ b/tests/test_utils.rs @@ -1,4 +1,4 @@ -pub async fn determine_port_to_bind() -> async_std::net::SocketAddr { +pub async fn find_port() -> async_std::net::SocketAddr { async_std::net::TcpListener::bind("localhost:0") .await .unwrap()