From f41b363ea315509040a20e59acae97955dc8e601 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Tue, 6 Sep 2016 20:16:56 +0000 Subject: [PATCH] Update libtest for single-threaded emscripten support --- src/libtest/lib.rs | 71 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 17 deletions(-) diff --git a/src/libtest/lib.rs b/src/libtest/lib.rs index 12dd8e615e8..49a7f6589d2 100644 --- a/src/libtest/lib.rs +++ b/src/libtest/lib.rs @@ -1182,26 +1182,63 @@ fn flush(&mut self) -> io::Result<()> { } } - thread::spawn(move || { - let data = Arc::new(Mutex::new(Vec::new())); - let data2 = data.clone(); - let cfg = thread::Builder::new().name(match desc.name { - DynTestName(ref name) => name.clone(), - StaticTestName(name) => name.to_owned(), - }); + // If the platform is single-threaded we're just going to run + // the test synchronously, regardless of the concurrency + // level. + let supports_threads = !cfg!(target_os = "emscripten"); - let result_guard = cfg.spawn(move || { - if !nocapture { - io::set_print(box Sink(data2.clone())); - io::set_panic(box Sink(data2)); - } - testfn() - }) - .unwrap(); - let test_result = calc_result(&desc, result_guard.join()); + // Buffer for capturing standard I/O + let data = Arc::new(Mutex::new(Vec::new())); + let data2 = data.clone(); + + if supports_threads { + thread::spawn(move || { + let cfg = thread::Builder::new().name(match desc.name { + DynTestName(ref name) => name.clone(), + StaticTestName(name) => name.to_owned(), + }); + + let result_guard = cfg.spawn(move || { + if !nocapture { + io::set_print(box Sink(data2.clone())); + io::set_panic(box Sink(data2)); + } + testfn() + }) + .unwrap(); + let test_result = calc_result(&desc, result_guard.join()); + let stdout = data.lock().unwrap().to_vec(); + monitor_ch.send((desc.clone(), test_result, stdout)).unwrap(); + }); + } else { + let oldio = if !nocapture { + Some(( + io::set_print(box Sink(data2.clone())), + io::set_panic(box Sink(data2)) + )) + } else { + None + }; + + use std::panic::{catch_unwind, AssertUnwindSafe}; + + let result = catch_unwind(AssertUnwindSafe(|| { + testfn() + })); + + if let Some((printio, panicio)) = oldio { + if let Some(printio) = printio { + io::set_print(printio); + } + if let Some(panicio) = panicio { + io::set_panic(panicio); + } + }; + + let test_result = calc_result(&desc, result); let stdout = data.lock().unwrap().to_vec(); monitor_ch.send((desc.clone(), test_result, stdout)).unwrap(); - }); + } } match testfn {