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
@@ -46,3 +46,4 @@ Tick these in [`TODO.md`](../../TODO.md).
|
|||||||
|
|
||||||
- 2026-10-15: Card created.
|
- 2026-10-15: Card created.
|
||||||
- 2026-10-15: Implemented B1-B6 on branch ws-b/journal-source. Query and follow verified on the live host. Unit filter matches `_SYSTEMD_UNIT` only; `UNIT` is used only as a fallback when mapping entries. Follow applies backpressure (no drops). `since` is applied during the backward backlog scan.
|
- 2026-10-15: Implemented B1-B6 on branch ws-b/journal-source. Query and follow verified on the live host. Unit filter matches `_SYSTEMD_UNIT` only; `UNIT` is used only as a fallback when mapping entries. Follow applies backpressure (no drops). `since` is applied during the backward backlog scan.
|
||||||
|
- 2026-10-15: Review fixes: unit filter is now (_SYSTEMD_UNIT=u OR UNIT=u), with PRIORITY matches repeated per disjunct. Verified against `journalctl -u hyprland-headless.service` style PID1 lines (same 5 entries, same timestamps). Offline unit tests cover match plan, ordering, `since`, and send exit on drop/cancel. Live follow tests are #[ignore]: `cargo test -- --ignored live_`.
|
||||||
+165
-33
@@ -88,20 +88,44 @@ fn open_filtered(ns: Option<&str>, q: &JournalQuery) -> Result<Journal, AppError
|
|||||||
None => opts.open(),
|
None => opts.open(),
|
||||||
}
|
}
|
||||||
.map_err(unavailable)?;
|
.map_err(unavailable)?;
|
||||||
if let Some(unit) = &q.unit {
|
let plan = match_plan(q);
|
||||||
j.match_add("_SYSTEMD_UNIT", unit.as_str())
|
for (i, group) in plan.iter().enumerate() {
|
||||||
.map_err(unavailable)?;
|
if i > 0 {
|
||||||
}
|
j.match_or().map_err(unavailable)?;
|
||||||
if let Some(p) = q.priority {
|
}
|
||||||
// Same-field matches are OR'ed: PRIORITY in 0..=p.
|
for (k, v) in group {
|
||||||
for k in 0..=p.as_u8() {
|
j.match_add(k, v.as_str()).map_err(unavailable)?;
|
||||||
j.match_add("PRIORITY", k.to_string())
|
|
||||||
.map_err(unavailable)?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(j)
|
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 {
|
fn to_entry(j: &mut Journal, rec: JournalRecord) -> JournalEntry {
|
||||||
let ts = j
|
let ts = j
|
||||||
.timestamp_usec()
|
.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.
|
/// Collect up to `lines` entries from a newest-first source, honoring
|
||||||
fn read_backlog(
|
/// `since`, and return them oldest first. Entries with an unreadable
|
||||||
j: &mut Journal,
|
/// timestamp (epoch fallback) are skipped rather than ending the scan.
|
||||||
q: &JournalQuery,
|
fn collect_backlog(
|
||||||
|
mut prev: impl FnMut() -> Result<Option<JournalEntry>, AppError>,
|
||||||
|
since: Option<Timestamp>,
|
||||||
lines: usize,
|
lines: usize,
|
||||||
) -> Result<Vec<JournalEntry>, AppError> {
|
) -> Result<Vec<JournalEntry>, AppError> {
|
||||||
j.seek_tail().map_err(unavailable)?;
|
|
||||||
let mut out = Vec::new();
|
let mut out = Vec::new();
|
||||||
while out.len() < lines {
|
while out.len() < lines {
|
||||||
let Some(rec) = j.previous_entry().map_err(unavailable)? else {
|
let Some(e) = prev()? else { break };
|
||||||
break;
|
if e.timestamp == Timestamp::UNIX_EPOCH {
|
||||||
};
|
continue;
|
||||||
let e = to_entry(j, rec);
|
}
|
||||||
if q.since.is_some_and(|s| e.timestamp < s) {
|
if since.is_some_and(|s| e.timestamp < s) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
out.push(e);
|
out.push(e);
|
||||||
@@ -150,8 +175,25 @@ fn read_backlog(
|
|||||||
Ok(out)
|
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.
|
/// 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 {
|
loop {
|
||||||
if token.is_cancelled() {
|
if token.is_cancelled() {
|
||||||
return false;
|
return false;
|
||||||
@@ -244,21 +286,109 @@ impl JournalSource for SdJournal {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
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]
|
#[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 src = SdJournal::new(&Config::default(), CancellationToken::new());
|
||||||
let q = JournalQuery {
|
let q = JournalQuery {
|
||||||
lines: Some(1),
|
lines: Some(1),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
let (rx, handle) = match src.spawn_follow(&q).await {
|
let (rx, handle) = src.spawn_follow(&q).await.expect("journal must open");
|
||||||
Ok(v) => v,
|
|
||||||
Err(e) => {
|
|
||||||
eprintln!("skipping: journal unavailable: {e}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
drop(rx);
|
drop(rx);
|
||||||
tokio::time::timeout(Duration::from_millis(1500), handle)
|
tokio::time::timeout(Duration::from_millis(1500), handle)
|
||||||
.await
|
.await
|
||||||
@@ -267,12 +397,14 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[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 token = CancellationToken::new();
|
||||||
let src = SdJournal::new(&Config::default(), token.clone());
|
let src = SdJournal::new(&Config::default(), token.clone());
|
||||||
let Ok((_rx, handle)) = src.spawn_follow(&JournalQuery::default()).await else {
|
let (_rx, handle) = src
|
||||||
return;
|
.spawn_follow(&JournalQuery::default())
|
||||||
};
|
.await
|
||||||
|
.expect("journal must open");
|
||||||
token.cancel();
|
token.cancel();
|
||||||
tokio::time::timeout(Duration::from_millis(1500), handle)
|
tokio::time::timeout(Duration::from_millis(1500), handle)
|
||||||
.await
|
.await
|
||||||
|
|||||||
Reference in new issue
Block a user