Skip to content

Commit

Permalink
refact: removes sleep in the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianoliveira committed Jun 30, 2024
1 parent 499b996 commit 46c71e2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 26 deletions.
17 changes: 9 additions & 8 deletions tests/common/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{
};

pub struct Options {
pub log_file: &'static str,
pub output_file: &'static str,
pub example_file: &'static str,
}

Expand All @@ -22,24 +22,25 @@ where

// check if the file exists if so fail
assert!(
!std::path::Path::new(&dir.join(opts.log_file)).exists(),
!std::path::Path::new(&dir.join(opts.output_file)).exists(),
"the log file already exists, make sure to give an unique log file to avoid multiple writes to same file: {}",
dir.join(opts.log_file).display()
dir.join(opts.output_file).display()
);

defer!({
std::fs::remove_file(dir.join(opts.log_file)).expect("failed to remove test output file")
std::fs::remove_file(dir.join(opts.output_file)).expect("failed to remove test output file")
});

let bin_path = dir.join("target/debug/fzz");
let _ = std::fs::remove_file(dir.join(opts.log_file));
let output_log = File::create(dir.join(opts.log_file)).expect("error log file");
let output_file = File::create(dir.join(opts.output_file)).expect("error log file");

handler(
Command::new(bin_path)
.arg("-c")
.arg(dir.join(opts.example_file))
.stdout(Stdio::from(output_log)),
File::open(dir.join(opts.log_file)).expect("failed to open file"),
.stdout(Stdio::from(output_file)),
File::open(dir.join(opts.output_file)).expect("failed to open file"),
);

std::fs::remove_file(dir.join(opts.output_file)).expect("failed to remove file after running test");
}
7 changes: 4 additions & 3 deletions tests/common/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ macro_rules! wait_until {
if result {
break;
}
sleep(Duration::from_millis(100));
std::thread::sleep(std::time::Duration::from_millis(100));
}

assert!($e, $($arg)+);
Expand All @@ -38,15 +38,16 @@ macro_rules! wait_until {
if result {
break;
}
sleep(Duration::from_millis(100));

std::thread::sleep(std::time::Duration::from_millis(100));
}
};
}

#[macro_export]
macro_rules! write_to_file {
($file:expr) => {
let mut file = File::create($file).expect("failed to open file");
let mut file = std::fs::File::create($file).expect("failed to open file");
file.write_all(b"test_content\n")
.expect("failed to write to file");
};
Expand Down
8 changes: 4 additions & 4 deletions tests/watcher_does_not_die_with_failing_tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ fn test_it_watches_a_list_of_tasks_and_do_not_panic() {
setup::with_example(
setup::Options {
example_file: "examples/list-of-watches.yml",
log_file: "test_it_watches_a_list_of_tasks_and_do_not_panic.log",
output_file: "test_it_watches_a_list_of_tasks_and_do_not_panic.log",
},
|fzz_cmd, mut output_log| {
|fzz_cmd, mut output_file| {
let mut output = String::new();
let mut child = fzz_cmd.spawn().expect("failed to spawn sub process");
defer!({
child.kill().expect("failed to kill sub process");
});

wait_until!({
output_log
output_file
.read_to_string(&mut output)
.expect("failed to read test output file");

Expand All @@ -31,7 +31,7 @@ fn test_it_watches_a_list_of_tasks_and_do_not_panic() {
write_to_file!("examples/workdir/trigger-watcher.txt");

wait_until!({
output_log
output_file
.read_to_string(&mut output)
.expect("failed to read test output file");

Expand Down
14 changes: 3 additions & 11 deletions tests/watching_configured_rules.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use std::io::prelude::*;
use std::{
fs::File,
thread::sleep,
time::Duration,
};
use std::io::prelude::*;

#[path = "./common/lib.rs"]
mod setup;
Expand All @@ -12,7 +7,7 @@ mod setup;
fn test_it_is_not_triggered_by_ignored_files() {
setup::with_example(
setup::Options {
log_file: "test_it_is_not_triggered_by_ignored_files.log",
output_file: "test_it_is_not_triggered_by_ignored_files.log",
example_file: "examples/simple-case.yml",
},
|fzz_cmd, mut log| {
Expand Down Expand Up @@ -44,10 +39,7 @@ fn test_it_is_not_triggered_by_ignored_files() {

write_to_file!("examples/workdir/ignored/modifyme.txt");

sleep(Duration::from_secs(2));

wait_until!({
sleep(Duration::from_millis(100));
log.read_to_string(&mut output)
.expect("failed to read from file");

Expand Down Expand Up @@ -77,7 +69,7 @@ fn test_it_watch_files_and_execute_configured_commands() {
setup::with_example(
setup::Options {
example_file: "examples/simple-case.yml",
log_file: "test_it_watch_files_and_execute_configured_commands.log",
output_file: "test_it_watch_files_and_execute_configured_commands.log",
},
|fzz_cmd, mut logger| {
let mut child = fzz_cmd.spawn().expect("failed to spawn process");
Expand Down

0 comments on commit 46c71e2

Please sign in to comment.