Skip to content

Commit 588d2f1

Browse files
authored
Merge pull request #446 from jbr/port-binding-in-tests
bind to an unused port in integration tests
2 parents c61d477 + d7ab2d9 commit 588d2f1

File tree

4 files changed

+36
-21
lines changed

4 files changed

+36
-21
lines changed

tests/chunked-encode-large.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod test_utils;
12
use async_std::io::Cursor;
23
use async_std::prelude::*;
34
use async_std::task;
@@ -67,22 +68,23 @@ const TEXT: &'static str = concat![
6768

6869
#[async_std::test]
6970
async fn chunked_large() -> Result<(), http_types::Error> {
70-
let server = task::spawn(async {
71+
let bind = test_utils::find_port().await;
72+
let server = task::spawn(async move {
7173
let mut app = tide::new();
72-
app.at("/").get(|mut _req: tide::Request<()>| async move {
74+
app.at("/").get(|mut _req: tide::Request<()>| async {
7375
let body = Cursor::new(TEXT.to_owned());
7476
let res = Response::new(StatusCode::Ok)
7577
.body(body)
7678
.set_header(headers::CONTENT_TYPE, "text/plain; charset=utf-8");
7779
Ok(res)
7880
});
79-
app.listen("localhost:8080").await?;
81+
app.listen(&bind).await?;
8082
Result::<(), http_types::Error>::Ok(())
8183
});
8284

83-
let client = task::spawn(async {
85+
let client = task::spawn(async move {
8486
task::sleep(Duration::from_millis(100)).await;
85-
let mut res = surf::get("http://localhost:8080").await?;
87+
let mut res = surf::get(format!("http://{}", bind)).await?;
8688
assert_eq!(res.status(), 200);
8789
assert_eq!(
8890
res.header(&"transfer-encoding".parse().unwrap()),

tests/chunked-encode-small.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod test_utils;
12
use async_std::io::Cursor;
23
use async_std::prelude::*;
34
use async_std::task;
@@ -16,7 +17,8 @@ const TEXT: &'static str = concat![
1617

1718
#[async_std::test]
1819
async fn chunked_large() -> Result<(), http_types::Error> {
19-
let server = task::spawn(async {
20+
let port = test_utils::find_port().await;
21+
let server = task::spawn(async move {
2022
let mut app = tide::new();
2123
app.at("/").get(|mut _req: tide::Request<()>| async move {
2224
let body = Cursor::new(TEXT.to_owned());
@@ -25,13 +27,13 @@ async fn chunked_large() -> Result<(), http_types::Error> {
2527
.set_header(headers::CONTENT_TYPE, "text/plain; charset=utf-8");
2628
Ok(res)
2729
});
28-
app.listen("localhost:8080").await?;
30+
app.listen(&port).await?;
2931
Result::<(), http_types::Error>::Ok(())
3032
});
3133

32-
let client = task::spawn(async {
34+
let client = task::spawn(async move {
3335
task::sleep(Duration::from_millis(100)).await;
34-
let mut res = surf::get("http://localhost:8080").await?;
36+
let mut res = surf::get(format!("http://{}", port)).await?;
3537
assert_eq!(res.status(), 200);
3638
assert_eq!(
3739
res.header(&"transfer-encoding".parse().unwrap()),

tests/server.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod test_utils;
12
use async_std::prelude::*;
23
use async_std::task;
34
use std::time::Duration;
@@ -8,20 +9,21 @@ use tide::{Request, Response, StatusCode};
89
#[test]
910
fn hello_world() -> Result<(), http_types::Error> {
1011
task::block_on(async {
11-
let server = task::spawn(async {
12+
let port = test_utils::find_port().await;
13+
let server = task::spawn(async move {
1214
let mut app = tide::new();
1315
app.at("/").get(|mut req: Request<()>| async move {
1416
assert_eq!(req.body_string().await.unwrap(), "nori".to_string());
1517
let res = Response::new(StatusCode::Ok).body_string("says hello".to_string());
1618
Ok(res)
1719
});
18-
app.listen("localhost:8080").await?;
20+
app.listen(&port).await?;
1921
Result::<(), http_types::Error>::Ok(())
2022
});
2123

22-
let client = task::spawn(async {
24+
let client = task::spawn(async move {
2325
task::sleep(Duration::from_millis(100)).await;
24-
let string = surf::get("http://localhost:8080")
26+
let string = surf::get(format!("http://{}", port))
2527
.body_string("nori".to_string())
2628
.recv_string()
2729
.await?;
@@ -36,17 +38,18 @@ fn hello_world() -> Result<(), http_types::Error> {
3638
#[test]
3739
fn echo_server() -> Result<(), http_types::Error> {
3840
task::block_on(async {
39-
let server = task::spawn(async {
41+
let port = test_utils::find_port().await;
42+
let server = task::spawn(async move {
4043
let mut app = tide::new();
4144
app.at("/").get(|req| async move { Ok(req) });
4245

43-
app.listen("localhost:8081").await?;
46+
app.listen(&port).await?;
4447
Result::<(), http_types::Error>::Ok(())
4548
});
4649

47-
let client = task::spawn(async {
50+
let client = task::spawn(async move {
4851
task::sleep(Duration::from_millis(100)).await;
49-
let string = surf::get("http://localhost:8081")
52+
let string = surf::get(format!("http://{}", port))
5053
.body_string("chashu".to_string())
5154
.recv_string()
5255
.await?;
@@ -66,7 +69,8 @@ fn json() -> Result<(), http_types::Error> {
6669
}
6770

6871
task::block_on(async {
69-
let server = task::spawn(async {
72+
let port = test_utils::find_port().await;
73+
let server = task::spawn(async move {
7074
let mut app = tide::new();
7175
app.at("/").get(|mut req: Request<()>| async move {
7276
let mut counter: Counter = req.body_json().await.unwrap();
@@ -75,13 +79,13 @@ fn json() -> Result<(), http_types::Error> {
7579
let res = Response::new(StatusCode::Ok).body_json(&counter)?;
7680
Ok(res)
7781
});
78-
app.listen("localhost:8082").await?;
82+
app.listen(&port).await?;
7983
Result::<(), http_types::Error>::Ok(())
8084
});
8185

82-
let client = task::spawn(async {
86+
let client = task::spawn(async move {
8387
task::sleep(Duration::from_millis(100)).await;
84-
let counter: Counter = surf::get("http://localhost:8082")
88+
let counter: Counter = surf::get(format!("http://{}", &port))
8589
.body_json(&Counter { count: 0 })?
8690
.recv_json()
8791
.await?;

tests/test_utils.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub async fn find_port() -> async_std::net::SocketAddr {
2+
async_std::net::TcpListener::bind("localhost:0")
3+
.await
4+
.unwrap()
5+
.local_addr()
6+
.unwrap()
7+
}

0 commit comments

Comments
 (0)