use std::time::{Duration, Instant};
use super::super::{Cancel, Capture, Outcome, RunOptions, StopReason, WholeCapture, run};
use crate::testing::{cmd, gone, pid_file, pid_in, remove, started_grandchild};
use crate::{Scope, Status};
const WHOLE: WholeCapture = Capture::whole(1 << 20);
fn text(bytes: &[u8]) -> String {
String::from_utf8_lossy(bytes).into_owned()
}
#[test]
fn a_child_gives_its_exit_code_and_its_standard_output() {
let finished = run(cmd("echo out& exit 3"), &RunOptions::new().stdout(WHOLE)).unwrap();
assert_eq!(finished.outcome(), Outcome::Ended(Status::Code(3)));
assert_eq!(finished.status(), Some(Status::Code(3)));
assert_eq!(finished.stdout().bytes(), b"out\r\n");
}
#[test]
fn a_cancelled_child_whose_job_was_ended_gives_exit_code_1() {
let cancel = Cancel::new();
let canceller = std::thread::spawn({
let cancel = cancel.clone();
move || {
std::thread::sleep(Duration::from_millis(500));
cancel.cancel();
}
});
let finished = run(
cmd("ping -n 120 127.0.0.1 >NUL"),
&RunOptions::new().stdout(WHOLE).cancel(&cancel),
)
.unwrap();
canceller.join().unwrap();
assert_eq!(finished.outcome(), Outcome::Stopped(StopReason::Cancelled));
assert_eq!(finished.status(), Some(Status::Code(1)));
}
#[test]
fn ending_the_job_when_the_child_exits_ends_a_grandchild_that_holds_standard_output() {
let file = pid_file("job-ends");
let started = Instant::now();
let finished = run(
cmd(&started_grandchild(&file, 120)),
&RunOptions::new().stdout(WHOLE),
)
.unwrap();
let took = started.elapsed();
remove(&file);
let out = text(finished.stdout().bytes());
assert_eq!(finished.outcome(), Outcome::Ended(Status::Code(0)));
assert!(out.contains("started"), "{out:?}");
assert!(!out.contains("woke"), "{out:?}");
let grandchild = pid_in(finished.stdout().bytes());
assert!(took < Duration::from_secs(60), "the run took {took:?}");
assert!(
gone(grandchild),
"the grandchild {grandchild} outlived the run"
);
}
#[test]
fn in_the_child_scope_the_run_waits_for_a_grandchild_that_holds_standard_output() {
let file = pid_file("child-scope");
let started = Instant::now();
let finished = run(
cmd(&started_grandchild(&file, 3)),
&RunOptions::new().stdout(WHOLE).scope(Scope::Child),
)
.unwrap();
let took = started.elapsed();
remove(&file);
let out = text(finished.stdout().bytes());
assert_eq!(finished.outcome(), Outcome::Ended(Status::Code(0)));
assert!(out.contains("started"), "{out:?}");
assert!(
out.contains("woke"),
"the grandchild did not outlive the child: {out:?}"
);
assert!(took >= Duration::from_secs(3), "the run took {took:?}");
let grandchild = pid_in(finished.stdout().bytes());
assert!(
gone(grandchild),
"the grandchild {grandchild} is still running"
);
}
#[test]
fn a_deadline_ends_the_job_with_the_grandchild_in_it() {
let file = pid_file("deadline");
let script = format!(
"{} & ping -n 120 127.0.0.1 >NUL",
started_grandchild(&file, 120)
);
let started = Instant::now();
let finished = run(
cmd(&script),
&RunOptions::new()
.stdout(WHOLE)
.deadline(Instant::now() + Duration::from_secs(20)),
)
.unwrap();
let took = started.elapsed();
remove(&file);
let out = text(finished.stdout().bytes());
assert_eq!(finished.outcome(), Outcome::TimedOut);
assert!(out.contains("started"), "{out:?}");
let grandchild = pid_in(finished.stdout().bytes());
assert!(took < Duration::from_secs(60), "the run took {took:?}");
assert!(
gone(grandchild),
"the grandchild {grandchild} outlived the run"
);
}