use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
fn claw() -> Command {
Command::new(env!("CARGO_BIN_EXE_claw"))
}
fn run_claw(args: &[&str]) -> (String, String, i32) {
let out = claw()
.args(args)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.expect("failed to spawn claw");
(
String::from_utf8(out.stdout).unwrap(),
String::from_utf8(out.stderr).unwrap(),
out.status.code().unwrap(),
)
}
#[test]
fn test_045_pipe_line_buffering() {
let mut child = claw()
.args([
"-w",
"localhost",
"--worker=exec",
"for i in A B C; do echo $i; sleep 0.5; done",
])
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn()
.expect("failed to spawn claw");
let stdout = child.stdout.take().unwrap();
let reader = BufReader::new(stdout);
let t0 = Instant::now();
let mut timestamps = Vec::new();
let mut lines = Vec::new();
for line in reader.lines() {
lines.push(line.unwrap());
timestamps.push(t0.elapsed());
}
let status = child.wait().unwrap();
assert!(status.success());
assert_eq!(
timestamps.len(),
3,
"expected 3 lines, got {}: {lines:?}",
timestamps.len()
);
let spread = timestamps[2] - timestamps[0];
assert!(
spread > Duration::from_millis(500),
"lines arrived together (spread={spread:.3?}); claw stdout looks block-buffered"
);
}
#[test]
fn test_046_axis() {
let base = ["-R", "exec", "-w", "foo[1-2]-[1-2]", "-b", "echo test"];
let check = |args: &[&str], expected: &str| {
let (stdout, stderr, rc) = run_claw(args);
assert_eq!(rc, 0, "claw failed for args {args:?}: {stderr}");
assert_eq!(stdout, expected, "stdout mismatch for args {args:?}");
};
check(
&base,
"---------------\nfoo[1-2]-[1-2] (4)\n---------------\ntest\n",
);
check(
&[&["--axis=1"], &base[..]].concat(),
"---------------\nfoo[1-2]-1,foo[1-2]-2 (4)\n---------------\ntest\n",
);
check(
&[&["--axis=2"], &base[..]].concat(),
"---------------\nfoo1-[1-2],foo2-[1-2] (4)\n---------------\ntest\n",
);
check(
&[&["--axis=-1"], &base[..]].concat(),
"---------------\nfoo1-[1-2],foo2-[1-2] (4)\n---------------\ntest\n",
);
check(
&[
"--axis=1",
"-R",
"exec",
"-w",
"node[1-4]",
"-b",
"echo test",
],
"---------------\nnode[1-4] (4)\n---------------\ntest\n",
);
}