Skip to content

Commit

Permalink
Made wait_for_server public and more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
pictographer committed Jul 15, 2022
1 parent 1a78896 commit 521a16f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
26 changes: 5 additions & 21 deletions examples/hello-world/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,40 +88,24 @@ async fn main() -> io::Result<()> {
#[cfg(test)]
mod test {
use super::*;
use async_std::task::{sleep, spawn};
use async_std::task::spawn;
use portpicker::pick_unused_port;
use std::time::Duration;
use surf::Url;
use tide_disco::{
api::ApiVersion,
app::{AppHealth, AppVersion},
healthcheck::HealthStatus,
wait_for_server, SERVER_STARTUP_RETRIES, SERVER_STARTUP_SLEEP_MS,
};
use tracing_test::traced_test;

const STARTUP_RETRIES: usize = 255;

async fn wait_for_server(port: u16) {
let sleep_ms = Duration::from_millis(100);
for _ in 0..STARTUP_RETRIES {
if surf::connect(format!("http://localhost:{}", port))
.send()
.await
.is_ok()
{
return;
}
sleep(sleep_ms).await;
}
}

#[async_std::test]
#[traced_test]
async fn test_get_set_greeting() {
let port = pick_unused_port().unwrap();
spawn(serve(port));
let url = Url::parse(&format!("http://localhost:{}/hello/", port)).unwrap();
wait_for_server(port).await;
wait_for_server(&url, SERVER_STARTUP_RETRIES, SERVER_STARTUP_SLEEP_MS).await;

let mut res = surf::get(url.join("greeting/tester").unwrap())
.send()
Expand Down Expand Up @@ -150,7 +134,7 @@ mod test {
let port = pick_unused_port().unwrap();
spawn(serve(port));
let url = Url::parse(&format!("http://localhost:{}/", port)).unwrap();
wait_for_server(port).await;
wait_for_server(&url, SERVER_STARTUP_RETRIES, SERVER_STARTUP_SLEEP_MS).await;

// Check the API version.
let mut res = surf::get(url.join("hello/version").unwrap())
Expand Down Expand Up @@ -189,7 +173,7 @@ mod test {
let port = pick_unused_port().unwrap();
spawn(serve(port));
let url = Url::parse(&format!("http://localhost:{}/", port)).unwrap();
wait_for_server(port).await;
wait_for_server(&url, SERVER_STARTUP_RETRIES, SERVER_STARTUP_SLEEP_MS).await;

// Check the API health.
let mut res = surf::get(url.join("hello/healthcheck").unwrap())
Expand Down
20 changes: 11 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,10 @@ pub use request::{RequestError, RequestParam, RequestParamType, RequestParamValu
pub use tide::http::{self, StatusCode};

/// Number of times to poll before failing
const STARTUP_RETRIES: u32 = 255;
pub const SERVER_STARTUP_RETRIES: u64 = 255;

/// Number of milliseconds to sleep between attempts
pub const SERVER_STARTUP_SLEEP_MS: u64 = 100;

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
Expand Down Expand Up @@ -922,17 +925,16 @@ pub fn app_api_path(org_name: &str, app_name: &str) -> PathBuf {
///
/// This is useful for tests for which it doesn't make sense to send requests until the server has
/// started.
pub async fn wait_for_server(base_url: &str) {
// Wait for the server to come up and start serving.
let pause_ms = Duration::from_millis(100);
for _ in 0..STARTUP_RETRIES {
if surf::connect(base_url).send().await.is_ok() {
pub async fn wait_for_server(url: &Url, retries: u64, sleep_ms: u64) {
let dur = Duration::from_millis(sleep_ms);
for _ in 0..retries {
if surf::connect(&url).send().await.is_ok() {
return;
}
sleep(pause_ms).await;
sleep(dur).await;
}
panic!(
"Address Book did not start in {:?} milliseconds",
pause_ms * STARTUP_RETRIES
"Server did not start in {:?} milliseconds",
sleep_ms * SERVER_STARTUP_RETRIES
);
}

0 comments on commit 521a16f

Please sign in to comment.