Add a dedicated thread for output printing

This commit is contained in:
Oli Scherer 2022-07-18 09:02:40 +00:00
parent 084e02fd08
commit 30931eeecb

View File

@ -123,11 +123,22 @@ pub fn run_tests(mut config: Config) -> Result<()> {
drop(submit); drop(submit);
}); });
// A channel for the messages emitted by the individual test threads.
let (finish_file, finished_files) = crossbeam::channel::unbounded();
s.spawn(|_| {
for msg in finished_files {
eprintln!("{msg}");
}
});
let mut threads = vec![]; let mut threads = vec![];
// Create N worker threads that receive files to test. // Create N worker threads that receive files to test.
for _ in 0..std::thread::available_parallelism().unwrap().get() { for _ in 0..std::thread::available_parallelism().unwrap().get() {
let finish_file = finish_file.clone();
threads.push(s.spawn(|_| -> Result<()> { threads.push(s.spawn(|_| -> Result<()> {
let finish_file = finish_file;
for path in &receive { for path in &receive {
if !config.path_filter.is_empty() { if !config.path_filter.is_empty() {
let path_display = path.display().to_string(); let path_display = path.display().to_string();
@ -140,11 +151,12 @@ pub fn run_tests(mut config: Config) -> Result<()> {
// Ignore file if only/ignore rules do (not) apply // Ignore file if only/ignore rules do (not) apply
if !test_file_conditions(&comments, &target, &config) { if !test_file_conditions(&comments, &target, &config) {
ignored.fetch_add(1, Ordering::Relaxed); ignored.fetch_add(1, Ordering::Relaxed);
eprintln!( let msg = format!(
"{} ... {}", "{} ... {}",
path.display(), path.display(),
"ignored (in-test comment)".yellow() "ignored (in-test comment)".yellow()
); );
finish_file.send(msg)?;
continue; continue;
} }
// Run the test for all revisions // Run the test for all revisions
@ -161,10 +173,10 @@ pub fn run_tests(mut config: Config) -> Result<()> {
} }
write!(msg, "... ").unwrap(); write!(msg, "... ").unwrap();
if errors.is_empty() { if errors.is_empty() {
eprintln!("{msg}{}", "ok".green()); write!(msg, "{}", "ok".green()).unwrap();
succeeded.fetch_add(1, Ordering::Relaxed); succeeded.fetch_add(1, Ordering::Relaxed);
} else { } else {
eprintln!("{msg}{}", "FAILED".red().bold()); write!(msg, "{}", "FAILED".red().bold()).unwrap();
failures.lock().unwrap().push(( failures.lock().unwrap().push((
path.clone(), path.clone(),
m, m,
@ -173,11 +185,13 @@ pub fn run_tests(mut config: Config) -> Result<()> {
stderr, stderr,
)); ));
} }
finish_file.send(msg)?;
} }
} }
Ok(()) Ok(())
})); }));
} }
for thread in threads { for thread in threads {
thread.join().unwrap()?; thread.join().unwrap()?;
} }