use std::fs;
#[cfg(unix)]
use std::io;
#[cfg(unix)]
use std::os::fd::{AsRawFd, RawFd};
use std::path::{Path, PathBuf};
use std::time::Duration;
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt as _, DuplexStream};
use tokio::sync::{mpsc, oneshot};
use tokio::time::timeout;
#[cfg(unix)]
use crate::handover::CarriedFds;
use crate::runner::{LogCtl, LogLine, ReopenError};
use super::CHANNEL_CAPACITY;
#[cfg(unix)]
use super::IDLE_FLUSH;
use super::log_file::{LogSink, PipeFds};
use super::pump::spawn_log_pump;
mod handover;
#[cfg(unix)]
mod listen_layout;
mod log_io;
mod spawn_io;
#[cfg(unix)]
mod stop_signal;
const PUMP_DEADLINE: Duration = Duration::from_secs(5);
const STREAM_BUFFER: usize = 4096;
struct PumpHarness<W = DuplexStream> {
dir: tempfile::TempDir,
out_path: PathBuf,
err_path: PathBuf,
out_writer: W,
err_writer: W,
logs: mpsc::Receiver<LogLine>,
ctl: mpsc::Sender<LogCtl>,
pipes: PipeFds,
}
impl<W> PumpHarness<W> {
fn start_with<R>(
out_writer: W,
out_reader: R,
err_writer: W,
err_reader: R,
stamped: bool,
pipes: PipeFds,
) -> Self
where
R: AsyncRead + Unpin + Send + 'static,
{
let dir = tempfile::tempdir().unwrap();
let out_path = dir.path().join("out.log");
let err_path = dir.path().join("err.log");
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()),
stamped,
logs_tx,
ctl_rx,
pipes,
);
Self {
dir,
out_path,
err_path,
out_writer,
err_writer,
logs,
ctl,
pipes,
}
}
}
impl PumpHarness {
fn start() -> Self {
Self::start_stamped(true)
}
fn start_stamped(stamped: bool) -> Self {
let (out_writer, out_reader) = tokio::io::duplex(STREAM_BUFFER);
let (err_writer, err_reader) = tokio::io::duplex(STREAM_BUFFER);
Self::start_with(
out_writer,
out_reader,
err_writer,
err_reader,
stamped,
PipeFds::default(),
)
}
}
#[cfg(unix)]
impl PumpHarness<tokio::net::unix::pipe::Sender> {
fn start_over_pipes() -> Self {
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,
};
Self::start_with(out_writer, out_reader, err_writer, err_reader, true, pipes)
}
}
impl<W: AsyncWrite + Unpin> PumpHarness<W> {
#[cfg(unix)]
async fn report_fds(&self) -> CarriedFds {
request_fds(&self.ctl).await
}
#[cfg(unix)]
async fn resume(&self) {
self.ctl
.send(LogCtl::Resume)
.await
.expect("a parked pump must still be reading its control channel");
}
async fn feed(&mut self, err: bool, line: &str) {
let writer = if err {
&mut self.err_writer
} else {
&mut self.out_writer
};
writer
.write_all(format!("{line}\n").as_bytes())
.await
.unwrap();
let observed = timeout(PUMP_DEADLINE, self.logs.recv())
.await
.expect("the pump must forward a line it has read")
.expect("the pump must not end while its streams are open");
assert_eq!(
observed,
LogLine {
err,
line: line.to_string()
}
);
}
async fn reopen(&self) {
let outcome = self.reopen_for_answer().await;
assert_eq!(outcome, Ok(()), "this reopen must have worked");
}
async fn reopen_for_answer(&self) -> Result<(), ReopenError> {
ctl_acked(&self.ctl, "a reopen", |done| LogCtl::Reopen { done }).await
}
async fn flush(&self) {
let outcome = ctl_acked(&self.ctl, "a flush", |done| LogCtl::Flush { done }).await;
assert_eq!(outcome, Ok(()), "this flush must have worked");
}
}
async fn ctl_acked<T>(
ctl: &mpsc::Sender<LogCtl>,
what: &str,
make_cmd: impl FnOnce(oneshot::Sender<T>) -> LogCtl,
) -> T {
let (done, ack) = oneshot::channel();
ctl.send(make_cmd(done))
.await
.expect("the pump must still be reading its control channel");
timeout(PUMP_DEADLINE, ack)
.await
.unwrap_or_else(|_| panic!("{what} must be acknowledged"))
.expect("the pump must answer rather than drop the acknowledgement")
}
#[cfg(unix)]
async fn request_fds(ctl: &mpsc::Sender<LogCtl>) -> CarriedFds {
let report = |done| LogCtl::ReportFds { done };
ctl_acked(ctl, "a descriptor report", report).await
}
fn log_text(path: &Path) -> String {
unstamped(&fs::read_to_string(path).unwrap())
}
fn unstamped(text: &str) -> String {
let mut out = String::new();
for line in text.lines() {
out.push_str(shep_core::logstamp::strip(line));
out.push('\n');
}
out
}
async fn assert_file_settles(path: &Path, expected: &str) {
let settled = timeout(PUMP_DEADLINE, async {
while unstamped(&fs::read_to_string(path).unwrap_or_default()) != expected {
tokio::time::sleep(Duration::from_millis(5)).await;
}
})
.await;
assert!(
settled.is_ok(),
"{}: expected {expected:?}, found {:?}",
path.display(),
fs::read_to_string(path)
);
}
#[cfg(unix)]
fn is_open(fd: RawFd) -> bool {
nix::fcntl::fcntl(fd, nix::fcntl::FcntlArg::F_GETFD).is_ok()
}
#[cfg(unix)]
async fn report_fds_until(
ctl: &mpsc::Sender<LogCtl>,
settled: impl Fn(&CarriedFds) -> bool,
) -> CarriedFds {
let mut last: Option<CarriedFds> = None;
let report = timeout(PUMP_DEADLINE, async {
loop {
let (done, ack) = oneshot::channel();
ctl.send(LogCtl::ReportFds { done })
.await
.expect("the pump must still be reading its control channel");
let fds = ack
.await
.expect("the pump must answer a descriptor report rather than drop it");
if settled(&fds) {
return fds;
}
last = Some(fds);
ctl.send(LogCtl::Resume)
.await
.expect("a parked pump must still be reading its control channel");
tokio::time::sleep(Duration::from_millis(5)).await;
}
})
.await;
match report {
Ok(fds) => fds,
Err(_) => panic!(
"the pump never reported a settled descriptor set inside {PUMP_DEADLINE:?}, \
last report {last:?}"
),
}
}
const RACE_ATTEMPTS: usize = 32;
#[cfg(unix)]
async fn fill_pipe(writer: &tokio::net::unix::pipe::Sender, line: &str) -> usize {
timeout(PUMP_DEADLINE, writer.writable())
.await
.expect("an empty pipe is writable")
.expect("the pipe is open");
let mut whole = 0usize;
loop {
match writer.try_write(line.as_bytes()) {
Ok(written) if written == line.len() => whole += 1,
Ok(_) => return whole,
Err(error) if error.kind() == io::ErrorKind::WouldBlock => return whole,
Err(error) => {
panic!("the fill failed for a reason that is not a full pipe: {error}")
}
}
}
}
#[cfg(unix)]
const STILL_WINDOWS: u32 = 6;
#[cfg(unix)]
const BURST: u32 = 60;
#[cfg(unix)]
async fn assert_file_holds_still(path: &Path, expected: &str) {
for window in 1..=STILL_WINDOWS {
tokio::time::sleep(IDLE_FLUSH).await;
assert_eq!(
log_text(path),
expected,
"{}: a parked pump wrote during flush window {window}",
path.display()
);
}
}
#[cfg(unix)]
async fn wait_for_a_full_logs_channel(logs: &mpsc::Receiver<LogLine>) {
let filled = timeout(PUMP_DEADLINE, async {
while logs.len() < CHANNEL_CAPACITY {
tokio::time::sleep(Duration::from_millis(5)).await;
}
})
.await;
assert!(
filled.is_ok(),
"the pump must fall behind a burst it cannot forward"
);
}
#[cfg(unix)]
async fn read_to_eof(handle: std::io::PipeReader) -> String {
let mut out = Vec::new();
let mut buf = [0_u8; 4096];
let drained = timeout(PUMP_DEADLINE, async {
loop {
match std::io::Read::read(&mut &handle, &mut buf) {
Ok(0) => return,
Ok(n) => out.extend_from_slice(&buf[..n]),
Err(error) if error.kind() == io::ErrorKind::WouldBlock => {
tokio::time::sleep(Duration::from_millis(5)).await;
}
Err(error) => panic!("the successor's handle must be readable: {error}"),
}
}
})
.await;
assert!(
drained.is_ok(),
"the pipe must reach EOF once the sheep is gone"
);
String::from_utf8(out).expect("a pipe of ASCII lines")
}
#[cfg(unix)]
const SMALLEST_PIPE: usize = 16 * 1024;
#[cfg(unix)]
const RUN_LINE: usize = 8;
async fn let_the_duplex_pump_settle() {
for _ in 0..2 {
tokio::task::yield_now().await;
}
}