huskies: merge 1091 refactor Migrate the merge-gate's stale-cargo kill path to process_kill
This commit is contained in:
@@ -91,8 +91,10 @@ pub fn sigkill_pids_and_verify(pids: &[u32]) -> Result<usize, Vec<u32>> {
|
||||
/// "every process running in `<worktree>/`" or "every cargo invocation
|
||||
/// against this `Cargo.toml`".
|
||||
pub fn pids_matching(pattern: &str) -> Vec<u32> {
|
||||
// `--` prevents pgrep (getopt_long-based) from interpreting patterns that
|
||||
// start with `--` (e.g. `--manifest-path ...`) as unrecognised long options.
|
||||
let Ok(output) = std::process::Command::new("pgrep")
|
||||
.args(["-f", pattern])
|
||||
.args(["-f", "--", pattern])
|
||||
.output()
|
||||
else {
|
||||
return Vec::new();
|
||||
@@ -267,6 +269,50 @@ mod tests {
|
||||
let _ = reaper.join();
|
||||
}
|
||||
|
||||
/// Verify that the merge-gate's stale-cargo kill pattern actually kills
|
||||
/// processes whose command line contains `--manifest-path .../Cargo.toml`.
|
||||
#[test]
|
||||
fn stale_cargo_pattern_kills_matching_process() {
|
||||
use std::os::unix::process::CommandExt;
|
||||
let unique = format!("{}-{}", std::process::id(), rand_u64());
|
||||
let fake_manifest = format!("/tmp/huskies-test-{unique}/Cargo.toml");
|
||||
let argv0 = format!("cargo test --manifest-path {fake_manifest}");
|
||||
|
||||
let child: Child = Command::new("sleep")
|
||||
.arg0(&argv0)
|
||||
.arg("60")
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.stdin(Stdio::null())
|
||||
.spawn()
|
||||
.expect("failed to spawn sleeper");
|
||||
let pid = child.id();
|
||||
let reaper = std::thread::spawn(move || {
|
||||
let mut c = child;
|
||||
let _ = c.wait();
|
||||
});
|
||||
|
||||
std::thread::sleep(std::time::Duration::from_millis(100));
|
||||
|
||||
let pattern = format!("--manifest-path {fake_manifest}");
|
||||
let pids = pids_matching(&pattern);
|
||||
assert!(
|
||||
pids.contains(&pid),
|
||||
"pids_matching must find the stale-cargo-like process (pid {pid}); got {pids:?}"
|
||||
);
|
||||
|
||||
let result = sigkill_pids_and_verify(&pids);
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"sigkill_pids_and_verify must succeed for stale cargo pids: {result:?}"
|
||||
);
|
||||
assert!(
|
||||
!pid_is_alive(pid),
|
||||
"stale cargo-like process (pid {pid}) must be dead after kill"
|
||||
);
|
||||
let _ = reaper.join();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pids_matching_returns_empty_when_no_match() {
|
||||
let pattern = format!("nonexistent-pattern-{}-{}", std::process::id(), rand_u64());
|
||||
|
||||
Reference in New Issue
Block a user