From 1a78896bb3f359c47062168a7a12f83b707c8529 Mon Sep 17 00:00:00 2001 From: "John D. Corbett" Date: Thu, 14 Jul 2022 21:08:45 -0700 Subject: [PATCH 1/2] Removed redundant Snafu from error type. --- src/request.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/request.rs b/src/request.rs index 24e6d6c8..ba625c7f 100644 --- a/src/request.rs +++ b/src/request.rs @@ -30,7 +30,7 @@ pub enum RequestError { }, #[snafu(display("Unable to compose JSON"))] - JsonSnafu, + Json, } /// Parameters passed to a route handler. @@ -240,7 +240,7 @@ impl RequestParams { where T: serde::de::DeserializeOwned, { - serde_json::from_slice(&self.post_data.clone()).map_err(|_| RequestError::JsonSnafu {}) + serde_json::from_slice(&self.post_data.clone()).map_err(|_| RequestError::Json {}) } } From 521a16ff852c5928dad9cd4ceecf2a1dfd8e3c61 Mon Sep 17 00:00:00 2001 From: "John D. Corbett" Date: Fri, 15 Jul 2022 14:50:21 -0700 Subject: [PATCH 2/2] Made wait_for_server public and more flexible --- examples/hello-world/main.rs | 26 +++++--------------------- src/lib.rs | 20 +++++++++++--------- 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/examples/hello-world/main.rs b/examples/hello-world/main.rs index d97ca34b..50c79242 100644 --- a/examples/hello-world/main.rs +++ b/examples/hello-world/main.rs @@ -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() @@ -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()) @@ -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()) diff --git a/src/lib.rs b/src/lib.rs index 49cb7e75..db0cc224 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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)] @@ -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 ); }