fix(tests): stop parallel tests colliding (release run 3309) #49

Merged
whitlocktech merged 1 commits from fix/live-shard-test-race into main 2026-11-27 22:13:59 -06:00
2 changed files with 44 additions and 4 deletions
Showing only changes of commit 5322d0dd5c - Show all commits

View File

@@ -383,6 +383,16 @@ mod tests {
use super::*; use super::*;
use crate::util::TempDir; use crate::util::TempDir;
/// Held by every test that scans the real process table. The live-launch test's child names
/// `ServUO.exe`; caught mid-exit by a parallel scan (cmdline read, cwd already gone) it is an
/// `Unknown` match, and the empty-tree test would report it as a shard.
static PROCESS_TABLE: std::sync::Mutex<()> = std::sync::Mutex::new(());
fn process_table() -> std::sync::MutexGuard<'static, ()> {
// A failed test poisons the lock; the next one still needs it.
PROCESS_TABLE.lock().unwrap_or_else(|e| e.into_inner())
}
fn fake_root(dir: &Path) { fn fake_root(dir: &Path) {
fs::create_dir_all(dir.join("Scripts")).unwrap(); fs::create_dir_all(dir.join("Scripts")).unwrap();
fs::create_dir_all(dir.join("Config")).unwrap(); fs::create_dir_all(dir.join("Config")).unwrap();
@@ -607,7 +617,9 @@ mod tests {
/// installer#47 end to end on a real `/proc`: a process started *inside* the root naming a /// installer#47 end to end on a real `/proc`: a process started *inside* the root naming a
/// relative `ServUO.exe` — the shape of `cd root && mono ServUO.exe` — is found, under its own /// relative `ServUO.exe` — the shape of `cd root && mono ServUO.exe` — is found, under its own
/// pid (a thread id would not equal `child.id()`, installer#45 item 5). `sh -c '…' ServUO.exe` /// pid (a thread id would not equal `child.id()`, installer#45 item 5). `sh -c '…' ServUO.exe`
/// puts `ServUO.exe` in argv as `$0`; the trailing `:` stops the shell exec'ing into `sleep`. /// puts `ServUO.exe` in argv as `$0`. The shell waits in the `read` builtin on a stdin pipe the
/// test holds open, so it never forks: a forked child is a copy of `sh` — same argv, same cwd —
/// until it execs, a genuine second match the scan could report first (release run 3309).
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
#[test] #[test]
fn a_live_relative_launch_from_the_root_is_found_by_its_pid() { fn a_live_relative_launch_from_the_root_is_found_by_its_pid() {
@@ -616,9 +628,11 @@ mod tests {
fake_root(&root); fake_root(&root);
let root = fs::canonicalize(&root).unwrap(); let root = fs::canonicalize(&root).unwrap();
let _table = process_table();
let mut child = std::process::Command::new("sh") let mut child = std::process::Command::new("sh")
.args(["-c", "sleep 30; :", "ServUO.exe"]) .args(["-c", "read x", "ServUO.exe"])
.current_dir(&root) .current_dir(&root)
.stdin(std::process::Stdio::piped())
.spawn() .spawn()
.unwrap(); .unwrap();
// The process is listed as soon as it exists, but give /proc a moment to fill cmdline. // The process is listed as soon as it exists, but give /proc a moment to fill cmdline.
@@ -651,6 +665,7 @@ mod tests {
let tmp = TempDir::new("rg-test-running").unwrap(); let tmp = TempDir::new("rg-test-running").unwrap();
let root = tmp.path().join("srv"); let root = tmp.path().join("srv");
fake_root(&root); fake_root(&root);
let _table = process_table();
assert!(find_running(&root).is_none()); assert!(find_running(&root).is_none());
} }
} }

View File

@@ -119,13 +119,22 @@ pub struct TempDir {
} }
impl TempDir { impl TempDir {
/// The name is pid + a per-process counter + the clock. The counter is what makes it unique:
/// two threads can read the same nanosecond (parallel tests did, and deleted each other's
/// files). `create_dir`, not `create_dir_all`, so a name that somehow exists is an error rather
/// than a directory silently shared with its owner.
pub fn new(prefix: &str) -> Result<Self> { pub fn new(prefix: &str) -> Result<Self> {
use std::sync::atomic::{AtomicU64, Ordering};
static NEXT: AtomicU64 = AtomicU64::new(0);
let n = NEXT.fetch_add(1, Ordering::Relaxed);
let nanos = SystemTime::now() let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH) .duration_since(UNIX_EPOCH)
.map(|d| d.as_nanos()) .map(|d| d.as_nanos())
.unwrap_or(0); .unwrap_or(0);
let path = std::env::temp_dir().join(format!("{prefix}-{}-{nanos}", std::process::id())); let path =
fs::create_dir_all(&path) std::env::temp_dir().join(format!("{prefix}-{}-{n}-{nanos}", std::process::id()));
fs::create_dir(&path)
.with_context(|| format!("cannot create scratch directory {}", path.display()))?; .with_context(|| format!("cannot create scratch directory {}", path.display()))?;
Ok(Self { path }) Ok(Self { path })
} }
@@ -307,6 +316,22 @@ mod tests {
assert!(!path.exists()); assert!(!path.exists());
} }
#[test]
fn temp_dirs_made_at_once_on_many_threads_never_share_a_path() {
// Parallel tests with one prefix used to land in one directory when they read the same
// nanosecond, and the first to finish deleted the other's files.
let dirs: Vec<TempDir> = std::thread::scope(|s| {
let handles: Vec<_> = (0..16)
.map(|_| s.spawn(|| TempDir::new("rg-test-unique").unwrap()))
.collect();
handles.into_iter().map(|h| h.join().unwrap()).collect()
});
let mut paths: Vec<_> = dirs.iter().map(|d| d.path().to_path_buf()).collect();
paths.sort();
paths.dedup();
assert_eq!(paths.len(), dirs.len());
}
#[test] #[test]
fn a_failed_command_is_reported_with_what_it_printed() { fn a_failed_command_is_reported_with_what_it_printed() {
// Both halves matter: the command to retype, and the reason it failed. stderr wins over // Both halves matter: the command to retype, and the reason it failed. stderr wins over