use core::pin::Pin;
use core::sync::atomic::{AtomicUsize, Ordering};
use core::task::{Context, Poll};
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;
use tokio::io::{AsyncWrite, BufWriter};
use tokio::sync::oneshot;
use tokio::time::{Instant, timeout};
use shep_core::logstamp::{LOG_STAMP_BYTES, Stamper, stamp_into};
use crate::runner::LogCtl;
use super::super::log_file::{LogFile, LogFiles, PipeFds, open_append, record_lock};
use super::super::{CHANNEL_CAPACITY, IDLE_FLUSH, LOG_BUFFER};
use super::*;
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn narration_cannot_tear_a_line_the_pump_is_writing() {
let dir = tempfile::tempdir().expect("a temp dir");
let path = dir.path().join("torn.log");
let long = "x".repeat(LOG_BUFFER + 512);
let pump = {
let path = path.clone();
let long = long.clone();
tokio::spawn(async move {
let mut log = LogFile::open(path, true).await;
for i in 0..1200 {
log.append(&format!("{i} {long}")).await;
if i % 4 == 0 {
tokio::task::yield_now().await;
}
}
log.flush().await.expect("the pump's own flush");
})
};
let narrator = {
let path = path.clone();
tokio::spawn(async move {
for i in 0..600 {
let mut written = String::new();
stamp_into(&mut written);
written.push_str(&format!("[shep] narration {i}\n"));
let file = open_append(&path).await.expect("the narration handle");
let _record = record_lock(&path).lock_owned().await;
let mut file = file;
use tokio::io::AsyncWriteExt as _;
file.write_all(written.as_bytes()).await.expect("write");
file.flush().await.expect("flush");
}
})
};
pump.await.expect("the pump task");
narrator.await.expect("the narration task");
let text = std::fs::read_to_string(&path).expect("the log is readable");
let mut lines = 0_usize;
for line in text.lines() {
lines += 1;
let once = shep_core::logstamp::strip(line);
assert_ne!(
once, line,
"a line reached the file with no stamp: {line:.80}"
);
assert_eq!(
shep_core::logstamp::strip(once),
once,
"a line carries two stamps, so a record was torn: {line:.80}"
);
}
assert_eq!(lines, 1800, "every record reached the file exactly once");
}
#[tokio::test]
async fn a_last_line_written_before_the_sheep_task_lets_go_reaches_the_file() {
for attempt in 0..RACE_ATTEMPTS {
let mut pump = PumpHarness::start();
pump.out_writer.write_all(b"last-words\n").await.unwrap();
drop(pump.out_writer);
drop(pump.err_writer);
drop(pump.logs);
let settled = wait_for_file_to_contain(&pump.out_path, "last-words").await;
assert!(
settled,
"attempt {attempt}: the pump dropped the line it had not read yet, \
leaving {:?}",
fs::read_to_string(&pump.out_path)
);
}
}
#[tokio::test]
async fn a_last_line_survives_the_control_channel_closing_with_the_logs() {
for attempt in 0..RACE_ATTEMPTS {
let mut pump = PumpHarness::start();
pump.out_writer.write_all(b"last-words\n").await.unwrap();
drop(pump.out_writer);
drop(pump.err_writer);
drop(pump.ctl);
drop(pump.logs);
let settled = wait_for_file_to_contain(&pump.out_path, "last-words").await;
assert!(
settled,
"attempt {attempt}: the pump took an exit that skips the drain, \
leaving {:?}",
fs::read_to_string(&pump.out_path)
);
}
}
async fn wait_for_file_to_contain(path: &Path, needle: &str) -> bool {
timeout(PUMP_DEADLINE, async {
while !fs::read_to_string(path)
.unwrap_or_default()
.contains(needle)
{
tokio::time::sleep(Duration::from_millis(2)).await;
}
})
.await
.is_ok()
}
#[derive(Clone, Debug, Default)]
struct WriteCounter {
writes: Arc<AtomicUsize>,
bytes: Arc<AtomicUsize>,
}
impl WriteCounter {
fn writes(&self) -> usize {
self.writes.load(Ordering::Relaxed)
}
fn bytes(&self) -> usize {
self.bytes.load(Ordering::Relaxed)
}
}
impl AsyncWrite for WriteCounter {
fn poll_write(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
self.writes.fetch_add(1, Ordering::Relaxed);
self.bytes.fetch_add(buf.len(), Ordering::Relaxed);
Poll::Ready(Ok(buf.len()))
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
}
fn buffered_log<W: AsyncWrite + Unpin>(path: &Path, sink: W) -> LogFile<W> {
LogFile {
record: record_lock(path),
path: path.to_path_buf(),
handle: Some(BufWriter::with_capacity(LOG_BUFFER, sink)),
buffered_since: None,
stamp: String::new(),
stamper: Some(Stamper::default()),
}
}
#[tokio::test(start_paused = true)]
async fn a_run_of_lines_costs_one_write_per_bufferful_not_one_per_line() {
const LINE: &str = "012345678901234567890123456789012345678901234567890123456789012345678";
const LINE_BYTES: usize = LOG_STAMP_BYTES + LINE.len() + 1;
let lines = 3 * LOG_BUFFER / LINE_BYTES;
let sink = WriteCounter::default();
let mut log = buffered_log(Path::new("counted.log"), sink.clone());
for _ in 0..lines {
log.append(LINE).await;
}
log.flush().await.unwrap();
let total = lines * LINE_BYTES;
let ceiling = total.div_ceil(LOG_BUFFER) + 1; assert_eq!(
sink.bytes(),
total,
"buffering must not lose or repeat a byte"
);
assert!(
sink.writes() <= ceiling,
"{lines} lines cost {} writes; one per bufferful plus the closing flush is {ceiling}",
sink.writes()
);
assert!(
sink.writes() < lines,
"a write per line is the regression this exists to catch: \
{lines} lines, {} writes",
sink.writes()
);
}
#[tokio::test(start_paused = true)]
async fn a_line_from_a_sheep_that_then_goes_quiet_still_reaches_its_file() {
const LINE: &str = "the-only-line";
let sink = WriteCounter::default();
let mut log = buffered_log(Path::new("quiet.log"), sink.clone());
log.append(LINE).await;
assert_eq!(
sink.bytes(),
0,
"one short line must sit in the buffer, or there is nothing for the idle flush to do"
);
let armed = log
.buffered_since
.expect("an appended line must arm the flush deadline");
tokio::time::sleep_until(armed + IDLE_FLUSH).await;
log.flush().await.unwrap();
assert_eq!(
sink.bytes(),
LOG_STAMP_BYTES + LINE.len() + 1,
"the stamp, the line and its newline must reach the file once the window closes"
);
assert_eq!(
log.buffered_since, None,
"a flush must retire the deadline, or the pump re-arms it every window"
);
}
#[tokio::test]
async fn a_sheep_with_log_timestamps_off_gets_its_lines_as_written() {
let mut pump = PumpHarness::start_stamped(false);
pump.feed(false, "out before").await;
pump.feed(true, "err before").await;
pump.reopen().await;
pump.feed(false, "out after").await;
pump.flush().await;
let raw = |path: &PathBuf| fs::read_to_string(path).unwrap();
assert_eq!(raw(&pump.out_path), "out before\nout after\n");
assert_eq!(raw(&pump.err_path), "err before\n");
}
#[tokio::test]
async fn a_line_carries_the_time_it_was_written() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("stamped.log");
let mut log = LogFile::open(path.clone(), true).await;
log.append("the-sheep-said-this").await;
log.flush().await.expect("the file must be writable");
let written = fs::read_to_string(&path).unwrap();
let line = written.strip_suffix('\n').expect("one whole line");
let (stamp, rest) = line.split_at(LOG_STAMP_BYTES);
assert_eq!(
rest, "the-sheep-said-this",
"the sheep's own bytes must survive the prefix, whole and unaltered"
);
let stamp = stamp
.strip_suffix(' ')
.expect("one space must separate the stamp from the line");
let parsed = chrono::DateTime::parse_from_rfc3339(stamp)
.unwrap_or_else(|err| panic!("{stamp:?} must parse as RFC 3339: {err}"));
let drift = (chrono::Utc::now() - parsed.to_utc()).num_seconds().abs();
assert!(
drift < 60,
"{stamp:?} is {drift}s from now; the stamp must be the moment the line was written"
);
}
#[tokio::test(start_paused = true)]
async fn the_idle_flush_window_is_measured_from_the_oldest_buffered_line() {
let dir = tempfile::tempdir().unwrap();
let mut files = LogFiles {
out: LogFile::open(dir.path().join("out.log"), true).await,
err: LogFile::open(dir.path().join("err.log"), true).await,
pipes: PipeFds::default(),
#[cfg(unix)]
parked: false,
};
assert_eq!(
files.flush_deadline(),
None,
"an untouched pair owes no flush"
);
let oldest = Instant::now();
files.stream(false).append("out-first").await;
assert_eq!(files.flush_deadline(), Some(oldest + IDLE_FLUSH));
tokio::time::advance(IDLE_FLUSH / 2).await;
files.stream(false).append("out-second").await;
files.stream(true).append("err-first").await;
assert_eq!(
files.flush_deadline(),
Some(oldest + IDLE_FLUSH),
"a later line must not push the window out"
);
files.flush_idle().await;
assert_eq!(
files.flush_deadline(),
None,
"a flush must retire the deadline it satisfied"
);
}
#[tokio::test]
async fn a_rotation_lands_every_buffered_line_exactly_once() {
let mut pump = PumpHarness::start();
let mut before = String::new();
for n in 0..40 {
let line = format!("before-{n}");
pump.feed(false, &line).await;
before.push_str(&line);
before.push('\n');
}
assert!(
before.len() < LOG_BUFFER,
"the case needs lines small enough that the buffer may still hold them"
);
let rotated = pump.dir.path().join("out.log.1");
fs::rename(&pump.out_path, &rotated).unwrap();
pump.reopen().await;
assert_eq!(log_text(&rotated), before);
assert_eq!(log_text(&pump.out_path), "");
let mut after = String::new();
for n in 0..40 {
let line = format!("after-{n}");
pump.feed(false, &line).await;
after.push_str(&line);
after.push('\n');
}
pump.flush().await;
assert_eq!(log_text(&pump.out_path), after);
assert_eq!(
log_text(&rotated),
before,
"the archive must have stopped growing at the swap"
);
}
#[tokio::test]
async fn a_reopen_moves_both_streams_onto_the_recreated_paths() {
let mut pump = PumpHarness::start();
pump.feed(false, "before-out").await;
pump.feed(true, "before-err").await;
let rotated_out = pump.dir.path().join("out.log.1");
let rotated_err = pump.dir.path().join("err.log.1");
fs::rename(&pump.out_path, &rotated_out).unwrap();
fs::rename(&pump.err_path, &rotated_err).unwrap();
assert!(
!pump.out_path.exists(),
"sanity: the rename really moved it"
);
pump.reopen().await;
assert_eq!(log_text(&rotated_out), "before-out\n");
assert_eq!(log_text(&rotated_err), "before-err\n");
assert_eq!(log_text(&pump.out_path), "");
assert_eq!(log_text(&pump.err_path), "");
pump.feed(false, "after-out").await;
pump.feed(true, "after-err").await;
pump.reopen().await;
assert_eq!(log_text(&pump.out_path), "after-out\n");
assert_eq!(log_text(&pump.err_path), "after-err\n");
assert_eq!(log_text(&rotated_out), "before-out\n");
assert_eq!(log_text(&rotated_err), "before-err\n");
}
#[tokio::test]
async fn a_reopened_handle_still_appends_so_a_truncation_leaves_no_hole() {
let mut pump = PumpHarness::start();
pump.feed(false, "first").await;
fs::rename(&pump.out_path, pump.dir.path().join("out.log.1")).unwrap();
pump.reopen().await;
pump.feed(false, "second").await;
assert_file_settles(&pump.out_path, "second\n").await;
fs::File::create(&pump.out_path).unwrap();
assert_eq!(fs::metadata(&pump.out_path).unwrap().len(), 0);
pump.feed(false, "third").await;
assert_file_settles(&pump.out_path, "third\n").await;
}
#[cfg(unix)]
#[tokio::test]
async fn opening_a_symlinked_log_path_is_refused_rather_than_followed() {
let dir = tempfile::tempdir().unwrap();
let target = dir.path().join("someone-elses.conf");
let link = dir.path().join("web-out.log");
fs::write(&target, b"original").unwrap();
std::os::unix::fs::symlink(&target, &link).unwrap();
let error = open_append(&link)
.await
.expect_err("a symlinked log path must not be opened for appending");
assert_eq!(
fs::read(&target).unwrap(),
b"original",
"a refused open must not have reached the symlink's target at all"
);
assert_eq!(
error.to_string(),
crate::runner::SYMLINK_REFUSED,
"the operator who reads this needs the word symlink, not ELOOP's \
own wording about levels of them"
);
}
#[cfg(target_os = "linux")]
#[tokio::test]
async fn a_log_path_below_a_symlinked_parent_the_daemon_owns_opens_on_linux() {
let dir = tempfile::tempdir().unwrap();
let real = dir.path().join("real");
let link = dir.path().join("logs");
fs::create_dir(&real).unwrap();
std::os::unix::fs::symlink(&real, &link).unwrap();
open_append(&link.join("web-out.log"))
.await
.expect("a link the daemon's own user owns is trusted");
assert!(real.join("web-out.log").exists());
}
#[cfg(target_os = "linux")]
#[tokio::test]
async fn a_plain_log_path_still_appends_after_the_resolving_open() {
use tokio::io::AsyncWriteExt as _;
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("web-out.log");
let mut handle = open_append(&path).await.unwrap();
handle.write_all(b"first line\n").await.unwrap();
handle.flush().await.unwrap();
fs::File::create(&path).unwrap();
handle.write_all(b"second\n").await.unwrap();
handle.flush().await.unwrap();
assert_eq!(fs::read(&path).unwrap(), b"second\n");
}
#[tokio::test]
async fn a_reopen_is_answered_while_both_streams_are_idle() {
let pump = PumpHarness::start();
pump.reopen().await;
fs::remove_file(&pump.out_path).unwrap();
fs::remove_file(&pump.err_path).unwrap();
pump.reopen().await;
assert!(pump.out_path.exists(), "stdout's path must be back");
assert!(pump.err_path.exists(), "stderr's path must be back");
}
#[tokio::test]
async fn a_reopen_is_answered_while_the_logs_channel_is_full() {
let mut pump = PumpHarness::start();
let flooded = CHANNEL_CAPACITY + 1;
let mut written = String::new();
for n in 0..flooded {
let line = format!("line-{n}\n");
pump.out_writer.write_all(line.as_bytes()).await.unwrap();
written.push_str(&line);
}
assert_file_settles(&pump.out_path, &written).await;
fs::remove_file(&pump.out_path).unwrap();
fs::remove_file(&pump.err_path).unwrap();
pump.reopen().await;
assert!(pump.out_path.exists(), "stdout's path must be back");
assert!(pump.err_path.exists(), "stderr's path must be back");
for n in 0..flooded {
let observed = timeout(PUMP_DEADLINE, pump.logs.recv())
.await
.expect("the pump must resume once `logs` has room")
.expect("the pump must not end while its streams are open");
assert_eq!(observed.line, format!("line-{n}"));
}
}
#[tokio::test]
async fn a_stream_that_has_ended_is_still_reopened() {
let mut pump = PumpHarness::start();
pump.feed(false, "before-out").await;
pump.feed(true, "before-err").await;
pump.out_writer
.shutdown()
.await
.expect("a duplex stream's write end must close");
let_the_duplex_pump_settle().await;
fs::remove_file(&pump.out_path).unwrap();
fs::remove_file(&pump.err_path).unwrap();
let outcome = pump.reopen_for_answer().await;
assert_eq!(
outcome,
Ok(()),
"an ended stream's path is as openable as a live one's"
);
assert!(
pump.out_path.exists(),
"the ended stream's path must come back too"
);
assert!(
pump.err_path.exists(),
"the live stream's path must be back"
);
}
#[tokio::test]
async fn a_send_fails_once_the_pump_has_ended() {
let mut pump = PumpHarness::start();
pump.feed(false, "before-out").await;
pump.feed(true, "before-err").await;
drop(pump.out_writer);
drop(pump.err_writer);
let ended = timeout(PUMP_DEADLINE, pump.logs.recv())
.await
.expect("the pump must end once both streams reach EOF");
assert!(ended.is_none(), "a pump that has ended sends nothing more");
let (done, ack) = oneshot::channel();
assert!(
pump.ctl.send(LogCtl::Reopen { done }).await.is_err(),
"a reopen aimed at an ended pump must fail to send"
);
assert!(ack.await.is_err());
}
#[tokio::test]
async fn a_reopen_that_cannot_open_a_path_again_answers_with_the_failure() {
let pump = PumpHarness::start();
pump.reopen().await;
fs::rename(&pump.out_path, pump.dir.path().join("out.log.1")).unwrap();
fs::create_dir(&pump.out_path).unwrap();
fs::remove_file(&pump.err_path).unwrap();
let error = pump
.reopen_for_answer()
.await
.expect_err("a reopen that could not open stdout's path must say so");
assert!(
error.message.contains(pump.out_path.to_str().unwrap()),
"the failure must name the path it could not open: {error}"
);
assert!(
!error.message.contains(pump.err_path.to_str().unwrap()),
"stderr's path opened fine and must not be reported: {error}"
);
assert!(
pump.err_path.exists(),
"stderr must be reopened even though stdout's open failed"
);
}
#[tokio::test]
async fn a_flush_lands_both_streams_and_keeps_writing_afterwards() {
let mut pump = PumpHarness::start();
pump.feed(false, "out-before").await;
pump.feed(true, "err-before").await;
pump.flush().await;
assert_eq!(log_text(&pump.out_path), "out-before\n");
assert_eq!(log_text(&pump.err_path), "err-before\n");
pump.feed(false, "out-after").await;
assert_file_settles(&pump.out_path, "out-before\nout-after\n").await;
}
#[tokio::test]
async fn a_flush_reports_the_write_its_file_never_took() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("out.log");
fs::write(&path, "").unwrap();
let file = tokio::fs::File::open(&path).await.unwrap();
let mut log = buffered_log(&path, file);
log.append("never-lands").await;
let error = log
.flush()
.await
.expect_err("a write that never reached the file must fail the flush");
assert!(
error.message.starts_with(&format!("{}: ", path.display())),
"the failure must name the file it belongs to: {error}"
);
}
#[cfg(unix)]
#[tokio::test]
async fn a_log_file_from_an_open_handle_still_appends() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("out.log");
fs::write(&path, "first\n").unwrap();
let handle = open_append(&path).await.unwrap();
let mut log = LogFile::from_file(path.clone(), handle, true);
log.append("second").await;
log.flush().await.expect("the carried handle must be live");
assert_eq!(
log_text(&path),
"first\nsecond\n",
"a carried handle must append, not write at its own offset"
);
}
#[tokio::test]
async fn dropping_the_logs_receiver_ends_a_pump_with_no_line_to_carry_the_news() {
let pump = PumpHarness::start();
pump.reopen().await;
drop(pump.logs);
timeout(PUMP_DEADLINE, pump.ctl.closed())
.await
.expect("a pump whose `logs` receiver is gone must end");
}
#[tokio::test]
async fn dropping_the_control_sender_ends_the_pump() {
let mut pump = PumpHarness::start();
pump.feed(false, "still-here").await;
drop(pump.ctl);
let after = timeout(PUMP_DEADLINE, pump.logs.recv())
.await
.expect("the pump must end once nothing can control it");
assert!(after.is_none(), "a pump that has ended sends nothing more");
}