Skip to content

Commit

Permalink
Minot test improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
emabee committed Oct 13, 2024
1 parent 1842c4f commit 85ab845
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 52 deletions.
14 changes: 6 additions & 8 deletions tests/test_multi_threaded_cleanup_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ mod d {
wait_for_workers_to_close(worker_handles);
Local::now()
};
let delta1 = end.signed_duration_since(start).num_milliseconds();
let delta2 = Local::now().signed_duration_since(end);
let delta1_ms = end.signed_duration_since(start).num_milliseconds();
let delta2_ms = Local::now().signed_duration_since(end).num_milliseconds();
println!(
"Task executed with {NO_OF_THREADS} threads in {delta1} ms, \
program added {delta2} ms to finish writing logs.",
"Task executed with {NO_OF_THREADS} threads in {delta1_ms} ms, \
program added {delta2_ms} ms to finish writing logs.",
);

verify_logs(&directory.display().to_string());
Expand Down Expand Up @@ -164,15 +164,13 @@ mod d {
// .add("_r[0-9][0-9]*.");
.add("_r*.");

let log_pattern = fn_pattern.clone().add("log");
let no_of_log_files = glob(&log_pattern)
let no_of_log_files = glob(&fn_pattern.clone().add("log"))
.unwrap()
.map(Result::unwrap)
.inspect(|p| inspect_file(p, &mut counters))
.count();

let gz_pattern = fn_pattern.add("gz");
let no_of_gz_files = glob(&gz_pattern)
let no_of_gz_files = glob(&fn_pattern.add("gz"))
.unwrap()
.map(Result::unwrap)
.inspect(|p| inspect_file(p, &mut counters))
Expand Down
9 changes: 2 additions & 7 deletions tests/test_multi_threaded_cleanup_use_utc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,14 @@ mod d {
.add(&basename)
.add("_r[0-9][0-9]*.");

let log_pattern = fn_pattern.clone().add("log");
println!("log_pattern = {log_pattern}");
let no_of_log_files = glob(&log_pattern)
let no_of_log_files = glob(&fn_pattern.clone().add("log"))
.unwrap()
.map(Result::unwrap)
.inspect(|p| println!("found: {p:?}"))
.count();

let gz_pattern = fn_pattern.add("gz");
let no_of_gz_files = glob(&gz_pattern)
let no_of_gz_files = glob(&fn_pattern.add("gz"))
.unwrap()
.map(Result::unwrap)
.inspect(|p| println!("found: {p:?}"))
.count();

assert_eq!(no_of_log_files, NO_OF_LOG_FILES);
Expand Down
78 changes: 42 additions & 36 deletions tests/test_restart_with_no_suffix.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
mod test_utils;

use std::{
fs::File,
io::{BufRead, BufReader},
};

use flexi_logger::{Cleanup, Criterion, FileSpec, LogfileSelector, Logger, Naming};
use log::*;

const COUNT: u8 = 2;

#[test]
fn test_write_modes() {
fn test_restart_with_no_suffix() {
if let Some(value) = test_utils::dispatch(COUNT) {
std::thread::sleep(std::time::Duration::from_millis(1000));
work(value)
Expand All @@ -21,10 +16,16 @@ fn test_write_modes() {
fn work(value: u8) {
let directory = test_utils::dir();
let file_spec = FileSpec::default()
.directory(directory)
.directory(directory.clone())
.o_suffix(match value {
0 => Some("log".to_string()),
1 => None,
0 => {
println!("With suffix log");
Some("log".to_string())
}
1 => {
println!("Without suffix");
None
}
COUNT..=u8::MAX => {
unreachable!("got unexpected value {}", value)
}
Expand All @@ -42,32 +43,37 @@ fn work(value: u8) {
std::thread::sleep(std::time::Duration::from_millis(10));
}

let mut contents = String::new();
// verify all log lines are found
assert_eq!(100, test_utils::count_log_lines(&directory));

assert_eq!(
100,
logger
.existing_log_files(&LogfileSelector::default().with_r_current())
.unwrap()
.into_iter()
.filter(|pb| {
let extension = pb.extension().map(|s| s.to_string_lossy().into_owned());
match value {
0 => Some("log".to_string()) == extension,
1 => extension.is_none() || extension.unwrap().starts_with("restart"),
COUNT..=u8::MAX => {
unreachable!("got unexpected value {}", value)
}
}
})
.map(|path| {
let mut buf_reader = BufReader::new(File::open(path).unwrap());
let mut line_count = 0;
while buf_reader.read_line(&mut contents).unwrap() > 0 {
line_count += 1;
}
line_count
})
.sum::<u32>()
);
// verify that no unexpected files are found
match value {
0 => assert_eq!(
0,
logger
.existing_log_files(&LogfileSelector::default())
.unwrap()
.into_iter()
.filter(
|pb| pb.extension().map(|oss| oss.to_string_lossy().to_string())
!= Some(String::from("log"))
)
.count()
),
1 => assert_eq!(
0,
logger
.existing_log_files(&LogfileSelector::default())
.unwrap()
.into_iter()
.filter(|pb| match pb.extension() {
Some(oss) => !oss.to_string_lossy().to_string().starts_with("restart"),
None => false,
})
.count()
),
COUNT..=u8::MAX => {
unreachable!("got unexpected value {}", value)
}
}
}
3 changes: 2 additions & 1 deletion tests/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ pub fn wait_for_end_of_second() {
}
}

// Count all log lines written in all .log and .log.gz files in the given folder
// Count all log lines written in all files in the given folder
// ".gz" files are decompressed first
pub fn count_log_lines(directory: &Path) -> usize {
// read all files
let pattern = directory.display().to_string().add("/*");
Expand Down

0 comments on commit 85ab845

Please sign in to comment.