use assert_cmd::prelude::*;
use std::process::{Command, Stdio};
use std::{thread, time::Duration};
use tempfile::TempDir;
fn pend_bin() -> Command {
Command::cargo_bin("pend").expect("binary exists")
}
fn pend_with_tempdir() -> (TempDir, Command) {
let tmp = TempDir::new().expect("create tempdir");
let mut cmd = pend_bin();
cmd.env("PEND_DIR", tmp.path());
(tmp, cmd)
}
#[test]
fn ctrlc_does_not_kill_worker() {
let (tmp, mut pend_cmd) = pend_with_tempdir();
let job = "longrun";
pend_cmd
.args([
"do",
job,
"bash",
"-c",
"echo start && sleep 1 && echo done",
])
.assert()
.success();
let mut wait_child = pend_bin()
.env("PEND_DIR", tmp.path())
.arg("--no-color")
.args(["wait", job])
.stdout(Stdio::null())
.spawn()
.expect("spawn pend wait");
thread::sleep(Duration::from_millis(100));
#[cfg(unix)]
{
unsafe {
libc::kill(wait_child.id() as i32, libc::SIGINT);
}
}
#[cfg(not(unix))]
{
wait_child.kill().expect("terminate wait process");
}
let _ = wait_child.wait().expect("wait on child");
let exit_path = tmp.path().join(format!("{job}.exit"));
assert!(!exit_path.exists(), "job unexpectedly finished early");
pend_bin()
.env("PEND_DIR", tmp.path())
.arg("--no-color")
.args(["wait", job])
.assert()
.success();
}