Skip to content

Commit fde5218

Browse files
committed
test: set retry sleep to 1ms for all tests
1 parent 0425b11 commit fde5218

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

crates/cargo-test-support/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,8 @@ pub trait TestEnv: Sized {
12591259
.env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "stable")
12601260
// Keeps cargo within its sandbox.
12611261
.env("__CARGO_TEST_DISABLE_GLOBAL_KNOWN_HOST", "1")
1262+
// Set retry sleep to 1 millisecond.
1263+
.env("__CARGO_TEST_FIXED_RETRY_SLEEP_MS", "1")
12621264
// Incremental generates a huge amount of data per test, which we
12631265
// don't particularly need. Tests that specifically need to check
12641266
// the incremental behavior should turn this back on.

src/cargo/util/network/retry.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,29 @@ impl<'a> Retry<'a> {
5656
return RetryResult::Err(e);
5757
}
5858
self.retries += 1;
59-
let sleep = if self.retries == 1 {
60-
let mut rng = rand::thread_rng();
61-
INITIAL_RETRY_SLEEP_BASE_MS + rng.gen_range(0..INITIAL_RETRY_JITTER_MS)
62-
} else {
63-
min(
64-
((self.retries - 1) * 3) * 1000 + INITIAL_RETRY_SLEEP_BASE_MS,
65-
MAX_RETRY_SLEEP_MS,
66-
)
67-
};
68-
RetryResult::Retry(sleep)
59+
RetryResult::Retry(self.next_sleep_ms())
6960
}
7061
Err(e) => RetryResult::Err(e),
7162
Ok(r) => RetryResult::Success(r),
7263
}
7364
}
65+
66+
/// Gets the next sleep duration in milliseconds.
67+
fn next_sleep_ms(&self) -> u64 {
68+
if let Ok(sleep) = self.config.get_env("__CARGO_TEST_FIXED_RETRY_SLEEP_MS") {
69+
return sleep.parse().expect("a u64")
70+
}
71+
72+
if self.retries == 1 {
73+
let mut rng = rand::thread_rng();
74+
INITIAL_RETRY_SLEEP_BASE_MS + rng.gen_range(0..INITIAL_RETRY_JITTER_MS)
75+
} else {
76+
min(
77+
((self.retries - 1) * 3) * 1000 + INITIAL_RETRY_SLEEP_BASE_MS,
78+
MAX_RETRY_SLEEP_MS,
79+
)
80+
}
81+
}
7482
}
7583

7684
fn maybe_spurious(err: &Error) -> bool {

0 commit comments

Comments
 (0)