Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corbett wait for server #49

Merged
merged 2 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
);
}
4 changes: 2 additions & 2 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub enum RequestError {
},

#[snafu(display("Unable to compose JSON"))]
JsonSnafu,
Json,
}

/// Parameters passed to a route handler.
Expand Down Expand Up @@ -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 {})
}
}

Expand Down