Skip to content

Commit d72d0b2

Browse files
committed
Auto merge of #13860 - ehuss:max_download_size-test-time-fix, r=epage
Fix global_cache_tracker::max_download_size test flakiness This (hopefully) fixes an issue where the `global_cache_tracker::max_download_size` test was sporadically failing on CI. My theory is that the `populate_cache` function was inconsistently saving entries with either the same timestamp or timestamps that differed by 1 second. The SQL query in `get_registry_items_to_clean_size_both` sorts the results based on `(timestamp,name)`. Thus if the timestamps were the same, it was sorting on name. If they differed, then the timestamp would dominate. The solution is to force the tests to use the same basis for the starting time so that a function call like `days_ago(1)` returns consistent results. I don't have a particularly good way to reproduce the issue. Adding a sleep into `populate_cache` causes 100% errors. Running on a slowed down system, or perhaps GitHub Actions might also reproduce, but I did not try.
2 parents 05364cb + b44edc5 commit d72d0b2

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

tests/testsuite/global_cache_tracker.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use std::fmt::Write;
2222
use std::path::Path;
2323
use std::path::PathBuf;
2424
use std::process::Stdio;
25+
use std::sync::OnceLock;
2526
use std::time::{Duration, SystemTime};
2627

2728
/// Helper to create a simple `foo` project which depends on a registry
@@ -72,7 +73,18 @@ fn get_git_checkout_names(db_name: &str) -> Vec<String> {
7273
}
7374

7475
fn days_ago(n: u64) -> SystemTime {
75-
SystemTime::now() - Duration::from_secs(60 * 60 * 24 * n)
76+
now() - Duration::from_secs(60 * 60 * 24 * n)
77+
}
78+
79+
fn now() -> SystemTime {
80+
// This captures the time once to avoid potential time boundaries or
81+
// inconsistencies affecting a test. For example, on a fast system
82+
// `days_ago(1)` called twice in a row will return the same answer.
83+
// However, on a slower system, or if the clock happens to flip over from
84+
// one second to the next, then it would return different answers. This
85+
// ensures that it always returns the same answer.
86+
static START: OnceLock<SystemTime> = OnceLock::new();
87+
*START.get_or_init(|| SystemTime::now())
7688
}
7789

7890
/// Helper for simulating running cargo in the past. Use with the

0 commit comments

Comments
 (0)