use super::*;
#[tokio::test(start_paused = true)]
async fn a_flush_reaches_every_matched_sheep_and_no_others() {
let (events, _rx) = crate::bus::test_bus(64);
let runner = Arc::new(ScriptedRunner::new(vec![
ProcScript::never_exits(),
ProcScript::never_exits(),
ProcScript::never_exits(),
]));
let dir = tempfile::tempdir().unwrap();
let handle = spawn_supervisor(SharedRunner(Arc::clone(&runner)), test_paths(&dir), events);
let mut web = AppConfig::minimal("web", "./srv");
web.instances = 2;
handle
.start(vec![
normalize(web).unwrap(),
normalize(AppConfig::minimal("api", "./api")).unwrap(),
])
.await
.unwrap();
let flushed = handle
.flush(ProcessSelector::Name("web".to_string()))
.await
.unwrap();
assert_eq!(
flushed.iter().map(|info| info.id).collect::<Vec<_>>(),
vec![0, 1],
"the reply must carry both `web` instances, id-sorted, and no `api`"
);
assert_eq!(runner.flushes(0), 1, "web's first instance");
assert_eq!(runner.flushes(1), 1, "web's second instance");
assert_eq!(runner.flushes(2), 0, "api was never named");
assert_eq!(
runner.reopens(0),
0,
"a flush must push `LogCtl::Flush`, never `LogCtl::Reopen` — a \
flush wired to the neighbouring variant would swap the flock's \
handles and empty nothing"
);
}
#[tokio::test(start_paused = true)]
async fn the_actor_keeps_answering_while_a_flush_waits_on_a_silent_pump() {
let (events, _rx) = crate::bus::test_bus(64);
let (runner, mut requests) = SilentPumpRunner::new(vec![ProcScript::never_exits()]);
let dir = tempfile::tempdir().unwrap();
let handle = spawn_supervisor(runner, test_paths(&dir), events);
handle
.start(vec![normalize(AppConfig::minimal("web", "./srv")).unwrap()])
.await
.unwrap();
let flushing = tokio::spawn({
let handle = handle.clone();
async move { handle.flush(ProcessSelector::All).await }
});
tokio::time::timeout(Duration::from_secs(5), requests.wait_for(|seen| *seen == 1))
.await
.expect("the flush must reach the pump")
.expect("the runner outlives this wait, so its sender cannot have closed");
let listed = tokio::time::timeout(Duration::from_secs(5), handle.list())
.await
.expect("the actor must keep answering while a flush is outstanding");
assert_eq!(listed.len(), 1);
assert!(
!flushing.is_finished(),
"sanity: nothing can acknowledge this flush, so `list` answering \
above is not just the flush having finished first"
);
flushing.abort();
}
const LATE_LINE: &str = "landed-while-the-flush-was-being-answered\n";
const LATE_WRITING_SHEEP: &str = "latecomer";
struct LateWritingPumpRunner {
inner: ScriptedRunner,
}
impl LateWritingPumpRunner {
fn new(inner: ScriptedRunner) -> Self {
Self { inner }
}
}
impl fmt::Debug for LateWritingPumpRunner {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("LateWritingPumpRunner")
.finish_non_exhaustive()
}
}
impl ProcessRunner for LateWritingPumpRunner {
type Proc = crate::fake::FakeProc;
fn spawn(&self, spec: &SpawnSpec) -> Result<(Self::Proc, ProcIo), crate::runner::RunnerError> {
let (proc, mut io) = self.inner.spawn(spec)?;
if spec.name != LATE_WRITING_SHEEP {
return Ok((proc, io));
}
let (tx, mut rx) = mpsc::channel(SHEEP_CTL_CAPACITY);
io.log_ctl = tx;
let out_file = spec.out_file.clone();
tokio::spawn(async move {
while let Some(ctl) = rx.recv().await {
match ctl {
LogCtl::Flush { done } => {
if let Some(parent) = out_file.parent() {
std::fs::create_dir_all(parent).unwrap();
}
let mut file = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(&out_file)
.unwrap();
std::io::Write::write_all(&mut file, LATE_LINE.as_bytes()).unwrap();
let _ = done.send(Ok(()));
}
LogCtl::Reopen { done } => {
let _ = done.send(Ok(()));
}
#[cfg(unix)]
LogCtl::ReportFds { done } => {
let _ = done.send(CarriedFds::none());
}
#[cfg(unix)]
LogCtl::Resume => {}
}
}
});
Ok((proc, io))
}
}
#[tokio::test(start_paused = true)]
async fn a_flush_truncates_only_after_its_pump_has_answered() {
let (events, _rx) = crate::bus::test_bus(64);
let dir = tempfile::tempdir().unwrap();
let handle = spawn_supervisor(
LateWritingPumpRunner::new(ScriptedRunner::new(vec![ProcScript::never_exits()])),
test_paths(&dir),
events,
);
handle
.start(vec![
normalize(AppConfig::minimal(LATE_WRITING_SHEEP, "./srv")).unwrap(),
])
.await
.unwrap();
let out_file = PathBuf::from(
handle.list().await[0]
.out_file
.clone()
.expect("the daemon reports its own resolved log paths"),
);
handle.flush(ProcessSelector::All).await.unwrap();
assert!(
out_file.exists(),
"the pump never wrote, so this flush never reached one: {}",
out_file.display()
);
assert_eq!(
std::fs::read_to_string(&out_file).unwrap(),
"",
"a line the pump landed as it answered the flush must not survive \
the truncate that follows it"
);
}
#[tokio::test(start_paused = true)]
async fn a_stopped_sheeps_log_file_is_truncated_too() {
let (events, _rx) = crate::bus::test_bus(64);
let runner = ScriptedRunner::new(vec![ProcScript::never_exits()]);
let dir = tempfile::tempdir().unwrap();
let handle = spawn_supervisor(runner, test_paths(&dir), events);
let mut app = AppConfig::minimal("web", "./srv");
app.autorestart = false;
handle.start(vec![normalize(app).unwrap()]).await.unwrap();
handle
.stop(ProcessSelector::Name("web".to_string()))
.await
.unwrap();
let listed = handle.list().await;
assert_eq!(listed[0].status, ProcStatus::Stopped);
let out_file = PathBuf::from(listed[0].out_file.clone().unwrap());
std::fs::create_dir_all(out_file.parent().unwrap()).unwrap();
std::fs::write(&out_file, "what the sheep logged before it stopped\n").unwrap();
let flushed = tokio::time::timeout(Duration::from_secs(5), handle.flush(ProcessSelector::All))
.await
.expect("a flush aimed at a stopped sheep must not wait for an acknowledgement")
.expect("a stopped sheep has no pump to flush, which is not a failure");
assert_eq!(flushed.len(), 1);
assert_eq!(flushed[0].status, ProcStatus::Stopped);
assert_eq!(
std::fs::read_to_string(&out_file).unwrap(),
"",
"a stopped sheep's log is still readable, so it is still emptied"
);
}
#[tokio::test(start_paused = true)]
async fn instances_sharing_one_log_path_answer_one_row_each() {
let (events, _rx) = crate::bus::test_bus(64);
let runner = ScriptedRunner::new(vec![ProcScript::never_exits(), ProcScript::never_exits()]);
let dir = tempfile::tempdir().unwrap();
let handle = spawn_supervisor(runner, test_paths(&dir), events);
let mut web = AppConfig::minimal("web", "./srv");
web.instances = 2;
web.merge_logs = true;
handle.start(vec![normalize(web).unwrap()]).await.unwrap();
let listed = handle.list().await;
assert_eq!(
listed[0].out_file, listed[1].out_file,
"fixture check: `merge_logs` must really point both instances at \
one path, or this case proves nothing"
);
let shared = PathBuf::from(listed[0].out_file.clone().unwrap());
std::fs::create_dir_all(shared.parent().unwrap()).unwrap();
std::fs::write(&shared, "both instances wrote here\n").unwrap();
let flushed = handle.flush(ProcessSelector::All).await.unwrap();
assert_eq!(
flushed.iter().map(|info| info.id).collect::<Vec<_>>(),
vec![0, 1],
"one row per sheep, not per file emptied"
);
assert_eq!(std::fs::read_to_string(&shared).unwrap(), "");
}
async fn reload_to_a_shared_path(
dir: &tempfile::TempDir,
) -> (SupervisorHandle, Arc<ScriptedRunner>, Vec<ProcessInfo>) {
let (handle, runner, mut rx) = started(
dir,
AppConfig::minimal("web", "./srv"),
vec![ProcScript::never_exits(), ProcScript::never_exits()],
)
.await;
handle
.reload(ProcessSelector::Name("web".to_string()))
.await
.expect("the reload is accepted");
expect_event(&mut rx, 1, ProcessEventKind::Start).await;
let mid = handle.list().await;
(handle, runner, mid)
}
#[track_caller]
fn assert_swap_shares_log_paths(mid: &[ProcessInfo]) {
assert_eq!(
mid.len(),
2,
"fixture check: both halves of the swap must be registered, or \
there is no shared path to widen to"
);
assert_eq!(
mid[0].out_file, mid[1].out_file,
"fixture check: one instance slot must really give both entries \
one out path, or this case proves nothing"
);
assert_eq!(
mid[0].err_file, mid[1].err_file,
"fixture check: and one err path"
);
}
#[tokio::test(start_paused = true)]
async fn a_sibling_sharing_a_path_is_flushed_even_when_the_selector_skips_it() {
let (events, _rx) = crate::bus::test_bus(64);
let dir = tempfile::tempdir().unwrap();
let handle = spawn_supervisor(
LateWritingPumpRunner::new(ScriptedRunner::new(vec![
ProcScript::never_exits(),
ProcScript::never_exits(),
])),
test_paths(&dir),
events,
);
let shared = dir.path().join("shared-out.log");
let mut named = AppConfig::minimal("web", "./srv");
named.out_file = Some(shared.display().to_string());
let mut sibling = AppConfig::minimal(LATE_WRITING_SHEEP, "./api");
sibling.out_file = Some(shared.display().to_string());
handle
.start(vec![normalize(named).unwrap(), normalize(sibling).unwrap()])
.await
.unwrap();
let listed = handle.list().await;
assert_eq!(
listed[0].out_file, listed[1].out_file,
"fixture check: both apps must really resolve to one path, or \
this case proves nothing"
);
let flushed = handle
.flush(ProcessSelector::Name("web".to_string()))
.await
.unwrap();
assert_eq!(
flushed.iter().map(|info| info.id).collect::<Vec<_>>(),
vec![0],
"the reply answers the selector: the sibling's file was emptied \
too, but the operator never named the sibling and it is not a \
row here"
);
assert!(
shared.exists(),
"the sibling's pump writes as it answers a flush, so a path that \
is not there means it was never asked: {}",
shared.display()
);
assert_eq!(
std::fs::read_to_string(&shared).unwrap(),
"",
"a line the unmatched sibling landed as it answered must not \
survive the truncate of the path it shares"
);
}
#[tokio::test(start_paused = true)]
async fn a_pump_that_could_not_flush_fails_the_request() {
let (events, _rx) = crate::bus::test_bus(64);
let scripted = Arc::new(ScriptedRunner::new(vec![
ProcScript::never_exits(),
ProcScript::never_exits(),
]));
let dir = tempfile::tempdir().unwrap();
let handle = spawn_supervisor(
FailingPumpRunner::new(Arc::clone(&scripted)),
test_paths(&dir),
events,
);
handle
.start(vec![
normalize(AppConfig::minimal(REFUSING_SHEEP, "./srv")).unwrap(),
normalize(AppConfig::minimal("api", "./api")).unwrap(),
])
.await
.unwrap();
let error = tokio::time::timeout(Duration::from_secs(5), handle.flush(ProcessSelector::All))
.await
.expect("a pump that answers must not leave the flush waiting")
.expect_err("a flush a pump could not carry out must not answer Ok");
assert_eq!(
error,
SupervisorError::FlushFailed(PUMP_REFUSAL.to_string()),
"the failure must carry the path, and only the path that failed"
);
assert_eq!(
scripted.flushes(1),
1,
"the healthy sheep must still have been flushed"
);
}
#[tokio::test(start_paused = true)]
async fn a_flush_matching_nothing_is_not_found() {
let (events, _rx) = crate::bus::test_bus(64);
let dir = tempfile::tempdir().unwrap();
let handle = spawn_supervisor(ScriptedRunner::new(vec![]), test_paths(&dir), events);
assert_eq!(
handle.flush(ProcessSelector::All).await,
Err(SupervisorError::NotFound)
);
}
#[tokio::test]
async fn truncating_a_path_that_is_not_there_creates_nothing_and_succeeds() {
let dir = tempfile::tempdir().unwrap();
let missing = dir.path().join("never-started-out.log");
assert_eq!(truncate_log(&missing).await, Ok(()));
assert!(
!missing.exists(),
"a flush must not create the log file it did not find"
);
}
#[tokio::test]
async fn truncating_a_path_that_is_a_directory_reports_the_failure() {
let dir = tempfile::tempdir().unwrap();
let blocked = dir.path().join("web-out.log");
std::fs::create_dir(&blocked).unwrap();
let error = truncate_log(&blocked)
.await
.expect_err("a path that could not be truncated must not answer Ok");
assert!(
error
.message
.starts_with(&format!("{}: ", blocked.display())),
"the failure must name the path it could not empty: {error}"
);
}
#[cfg(unix)]
#[tokio::test]
async fn truncating_a_symlinked_log_path_refuses_and_leaves_its_target_alone() {
let dir = tempfile::tempdir().unwrap();
let target = dir.path().join("precious.txt");
let link = dir.path().join("web-out.log");
std::fs::write(&target, b"do not empty me").unwrap();
std::os::unix::fs::symlink(&target, &link).unwrap();
let error = truncate_log(&link)
.await
.expect_err("a symlinked log path must not be truncated through");
assert_eq!(
std::fs::read(&target).unwrap(),
b"do not empty me",
"the symlink's target must still hold every byte it did"
);
assert!(
std::fs::symlink_metadata(&link)
.unwrap()
.file_type()
.is_symlink(),
"the refusal must leave the symlink itself in place, not replace it"
);
assert_eq!(
error.message,
format!("{}: {}", link.display(), crate::runner::SYMLINK_REFUSED),
"the failure must name the path and say the word symlink: {error}"
);
}
#[tokio::test(start_paused = true)]
async fn a_flush_naming_a_replacement_still_drains_the_drainee_sharing_its_path() {
let dir = tempfile::tempdir().unwrap();
let (handle, runner, mid) = reload_to_a_shared_path(&dir).await;
assert_swap_shares_log_paths(&mid);
let flushed = handle.flush(ProcessSelector::Id(1)).await.unwrap();
assert_eq!(
flushed.iter().map(|info| info.id).collect::<Vec<_>>(),
vec![1],
"the reply answers the selector: the drainee's pump was drained \
too, but the operator named only the replacement"
);
assert_eq!(
runner.flushes(0),
1,
"the drainee's pump is what this case exists for — it is still \
holding the file the truncate is about to empty"
);
assert_eq!(
runner.flushes(1),
1,
"the replacement's pump, which the selector did name"
);
}
#[tokio::test(start_paused = true)]
async fn a_reopen_naming_a_replacement_still_reaches_the_drainee_sharing_its_path() {
let dir = tempfile::tempdir().unwrap();
let (handle, runner, mid) = reload_to_a_shared_path(&dir).await;
assert_swap_shares_log_paths(&mid);
let reopened = handle.reopen(ProcessSelector::Id(1)).await.unwrap();
assert_eq!(
reopened.iter().map(|info| info.id).collect::<Vec<_>>(),
vec![1],
"the reply answers the selector, the same way `flush`'s does"
);
assert_eq!(
runner.reopens(0),
1,
"the drainee's pump is what this case exists for — unasked, it \
keeps the renamed inode open and goes on filling the archive"
);
assert_eq!(
runner.reopens(1),
1,
"the replacement's pump, which the selector did name"
);
assert_eq!(
runner.flushes(0),
0,
"a reopen must push `LogCtl::Reopen`, never `LogCtl::Flush` — the \
neighbouring variant would land the drainee's owed bytes and \
leave it on the renamed inode regardless"
);
}