Skip to content

Commit

Permalink
test: fix flaky "run_watch" test (denoland#8519)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju authored Nov 27, 2020
1 parent 228ecb0 commit 40bf26b
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,17 @@ fn info_with_compiled_source() {
assert_eq!(output.stderr, b"");
}

// Helper function to skip watcher output that doesn't contain
// "Process finished" phrase.
fn wait_for_process_finished(stderr_lines: &mut impl Iterator<Item = String>) {
loop {
let msg = stderr_lines.next().unwrap();
if msg.contains("Process finished") {
break;
}
}
}

#[test]
fn run_watch() {
let t = TempDir::new().expect("tempdir fail");
Expand Down Expand Up @@ -1355,7 +1366,7 @@ fn run_watch() {
std::io::BufReader::new(stderr).lines().map(|r| r.unwrap());

assert!(stdout_lines.next().unwrap().contains("Hello world"));
assert!(stderr_lines.next().unwrap().contains("Process finished"));
wait_for_process_finished(&mut stderr_lines);

// TODO(lucacasonato): remove this timeout. It seems to be needed on Linux.
std::thread::sleep(std::time::Duration::from_secs(1));
Expand All @@ -1368,7 +1379,7 @@ fn run_watch() {

assert!(stderr_lines.next().unwrap().contains("Restarting"));
assert!(stdout_lines.next().unwrap().contains("Hello world2"));
assert!(stderr_lines.next().unwrap().contains("Process finished"));
wait_for_process_finished(&mut stderr_lines);

// Add dependency
let another_file = t.path().join("another_file.js");
Expand All @@ -1382,23 +1393,23 @@ fn run_watch() {
std::thread::sleep(std::time::Duration::from_secs(1));
assert!(stderr_lines.next().unwrap().contains("Restarting"));
assert!(stdout_lines.next().unwrap().contains('0'));
assert!(stderr_lines.next().unwrap().contains("Process finished"));
wait_for_process_finished(&mut stderr_lines);

// Confirm that restarting occurs when a new file is updated
std::fs::write(&another_file, "export const foo = 42;")
.expect("error writing file");
std::thread::sleep(std::time::Duration::from_secs(1));
assert!(stderr_lines.next().unwrap().contains("Restarting"));
assert!(stdout_lines.next().unwrap().contains("42"));
assert!(stderr_lines.next().unwrap().contains("Process finished"));
wait_for_process_finished(&mut stderr_lines);

// Confirm that the watcher keeps on working even if the file is updated and has invalid syntax
std::fs::write(&file_to_watch, "syntax error ^^")
.expect("error writing file");
std::thread::sleep(std::time::Duration::from_secs(1));
assert!(stderr_lines.next().unwrap().contains("Restarting"));
assert!(stderr_lines.next().unwrap().contains("error:"));
assert!(stderr_lines.next().unwrap().contains("Process finished"));
wait_for_process_finished(&mut stderr_lines);

// Then restore the file
std::fs::write(
Expand All @@ -1409,7 +1420,7 @@ fn run_watch() {
std::thread::sleep(std::time::Duration::from_secs(1));
assert!(stderr_lines.next().unwrap().contains("Restarting"));
assert!(stdout_lines.next().unwrap().contains("42"));
assert!(stderr_lines.next().unwrap().contains("Process finished"));
wait_for_process_finished(&mut stderr_lines);

child.kill().unwrap();
drop(t);
Expand Down

0 comments on commit 40bf26b

Please sign in to comment.