Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bind to an unused port in integration tests #446

Merged
merged 2 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions tests/chunked-encode-large.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod test_utils;
use async_std::io::Cursor;
use async_std::prelude::*;
use async_std::task;
Expand Down Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename the bind variable to port?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would socket_addr work? I initially had port (as still remains in the function name, which will also be fixed), but it's both localhost as well as the port

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()),
Expand Down
10 changes: 6 additions & 4 deletions tests/chunked-encode-small.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod test_utils;
use async_std::io::Cursor;
use async_std::prelude::*;
use async_std::task;
Expand All @@ -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());
Expand All @@ -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()),
Expand Down
28 changes: 16 additions & 12 deletions tests/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod test_utils;
use async_std::prelude::*;
use async_std::task;
use http_types::StatusCode;
Expand All @@ -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?;
Expand All @@ -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?;
Expand All @@ -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();
Expand All @@ -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?;
Expand Down
7 changes: 7 additions & 0 deletions tests/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub async fn determine_port_to_bind() -> async_std::net::SocketAddr {
Copy link
Member

@yoshuawuyts yoshuawuyts Apr 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more concise name would be find_port, would you mind changing that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as with above comment, would find_socket_addr be reasonably concise and clear? it's really something like "find a port and return a socket addr with that port" but that's awkward af

async_std::net::TcpListener::bind("localhost:0")
.await
.unwrap()
.local_addr()
.unwrap()
}