Skip to content

Commit

Permalink
test: set retry sleep to 1ms for all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed May 27, 2023
1 parent 0425b11 commit 7b4e32d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
2 changes: 2 additions & 0 deletions crates/cargo-test-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 18 additions & 10 deletions src/cargo/util/network/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7b4e32d

Please sign in to comment.