diff --git a/buildpacks/ruby/src/rake_task_detect.rs b/buildpacks/ruby/src/rake_task_detect.rs index 3673e820..59c15dc1 100644 --- a/buildpacks/ruby/src/rake_task_detect.rs +++ b/buildpacks/ruby/src/rake_task_detect.rs @@ -1,7 +1,6 @@ +use commons::fun_run::{CmdError, CommandWithName}; #[allow(clippy::wildcard_imports)] use commons::output::{fmt, section_log::*}; - -use commons::fun_run::{CmdError, CommandWithName}; use core::str::FromStr; use std::{ffi::OsStr, process::Command}; diff --git a/buildpacks/ruby/tests/integration_test.rs b/buildpacks/ruby/tests/integration_test.rs index ea8ef0c3..fdeba740 100644 --- a/buildpacks/ruby/tests/integration_test.rs +++ b/buildpacks/ruby/tests/integration_test.rs @@ -167,32 +167,32 @@ fn request_container( req.call().map_err(Box::new) } +fn time_bounded_retry( + max_time: Duration, + sleep_for: Duration, + f: impl Fn() -> Result, +) -> Result { + let start = Instant::now(); + + let mut result = f(); + while start.elapsed() < max_time { + if result.is_ok() { + return result; + } + + thread::sleep(sleep_for); + result = f(); + } + result +} + fn call_root_until_boot( container: &ContainerContext, port: u16, ) -> Result> { - let mut count = 0; - let max_time = 10.0_f64; // Seconds - let sleep_for = 0.1_f64; - - #[allow(clippy::cast_possible_truncation)] - let max_count = (max_time / sleep_for).floor() as i64; - let mut response = request_container(container, port, ""); - while count < max_count { - count += 1; - match response { - Err(ref box_e) => match box_e.as_ref() { - ureq::Error::Transport(error) => { - println!("Waiting for connection {error}, retrying in {sleep_for}"); - response = request_container(container, port, ""); - } - ureq::Error::Status(..) => break, - }, - _ => break, - } - - thread::sleep(frac_seconds(sleep_for)); - } + let response = time_bounded_retry(Duration::from_secs(10), frac_seconds(0.1_f64), || { + request_container(container, port, "") + }); println!( "{}\n{}",