From fde52183b70ebab2148e92fd0b0be96f35bf04ef Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Sat, 27 May 2023 11:22:27 +0100 Subject: [PATCH] test: set retry sleep to 1ms for all tests --- crates/cargo-test-support/src/lib.rs | 2 ++ src/cargo/util/network/retry.rs | 28 ++++++++++++++++++---------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/crates/cargo-test-support/src/lib.rs b/crates/cargo-test-support/src/lib.rs index d27aab44fd11..0997d7e8023d 100644 --- a/crates/cargo-test-support/src/lib.rs +++ b/crates/cargo-test-support/src/lib.rs @@ -1259,6 +1259,8 @@ pub trait TestEnv: Sized { .env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "stable") // Keeps cargo within its sandbox. .env("__CARGO_TEST_DISABLE_GLOBAL_KNOWN_HOST", "1") + // Set retry sleep to 1 millisecond. + .env("__CARGO_TEST_FIXED_RETRY_SLEEP_MS", "1") // Incremental generates a huge amount of data per test, which we // don't particularly need. Tests that specifically need to check // the incremental behavior should turn this back on. diff --git a/src/cargo/util/network/retry.rs b/src/cargo/util/network/retry.rs index 42c38ab9f80d..6a4f29c15ed4 100644 --- a/src/cargo/util/network/retry.rs +++ b/src/cargo/util/network/retry.rs @@ -56,21 +56,29 @@ impl<'a> Retry<'a> { return RetryResult::Err(e); } self.retries += 1; - let sleep = if self.retries == 1 { - let mut rng = rand::thread_rng(); - INITIAL_RETRY_SLEEP_BASE_MS + rng.gen_range(0..INITIAL_RETRY_JITTER_MS) - } else { - min( - ((self.retries - 1) * 3) * 1000 + INITIAL_RETRY_SLEEP_BASE_MS, - MAX_RETRY_SLEEP_MS, - ) - }; - RetryResult::Retry(sleep) + RetryResult::Retry(self.next_sleep_ms()) } Err(e) => RetryResult::Err(e), Ok(r) => RetryResult::Success(r), } } + + /// Gets the next sleep duration in milliseconds. + fn next_sleep_ms(&self) -> u64 { + if let Ok(sleep) = self.config.get_env("__CARGO_TEST_FIXED_RETRY_SLEEP_MS") { + return sleep.parse().expect("a u64") + } + + if self.retries == 1 { + let mut rng = rand::thread_rng(); + INITIAL_RETRY_SLEEP_BASE_MS + rng.gen_range(0..INITIAL_RETRY_JITTER_MS) + } else { + min( + ((self.retries - 1) * 3) * 1000 + INITIAL_RETRY_SLEEP_BASE_MS, + MAX_RETRY_SLEEP_MS, + ) + } + } } fn maybe_spurious(err: &Error) -> bool {