WS-B: review fixes (UNIT disjunct, offline tests)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YShymaPrXmc3KYRUg2HMSs
This commit is contained in:
1 parent
bc8daf3295
commit
2a74bb1047
2 files changed
+166
-33
No files matched your search
+165
-33
@@ -88,20 +88,44 @@ fn open_filtered(ns: Option<&str>, q: &JournalQuery) -> Result<Journal, AppError
|
||||
None => opts.open(),
|
||||
}
|
||||
.map_err(unavailable)?;
|
||||
if let Some(unit) = &q.unit {
|
||||
j.match_add("_SYSTEMD_UNIT", unit.as_str())
|
||||
.map_err(unavailable)?;
|
||||
}
|
||||
if let Some(p) = q.priority {
|
||||
// Same-field matches are OR'ed: PRIORITY in 0..=p.
|
||||
for k in 0..=p.as_u8() {
|
||||
j.match_add("PRIORITY", k.to_string())
|
||||
.map_err(unavailable)?;
|
||||
let plan = match_plan(q);
|
||||
for (i, group) in plan.iter().enumerate() {
|
||||
if i > 0 {
|
||||
j.match_or().map_err(unavailable)?;
|
||||
}
|
||||
for (k, v) in group {
|
||||
j.match_add(k, v.as_str()).map_err(unavailable)?;
|
||||
}
|
||||
}
|
||||
Ok(j)
|
||||
}
|
||||
|
||||
/// Disjuncts of AND-ed matches. sd-journal ANDs across fields and ORs within
|
||||
/// one field, so the priority set is repeated inside each unit disjunct
|
||||
/// (`_SYSTEMD_UNIT` for the unit's own lines, `UNIT` for PID1-style lines).
|
||||
fn match_plan(q: &JournalQuery) -> Vec<Vec<(String, String)>> {
|
||||
let prio: Vec<(String, String)> = q
|
||||
.priority
|
||||
.map(|p| {
|
||||
(0..=p.as_u8())
|
||||
.map(|k| ("PRIORITY".to_string(), k.to_string()))
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
match &q.unit {
|
||||
None if prio.is_empty() => Vec::new(),
|
||||
None => vec![prio],
|
||||
Some(u) => ["_SYSTEMD_UNIT", "UNIT"]
|
||||
.iter()
|
||||
.map(|f| {
|
||||
let mut g = vec![(f.to_string(), u.clone())];
|
||||
g.extend(prio.iter().cloned());
|
||||
g
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn to_entry(j: &mut Journal, rec: JournalRecord) -> JournalEntry {
|
||||
let ts = j
|
||||
.timestamp_usec()
|
||||
@@ -128,20 +152,21 @@ fn to_entry(j: &mut Journal, rec: JournalRecord) -> JournalEntry {
|
||||
}
|
||||
}
|
||||
|
||||
/// Read up to `lines` most recent matching entries, oldest first.
|
||||
fn read_backlog(
|
||||
j: &mut Journal,
|
||||
q: &JournalQuery,
|
||||
/// Collect up to `lines` entries from a newest-first source, honoring
|
||||
/// `since`, and return them oldest first. Entries with an unreadable
|
||||
/// timestamp (epoch fallback) are skipped rather than ending the scan.
|
||||
fn collect_backlog(
|
||||
mut prev: impl FnMut() -> Result<Option<JournalEntry>, AppError>,
|
||||
since: Option<Timestamp>,
|
||||
lines: usize,
|
||||
) -> Result<Vec<JournalEntry>, AppError> {
|
||||
j.seek_tail().map_err(unavailable)?;
|
||||
let mut out = Vec::new();
|
||||
while out.len() < lines {
|
||||
let Some(rec) = j.previous_entry().map_err(unavailable)? else {
|
||||
break;
|
||||
};
|
||||
let e = to_entry(j, rec);
|
||||
if q.since.is_some_and(|s| e.timestamp < s) {
|
||||
let Some(e) = prev()? else { break };
|
||||
if e.timestamp == Timestamp::UNIX_EPOCH {
|
||||
continue;
|
||||
}
|
||||
if since.is_some_and(|s| e.timestamp < s) {
|
||||
break;
|
||||
}
|
||||
out.push(e);
|
||||
@@ -150,8 +175,25 @@ fn read_backlog(
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Read up to `lines` most recent matching entries, oldest first.
|
||||
fn read_backlog(
|
||||
j: &mut Journal,
|
||||
q: &JournalQuery,
|
||||
lines: usize,
|
||||
) -> Result<Vec<JournalEntry>, AppError> {
|
||||
j.seek_tail().map_err(unavailable)?;
|
||||
collect_backlog(
|
||||
|| match j.previous_entry().map_err(unavailable)? {
|
||||
Some(rec) => Ok(Some(to_entry(j, rec))),
|
||||
None => Ok(None),
|
||||
},
|
||||
q.since,
|
||||
lines,
|
||||
)
|
||||
}
|
||||
|
||||
/// Send with backpressure; returns false when the consumer or shutdown ended us.
|
||||
fn send(tx: &mpsc::Sender<JournalEntry>, token: &CancellationToken, mut e: JournalEntry) -> bool {
|
||||
fn send<T>(tx: &mpsc::Sender<T>, token: &CancellationToken, mut e: T) -> bool {
|
||||
loop {
|
||||
if token.is_cancelled() {
|
||||
return false;
|
||||
@@ -244,21 +286,109 @@ impl JournalSource for SdJournal {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// B6: dropping the receiver ends the blocking follow thread within ~1s.
|
||||
fn entry(sec: i64) -> JournalEntry {
|
||||
JournalEntry {
|
||||
timestamp: Timestamp::from_second(sec).unwrap(),
|
||||
priority: None,
|
||||
unit: None,
|
||||
identifier: None,
|
||||
pid: None,
|
||||
hostname: None,
|
||||
message: sec.to_string(),
|
||||
cursor: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn plan_repeats_priority_in_each_unit_disjunct() {
|
||||
let q = JournalQuery {
|
||||
unit: Some("a.service".into()),
|
||||
priority: Some(Priority::Crit),
|
||||
..Default::default()
|
||||
};
|
||||
let plan = match_plan(&q);
|
||||
assert_eq!(plan.len(), 2);
|
||||
assert_eq!(plan[0][0], ("_SYSTEMD_UNIT".into(), "a.service".into()));
|
||||
assert_eq!(plan[1][0], ("UNIT".into(), "a.service".into()));
|
||||
for g in &plan {
|
||||
let p: Vec<_> = g.iter().filter(|(k, _)| k == "PRIORITY").collect();
|
||||
assert_eq!(p.len(), 3); // 0,1,2
|
||||
assert_eq!(p[2].1, "2");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn plan_without_unit_or_priority() {
|
||||
assert!(match_plan(&JournalQuery::default()).is_empty());
|
||||
let q = JournalQuery {
|
||||
priority: Some(Priority::Emerg),
|
||||
..Default::default()
|
||||
};
|
||||
assert_eq!(match_plan(&q), vec![vec![("PRIORITY".into(), "0".into())]]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn backlog_is_oldest_first_capped_and_since_filtered() {
|
||||
let mut src = vec![entry(5), entry(4), entry(3), entry(2), entry(1)].into_iter();
|
||||
let out = collect_backlog(|| Ok(src.next()), None, 3).unwrap();
|
||||
let t: Vec<_> = out.iter().map(|e| e.message.as_str()).collect();
|
||||
assert_eq!(t, ["3", "4", "5"]);
|
||||
|
||||
let mut src = vec![entry(5), entry(4), entry(3)].into_iter();
|
||||
let since = Timestamp::from_second(4).unwrap();
|
||||
let out = collect_backlog(|| Ok(src.next()), Some(since), 10).unwrap();
|
||||
assert_eq!(out.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn backlog_skips_unreadable_timestamps() {
|
||||
let mut bad = entry(0);
|
||||
bad.timestamp = Timestamp::UNIX_EPOCH;
|
||||
let mut src = vec![entry(3), bad, entry(1)].into_iter();
|
||||
let since = Timestamp::from_second(1).unwrap();
|
||||
let out = collect_backlog(|| Ok(src.next()), Some(since), 10).unwrap();
|
||||
assert_eq!(out.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn send_stops_when_receiver_dropped() {
|
||||
let (tx, rx) = mpsc::channel(1);
|
||||
let token = CancellationToken::new();
|
||||
assert!(send(&tx, &token, 1));
|
||||
let t = std::thread::spawn({
|
||||
let tx = tx.clone();
|
||||
let token = token.clone();
|
||||
move || send(&tx, &token, 2) // full: blocks until rx dropped
|
||||
});
|
||||
std::thread::sleep(Duration::from_millis(150));
|
||||
drop(rx);
|
||||
assert!(!t.join().unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn send_stops_on_cancel_while_full() {
|
||||
let (tx, _rx) = mpsc::channel(1);
|
||||
let token = CancellationToken::new();
|
||||
assert!(send(&tx, &token, 1));
|
||||
let t = std::thread::spawn({
|
||||
let token = token.clone();
|
||||
move || send(&tx, &token, 2)
|
||||
});
|
||||
std::thread::sleep(Duration::from_millis(150));
|
||||
token.cancel();
|
||||
assert!(!t.join().unwrap());
|
||||
}
|
||||
|
||||
// Live-journal tests: `cargo test -- --ignored live_`.
|
||||
#[tokio::test]
|
||||
async fn follow_thread_exits_when_receiver_dropped() {
|
||||
#[ignore = "needs a readable journal: cargo test -- --ignored live_"]
|
||||
async fn live_follow_thread_exits_when_receiver_dropped() {
|
||||
let src = SdJournal::new(&Config::default(), CancellationToken::new());
|
||||
let q = JournalQuery {
|
||||
lines: Some(1),
|
||||
..Default::default()
|
||||
};
|
||||
let (rx, handle) = match src.spawn_follow(&q).await {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
eprintln!("skipping: journal unavailable: {e}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let (rx, handle) = src.spawn_follow(&q).await.expect("journal must open");
|
||||
drop(rx);
|
||||
tokio::time::timeout(Duration::from_millis(1500), handle)
|
||||
.await
|
||||
@@ -267,12 +397,14 @@ mod tests {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn follow_thread_exits_on_shutdown() {
|
||||
#[ignore = "needs a readable journal: cargo test -- --ignored live_"]
|
||||
async fn live_follow_thread_exits_on_shutdown() {
|
||||
let token = CancellationToken::new();
|
||||
let src = SdJournal::new(&Config::default(), token.clone());
|
||||
let Ok((_rx, handle)) = src.spawn_follow(&JournalQuery::default()).await else {
|
||||
return;
|
||||
};
|
||||
let (_rx, handle) = src
|
||||
.spawn_follow(&JournalQuery::default())
|
||||
.await
|
||||
.expect("journal must open");
|
||||
token.cancel();
|
||||
tokio::time::timeout(Duration::from_millis(1500), handle)
|
||||
.await
|
||||
|
||||
Reference in new issue
Block a user