Skip to content

Commit

Permalink
poll test server instead of sleeping
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Dec 22, 2023
1 parent c202c76 commit b59951e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dwn-server/tests/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use reqwest::StatusCode;

#[tokio::test]
async fn recieve_post() {
let port = spawn_server();
let port = spawn_server().await;

let body = RequestBody {
messages: Vec::new(),
Expand Down
2 changes: 1 addition & 1 deletion dwn-server/tests/records.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use reqwest::StatusCode;

#[tokio::test]
async fn records_write() {
let port = spawn_server();
let port = spawn_server().await;

let body = RequestBody {
messages: vec![RecordsWrite::default().into()],
Expand Down
12 changes: 9 additions & 3 deletions dwn-server/tests/utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
//! Utility functions used in tests.
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use dwn::{request::RequestBody, response::ResponseBody};
use reqwest::{Response, StatusCode};
use tokio::time::sleep;

/// Starts a DWN server on a random open port and returns the port.
pub fn spawn_server() -> u16 {
pub async fn spawn_server() -> u16 {
let port = port_check::free_local_port().expect("Failed to find free port");
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port);

tokio::spawn(async move {
dwn_server::start(dwn_server::StartOptions { port }).await;
});

// Wait for server to start
std::thread::sleep(std::time::Duration::from_secs(2));
// Poll the port until it's open.
while !port_check::is_port_reachable(addr) {
sleep(std::time::Duration::from_millis(100)).await;
}

port
}
Expand Down

0 comments on commit b59951e

Please sign in to comment.