|
| 1 | +use std::io::{BufRead, BufReader}; |
| 2 | +use std::os::unix::io::AsFd; |
| 3 | +use std::process::{Command, Stdio}; |
| 4 | +use std::time::{Duration, Instant}; |
| 5 | + |
| 6 | +use candumpr::can::{self, LinuxCanFrame}; |
| 7 | +use vcan_fixture::VcanHarness; |
| 8 | + |
| 9 | +#[ctor::ctor] |
| 10 | +fn setup() { |
| 11 | + tracing_subscriber::fmt().with_test_writer().init(); |
| 12 | + vcan_fixture::enter_namespace(); |
| 13 | +} |
| 14 | + |
| 15 | +/// When the stdout consumer goes away (`candumpr can0 | head`), candumpr must notice the broken |
| 16 | +/// pipe on its next write and exit cleanly on its own, without being signalled. |
| 17 | +#[test] |
| 18 | +#[cfg_attr(feature = "ci", ignore = "requires vcan")] |
| 19 | +fn exits_cleanly_when_stdout_closes() { |
| 20 | + let vcans = VcanHarness::new(1).unwrap(); |
| 21 | + let iface = &vcans.names()[0]; |
| 22 | + |
| 23 | + let mut child = Command::new(env!("CARGO_BIN_EXE_candumpr")) |
| 24 | + .arg(iface) |
| 25 | + .arg("--log-level=INFO") // TRACE level fills up the stderr buf and prevents EPIPE error |
| 26 | + .stdout(Stdio::piped()) |
| 27 | + .stderr(Stdio::piped()) |
| 28 | + .spawn() |
| 29 | + .unwrap(); |
| 30 | + |
| 31 | + // Give the process time to set up io_uring and start receiving. |
| 32 | + std::thread::sleep(Duration::from_millis(200)); |
| 33 | + |
| 34 | + let tx = can::open_can_raw_blocking(iface).unwrap(); |
| 35 | + let frame = LinuxCanFrame::new(0x18FECA00 | libc::CAN_EFF_FLAG, &[0xAA, 0xBB]); |
| 36 | + |
| 37 | + // Prove the pipe is live: send one frame and read the line it produces. |
| 38 | + let mut stdout = BufReader::new(child.stdout.take().unwrap()); |
| 39 | + can::send_frame(tx.as_fd(), &frame).unwrap(); |
| 40 | + let mut line = String::new(); |
| 41 | + stdout.read_line(&mut line).unwrap(); |
| 42 | + assert!( |
| 43 | + line.ends_with('\n'), |
| 44 | + "expected a full output line: {line:?}" |
| 45 | + ); |
| 46 | + |
| 47 | + // Close the read end, like `head` exiting. The next batch write hits EPIPE. |
| 48 | + drop(stdout); |
| 49 | + can::send_frame(tx.as_fd(), &frame).unwrap(); |
| 50 | + |
| 51 | + let deadline = Instant::now() + Duration::from_secs(5); |
| 52 | + let status = loop { |
| 53 | + if let Some(status) = child.try_wait().unwrap() { |
| 54 | + break status; |
| 55 | + } |
| 56 | + if Instant::now() >= deadline { |
| 57 | + child.kill().unwrap(); |
| 58 | + let output = child.wait_with_output().unwrap(); |
| 59 | + panic!( |
| 60 | + "candumpr did not exit after stdout closed. stderr:\n{}", |
| 61 | + String::from_utf8_lossy(&output.stderr) |
| 62 | + ); |
| 63 | + } |
| 64 | + std::thread::sleep(Duration::from_millis(50)); |
| 65 | + }; |
| 66 | + |
| 67 | + let output = child.wait_with_output().unwrap(); |
| 68 | + eprint!("{}", String::from_utf8_lossy(&output.stderr)); |
| 69 | + assert!( |
| 70 | + status.success(), |
| 71 | + "expected clean exit after stdout closed, got {status}" |
| 72 | + ); |
| 73 | +} |
0 commit comments