use super::{Children, Config};
use serde_json::json;
use std::fs;
type TestResult = Result<(), Box<dyn std::error::Error>>;
#[test]
fn r2_ready_child_exit_starts_the_same_child_again() -> TestResult {
let directory = tempfile::Builder::new()
.prefix("door-wait-")
.tempdir_in("/tmp")?;
let records = directory.path().join("starts");
let release = directory.path().join("release");
assert!(
std::process::Command::new("/usr/bin/mkfifo")
.arg(&release)
.status()?
.success()
);
let config: Config = serde_json::from_value(json!({
"name":"once", "program":"/usr/bin/python3", "args":["-u", "-c",
"import sys, os\np=sys.argv[1]\nexists=os.path.exists(p)\nwith open(p, 'a') as f: f.write('started\\n')\nprint('fixture ready once', file=sys.stderr, flush=True)\nif not exists:\n with open(sys.argv[2]) as gate: gate.read()\n sys.exit(7)\nsys.stdin.read()", records, release],
"ready":"fixture ready", "log":directory.path().join("child.log"), "give_up_after":3
}))?;
let dependent_records = directory.path().join("dependent-starts");
let dependent: Config = serde_json::from_value(json!({
"name":"dependent", "program":"/usr/bin/python3", "args":["-u", "-c",
"import sys\nwith open(sys.argv[1], 'a') as f: f.write('started\\n')\nprint('fixture ready dependent', file=sys.stderr, flush=True)\nsys.stdin.read()", dependent_records],
"after":"once", "ready":"fixture ready", "log":directory.path().join("dependent.log"), "give_up_after":3
}))?;
let children = Children::start(&[config, dependent])?;
fs::write(&release, b"exit")?;
assert_eq!(children.wait_restarted(0)?, 2);
assert_eq!(fs::read_to_string(&records)?.lines().count(), 2);
assert_eq!(
fs::read_to_string(directory.path().join("child.log"))?
.lines()
.filter(|line| line.starts_with("fixture ready once"))
.count(),
2
);
assert_eq!(fs::read_to_string(&dependent_records)?.lines().count(), 1);
drop(children);
Ok(())
}