fix(tests): stop parallel tests colliding (release run 3309)
All checks were successful
PR Checks / rust-gates (pull_request) Successful in 1m49s

Release run 3309 went red on a_live_relative_launch_from_the_root_is_found_by_its_pid
(left 5085, right 5028). Three races between tests running in parallel, none in the
installer's behaviour:

- The live-launch test's `sh -c 'sleep 30; :'` forks to run sleep. Until it execs, the
  fork is a copy of sh with the same argv (naming ServUO.exe) and cwd - a genuine second
  match the scan could report first. The shell now waits in the `read` builtin on a stdin
  pipe the test holds, so it never forks.
- nothing_is_running_out_of_an_empty_tree could catch that child mid-exit (cmdline read,
  cwd gone) as an Unknown match. Both process-table tests now take a shared lock.
- TempDir named directories pid + nanoseconds and used create_dir_all, so two parallel
  tests with one prefix could share a directory and delete each other's files (the overlay
  tests' intermittent failures). A per-process counter now makes the name unique, and
  create_dir fails rather than sharing a directory that already exists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014HJEmd5sBSpNXicS4gmT1t
This commit is contained in:
2026-11-27 22:11:24 -06:00
parent 6a9dfac1ac
commit 5322d0dd5c
2 changed files with 44 additions and 4 deletions

View File

@@ -383,6 +383,16 @@ mod tests {
use super::*;
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) {
fs::create_dir_all(dir.join("Scripts")).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
/// 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`
/// 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")]
#[test]
fn a_live_relative_launch_from_the_root_is_found_by_its_pid() {
@@ -616,9 +628,11 @@ mod tests {
fake_root(&root);
let root = fs::canonicalize(&root).unwrap();
let _table = process_table();
let mut child = std::process::Command::new("sh")
.args(["-c", "sleep 30; :", "ServUO.exe"])
.args(["-c", "read x", "ServUO.exe"])
.current_dir(&root)
.stdin(std::process::Stdio::piped())
.spawn()
.unwrap();
// 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 root = tmp.path().join("srv");
fake_root(&root);
let _table = process_table();
assert!(find_running(&root).is_none());
}
}

View File

@@ -119,13 +119,22 @@ pub struct 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> {
use std::sync::atomic::{AtomicU64, Ordering};
static NEXT: AtomicU64 = AtomicU64::new(0);
let n = NEXT.fetch_add(1, Ordering::Relaxed);
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0);
let path = std::env::temp_dir().join(format!("{prefix}-{}-{nanos}", std::process::id()));
fs::create_dir_all(&path)
let 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()))?;
Ok(Self { path })
}
@@ -307,6 +316,22 @@ mod tests {
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]
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