Skip to content

Commit

Permalink
use filter for iter_blocking test
Browse files Browse the repository at this point in the history
The iter_blocking test is now blocking, and should
be killed after 60 seconds, which would have to be
counted as failure.

Signed-off-by: Finn Behrens <[email protected]>
  • Loading branch information
kloenk committed Jul 28, 2021
1 parent ce645fb commit 192fd85
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
1 change: 1 addition & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: test
timeout-minutes: 6

fmt:
name: Rustfmt
Expand Down
67 changes: 33 additions & 34 deletions tests/journal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extern crate journald;
use std::time::{SystemTime, UNIX_EPOCH};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use journald::reader::*;
use journald::JournalEntry;
Expand All @@ -11,6 +11,7 @@ const FILTER_FIELD: &str = "RUST_JOURNALD_TEST";
#[test]
fn test_reverse_walk() {
let filter: String = format!("test_reverse_walk_{}", rand::random::<u64>());
println!("random filter: {}={}", FILTER_FIELD, filter);

let now_usec: i64 = SystemTime::now()
.duration_since(UNIX_EPOCH)
Expand Down Expand Up @@ -66,6 +67,9 @@ fn test_reverse_walk() {

#[test]
fn iter_blocking() {
let filter: String = format!("test_iter_blocking_{}", rand::random::<u64>());
println!("random filter: {}={}", FILTER_FIELD, filter);

let now_usec: i64 = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
Expand All @@ -77,55 +81,50 @@ fn iter_blocking() {
"iter: rust-systemd test 3",
];

let mut journal =
JournalReader::open(&JournalReaderConfig::default()).expect("journal open failed");

std::thread::sleep(std::time::Duration::from_micros(1000));

// we want a forward walk
journal
.seek(JournalSeek::Tail)
.expect("journal seek failed");
// give systemd internals some time
std::thread::sleep(std::time::Duration::from_secs(1));

for message in &messages_expected {
let mut entry = JournalEntry::new();
entry.set_message(message);
entry
.fields
.insert(FILTER_FIELD.to_string(), filter.to_string());
journald::writer::submit(&entry).expect("journald write failed");
}

// give systemd internals some time
std::thread::sleep(std::time::Duration::from_micros(1000));
let mut journal =
JournalReader::open(&JournalReaderConfig::default()).expect("journal open failed");

journal
.add_filter(&format!("{}={}", FILTER_FIELD, filter))
.expect("Could not set journald filter");

let iter = journal.as_blocking_iter();
// we want a forward walk, there for we have to seek before writing messages
journal
.seek(JournalSeek::Head)
.expect("journal seek failed");

let mut j = 0;
let mut i = messages_expected.len();
let mut iter = journal.as_blocking_iter();
iter.set_timeout(Duration::from_secs(1))
.expect("Set iter timeout");

let mut i = 0;
for entry in iter {
let entry = entry.expect("failed to iterate");

let entry_message = entry.get_message().unwrap().to_string();
if !entry_message.starts_with("iter: ") {
j += 1;
// other logs should not create as many logs
assert!(j < 20);
continue;
}

let entry_time = entry.get_wallclock_time().unwrap().timestamp_us / 1000000;
println!("entry: {:?}", entry);

println!("{}: {}", entry_message, entry_time);

assert_eq!(
entry_message,
messages_expected[messages_expected.len() - i]
);
let entry_message = entry.get_message().unwrap().to_string();
assert_eq!(entry_message, messages_expected[i]);

let entry_time = entry.get_wallclock_time().unwrap().timestamp_us / 1000000;
assert!((entry_time - now_usec).abs() < TIME_EPSILON_SECONDS);

i -= 1;
if i == 0 {
return;
}
i += 1;
}

if i != messages_expected.len() {
panic!("Did not receive right amount of systemd iter messages");
}
}

0 comments on commit 192fd85

Please sign in to comment.