Skip to content

Commit c3ee6af

Browse files
committed
use unused port for tests
1 parent b45bf6b commit c3ee6af

File tree

5 files changed

+16
-17
lines changed

5 files changed

+16
-17
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ structopt = "0.3.3"
6262
surf = "2.0.0-alpha.1"
6363
futures = "0.3.1"
6464
femme = "1.3.0"
65+
portpicker = "0.1.0"
6566

6667
[[test]]
6768
name = "nested"

tests/chunked-encode-large.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const TEXT: &'static str = concat![
6868

6969
#[async_std::test]
7070
async fn chunked_large() -> Result<(), http_types::Error> {
71-
let bind = test_utils::find_port().await;
71+
let port = test_utils::find_port().await;
7272
let server = task::spawn(async move {
7373
let mut app = tide::new();
7474
app.at("/").get(|mut _req: tide::Request<()>| async {
@@ -78,13 +78,13 @@ async fn chunked_large() -> Result<(), http_types::Error> {
7878
.set_header(headers::CONTENT_TYPE, "text/plain; charset=utf-8");
7979
Ok(res)
8080
});
81-
app.listen(&bind).await?;
81+
app.listen(("localhost", port)).await?;
8282
Result::<(), http_types::Error>::Ok(())
8383
});
8484

8585
let client = task::spawn(async move {
8686
task::sleep(Duration::from_millis(100)).await;
87-
let mut res = surf::get(format!("http://{}", bind)).await?;
87+
let mut res = surf::get(format!("http://localhost:{}", port)).await?;
8888
assert_eq!(res.status(), 200);
8989
assert_eq!(
9090
res.header(&"transfer-encoding".parse().unwrap()),

tests/chunked-encode-small.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ async fn chunked_large() -> Result<(), http_types::Error> {
2727
.set_header(headers::CONTENT_TYPE, "text/plain; charset=utf-8");
2828
Ok(res)
2929
});
30-
app.listen(&port).await?;
30+
app.listen(("localhost", port)).await?;
3131
Result::<(), http_types::Error>::Ok(())
3232
});
3333

3434
let client = task::spawn(async move {
3535
task::sleep(Duration::from_millis(100)).await;
36-
let mut res = surf::get(format!("http://{}", port)).await?;
36+
let mut res = surf::get(format!("http://localhost:{}", port)).await?;
3737
assert_eq!(res.status(), 200);
3838
assert_eq!(
3939
res.header(&"transfer-encoding".parse().unwrap()),

tests/server.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ fn hello_world() -> Result<(), http_types::Error> {
1717
let res = Response::new(StatusCode::Ok).body_string("says hello".to_string());
1818
Ok(res)
1919
});
20-
app.listen(&port).await?;
20+
app.listen(("localhost", port)).await?;
2121
Result::<(), http_types::Error>::Ok(())
2222
});
2323

2424
let client = task::spawn(async move {
2525
task::sleep(Duration::from_millis(100)).await;
26-
let string = surf::get(format!("http://{}", port))
26+
let string = surf::get(format!("http://localhost:{}", port))
2727
.body_string("nori".to_string())
2828
.recv_string()
2929
.await?;
@@ -43,13 +43,13 @@ fn echo_server() -> Result<(), http_types::Error> {
4343
let mut app = tide::new();
4444
app.at("/").get(|req| async move { Ok(req) });
4545

46-
app.listen(&port).await?;
46+
app.listen(("localhost", port)).await?;
4747
Result::<(), http_types::Error>::Ok(())
4848
});
4949

5050
let client = task::spawn(async move {
5151
task::sleep(Duration::from_millis(100)).await;
52-
let string = surf::get(format!("http://{}", port))
52+
let string = surf::get(format!("http://localhost:{}", port))
5353
.body_string("chashu".to_string())
5454
.recv_string()
5555
.await?;
@@ -79,13 +79,13 @@ fn json() -> Result<(), http_types::Error> {
7979
let res = Response::new(StatusCode::Ok).body_json(&counter)?;
8080
Ok(res)
8181
});
82-
app.listen(&port).await?;
82+
app.listen(("localhost", port)).await?;
8383
Result::<(), http_types::Error>::Ok(())
8484
});
8585

8686
let client = task::spawn(async move {
8787
task::sleep(Duration::from_millis(100)).await;
88-
let counter: Counter = surf::get(format!("http://{}", &port))
88+
let counter: Counter = surf::get(format!("http://localhost:{}", &port))
8989
.body_json(&Counter { count: 0 })?
9090
.recv_json()
9191
.await?;

tests/test_utils.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
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()
1+
use portpicker::pick_unused_port;
2+
3+
pub async fn find_port() -> u16 {
4+
pick_unused_port().expect("No ports free")
75
}

0 commit comments

Comments
 (0)