#![cfg(unix)]
use std::collections::BTreeSet;
use std::fs;
use std::os::fd::{AsRawFd, OwnedFd, RawFd};
use std::time::Duration;
use tokio::io::DuplexStream;
use tokio::sync::{mpsc, oneshot};
use tokio::time::timeout;
use shep_core::logstamp::LOG_STAMP_BYTES;
use crate::runner::LogCtl;
use super::super::log_file::{LogSink, PipeFds};
use super::super::pump::spawn_log_pump;
use super::super::{CHANNEL_CAPACITY, FINAL_DRAIN, READ_BUFFER};
use super::*;
#[tokio::test]
async fn a_pump_reports_the_descriptors_it_holds() {
let pump = PumpHarness::start_over_pipes();
let fds = pump.report_fds().await;
assert_eq!(fds.out_pipe, pump.pipes.out, "stdout's read end");
assert_eq!(fds.err_pipe, pump.pipes.err, "stderr's read end");
assert!(
fds.out_log.is_some() && fds.err_log.is_some(),
"a pump that opened both log files must name both handles: {fds:?}"
);
let named: Vec<RawFd> = fds.all().into_iter().flatten().collect();
assert_eq!(named.len(), 4, "a running sheep has all four: {fds:?}");
for fd in &named {
assert!(
is_open(*fd),
"the blob would name a closed descriptor: {fd}"
);
}
let distinct: BTreeSet<RawFd> = named.iter().copied().collect();
assert_eq!(distinct.len(), 4, "four descriptors, four numbers: {fds:?}");
}
#[tokio::test]
async fn both_pipes_filled_to_capacity_drain_inside_the_budget() {
let pump = PumpHarness::start_over_pipes();
let line = format!("{}\n", "x".repeat(63));
let out_lines = fill_pipe(&pump.out_writer, &line).await;
let err_lines = fill_pipe(&pump.err_writer, &line).await;
assert!(
out_lines > 0 && err_lines > 0,
"neither pipe took a whole line: stdout={out_lines} stderr={err_lines}"
);
drop(pump.out_writer);
drop(pump.err_writer);
drop(pump.logs);
for (path, want, stream) in [
(&pump.out_path, out_lines, "stdout"),
(&pump.err_path, err_lines, "stderr"),
] {
let settled = timeout(PUMP_DEADLINE, async {
while fs::read_to_string(path).unwrap_or_default().lines().count() < want {
tokio::time::sleep(Duration::from_millis(2)).await;
}
})
.await;
assert!(
settled.is_ok(),
"a full {stream} pipe did not drain inside {FINAL_DRAIN:?}: {want} lines \
written, {} landed",
fs::read_to_string(path).unwrap_or_default().lines().count()
);
}
}
#[tokio::test]
async fn two_pumps_on_one_log_path_report_different_numbers() {
let dir = tempfile::tempdir().unwrap();
let out_path = dir.path().join("merged-out.log");
let err_path = dir.path().join("merged-err.log");
let mut writers = Vec::new();
let mut reports = Vec::new();
for _ in 0..2 {
let (out_writer, out_reader) = tokio::net::unix::pipe::pipe().unwrap();
let (err_writer, err_reader) = tokio::net::unix::pipe::pipe().unwrap();
let pipes = PipeFds {
out: Some(out_reader.as_raw_fd()),
err: Some(err_reader.as_raw_fd()),
stdin: None,
channel: None,
};
let (logs_tx, _logs) = mpsc::channel(CHANNEL_CAPACITY);
let (ctl, ctl_rx) = mpsc::channel(CHANNEL_CAPACITY);
spawn_log_pump(
Some(out_reader),
Some(err_reader),
LogSink::Path(out_path.clone()),
LogSink::Path(err_path.clone()),
logs_tx,
ctl_rx,
pipes,
);
let (done, ack) = oneshot::channel();
ctl.send(LogCtl::ReportFds { done }).await.unwrap();
let fds = timeout(PUMP_DEADLINE, ack)
.await
.expect("a descriptor report must be acknowledged")
.expect("the pump must answer rather than drop the acknowledgement");
writers.push((out_writer, err_writer, ctl, _logs));
reports.push(fds);
}
let named: Vec<RawFd> = reports
.iter()
.flat_map(|fds| fds.all().into_iter().flatten())
.collect();
assert_eq!(
named.len(),
8,
"two running instances, four descriptors each: {reports:?}"
);
let distinct: BTreeSet<RawFd> = named.iter().copied().collect();
assert_eq!(
distinct.len(),
8,
"a repeat here refuses the whole handover, not just this app: {reports:?}"
);
assert_ne!(
reports[0].out_log, reports[1].out_log,
"two instances sharing one log file must still hold two handles"
);
assert_ne!(reports[0].err_log, reports[1].err_log);
drop(writers);
}
#[tokio::test]
async fn a_report_after_a_stream_ends_names_no_descriptor_for_it() {
let mut pump = PumpHarness::start_over_pipes();
pump.feed(false, "before-the-eof").await;
drop(pump.out_writer);
let_the_pump_settle().await;
let (done, ack) = oneshot::channel();
pump.ctl
.send(LogCtl::ReportFds { done })
.await
.expect("a pump with one live stream still reads its control channel");
let fds = timeout(PUMP_DEADLINE, ack)
.await
.expect("a descriptor report must be acknowledged")
.expect("the pump must answer rather than drop the acknowledgement");
assert_eq!(
fds.out_pipe, None,
"stdout is at EOF and its descriptor is gone: {fds:?}"
);
assert_eq!(
fds.err_pipe, pump.pipes.err,
"stderr is still live and keeps its own number: {fds:?}"
);
assert!(
fds.out_log.is_some() && fds.err_log.is_some(),
"both log handles are held whichever stream ended: {fds:?}"
);
}
#[tokio::test]
async fn reporting_flushes_first() {
let mut pump = PumpHarness::start_over_pipes();
pump.feed(false, "before-the-blob").await;
pump.feed(true, "and-on-stderr").await;
let _ = pump.report_fds().await;
assert_eq!(log_text(&pump.out_path), "before-the-blob\n");
assert_eq!(log_text(&pump.err_path), "and-on-stderr\n");
}
#[tokio::test]
async fn a_pump_that_has_reported_stops_reading_until_it_is_resumed() {
let mut pump = PumpHarness::start();
pump.feed(false, "before-the-report").await;
let _ = pump.report_fds().await;
pump.out_writer
.write_all(b"after-1\nafter-2\n")
.await
.unwrap();
assert_file_holds_still(&pump.out_path, "before-the-report\n").await;
pump.resume().await;
assert_file_settles(&pump.out_path, "before-the-report\nafter-1\nafter-2\n").await;
}
#[tokio::test]
async fn a_report_lands_what_the_reader_was_holding() {
let mut pump = PumpHarness::start();
let burst: String = (1..=BURST).map(|n| format!("{n}\n")).collect();
pump.out_writer.write_all(burst.as_bytes()).await.unwrap();
wait_for_a_full_logs_channel(&pump.logs).await;
let _ = pump.report_fds().await;
assert_eq!(
log_text(&pump.out_path),
burst,
"every line the reader held must be on disk once, in order, \
before the report is answered"
);
}
#[tokio::test]
async fn a_report_leaves_in_the_pipe_everything_it_did_not_write() {
let dir = tempfile::tempdir().unwrap();
let out_path = dir.path().join("out.log");
let (reader, mut writer) = std::io::pipe().unwrap();
let inherited = reader.try_clone().unwrap();
let reader = tokio::net::unix::pipe::Receiver::from_owned_fd(OwnedFd::from(reader)).unwrap();
let (logs_tx, logs) = mpsc::channel(CHANNEL_CAPACITY);
let (ctl, ctl_rx) = mpsc::channel(CHANNEL_CAPACITY);
let pipes = PipeFds {
out: Some(reader.as_raw_fd()),
err: None,
stdin: None,
channel: None,
};
spawn_log_pump(
Some(reader),
None::<DuplexStream>,
LogSink::Path(out_path.clone()),
LogSink::Path(dir.path().join("err.log")),
logs_tx,
ctl_rx,
pipes,
);
let lines = u32::try_from(SMALLEST_PIPE * 3 / 4 / RUN_LINE).unwrap();
let run: String = (1..=lines).map(|n| format!("{n:07}\n")).collect();
assert!(run.len() > READ_BUFFER, "the reader must fill");
std::io::Write::write_all(&mut writer, run.as_bytes()).unwrap();
wait_for_a_full_logs_channel(&logs).await;
let (done, ack) = oneshot::channel();
ctl.send(LogCtl::ReportFds { done }).await.unwrap();
timeout(PUMP_DEADLINE, ack)
.await
.expect("a descriptor report must be acknowledged")
.expect("the pump must answer rather than drop the acknowledgement");
drop(writer);
let rest = read_to_eof(inherited).await;
let written = log_text(&out_path);
assert!(
run.starts_with(&written),
"the pump must write the run's own bytes, in order"
);
assert!(
run.ends_with(&rest),
"what is left in the pipe must be the run's own tail"
);
assert!(
written.len() + rest.len() >= run.len() - RUN_LINE,
"{} of {} bytes reached neither the file ({}) nor the pipe ({}): the report read \
them out of the pipe and then dropped them",
run.len() - written.len() - rest.len(),
run.len(),
written.len(),
rest.len()
);
}
#[tokio::test]
async fn a_report_drains_at_most_one_bufferful() {
let mut pump = PumpHarness::start_over_pipes();
let lines = u32::try_from(SMALLEST_PIPE * 3 / 4 / RUN_LINE).unwrap();
let run: String = (1..=lines).map(|n| format!("{n:07}\n")).collect();
assert!(run.len() > READ_BUFFER, "the run must reach the bound");
pump.out_writer.write_all(run.as_bytes()).await.unwrap();
wait_for_a_full_logs_channel(&pump.logs).await;
pump.flush().await;
let before = fs::metadata(&pump.out_path).unwrap().len();
let _ = pump.report_fds().await;
let drained = fs::metadata(&pump.out_path).unwrap().len() - before;
assert!(
drained > 0,
"the report must rescue what the reader was holding"
);
const RESCUED: usize = READ_BUFFER + RUN_LINE;
let ceiling = u64::try_from(RESCUED + RESCUED / RUN_LINE * LOG_STAMP_BYTES).unwrap();
assert!(
drained <= ceiling,
"the report drained {drained} bytes against a ceiling of {ceiling}, which is \
more than one bufferful: it is following the sheep rather than catching up"
);
}