From 472447c73642fa429c89971f2c0348a317fb54db Mon Sep 17 00:00:00 2001 From: Camelid Date: Tue, 20 Oct 2020 14:12:54 -0700 Subject: [PATCH 1/4] libtest: Don't panic if unable to spawn thread Instead just run the test synchronously. --- library/test/src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/library/test/src/lib.rs b/library/test/src/lib.rs index 9c5bb8957b548..f7fffc55c394d 100644 --- a/library/test/src/lib.rs +++ b/library/test/src/lib.rs @@ -470,7 +470,10 @@ pub fn run_test( let supports_threads = !cfg!(target_os = "emscripten") && !cfg!(target_arch = "wasm32"); if concurrency == Concurrent::Yes && supports_threads { let cfg = thread::Builder::new().name(name.as_slice().to_owned()); - cfg.spawn(runtest).unwrap(); + if cfg.spawn(runtest).is_err() { + // If we can't spawn a new thread, just run the test synchronously. + runtest(); + } } else { runtest(); } From 77b68c5d8b6995d2fd62f05e88dba6aeb8029957 Mon Sep 17 00:00:00 2001 From: Camelid Date: Tue, 20 Oct 2020 15:33:18 -0700 Subject: [PATCH 2/4] Panic if error was not `WouldBlock` --- library/test/src/lib.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/library/test/src/lib.rs b/library/test/src/lib.rs index f7fffc55c394d..238ad42c97d56 100644 --- a/library/test/src/lib.rs +++ b/library/test/src/lib.rs @@ -470,9 +470,16 @@ pub fn run_test( let supports_threads = !cfg!(target_os = "emscripten") && !cfg!(target_arch = "wasm32"); if concurrency == Concurrent::Yes && supports_threads { let cfg = thread::Builder::new().name(name.as_slice().to_owned()); - if cfg.spawn(runtest).is_err() { - // If we can't spawn a new thread, just run the test synchronously. - runtest(); + match cfg.spawn(runtest) { + Ok(_) => {} + Err(err @ io::Error { .. }) if err.kind() == io::ErrorKind::WouldBlock => { + // If spawning a new thread would block, and blocking was + // requested to not occur, just run the test synchronously. + runtest(); + } + Err(err) => { + panic!("spawning a test thread failed: {}", err); + } } } else { runtest(); From 837b22858cd1e1e1fba9a756bac023b186282a9f Mon Sep 17 00:00:00 2001 From: Camelid Date: Tue, 20 Oct 2020 15:55:56 -0700 Subject: [PATCH 3/4] Update comment Co-authored-by: Joshua Nelson --- library/test/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/library/test/src/lib.rs b/library/test/src/lib.rs index 238ad42c97d56..cce28646f9b53 100644 --- a/library/test/src/lib.rs +++ b/library/test/src/lib.rs @@ -473,8 +473,7 @@ pub fn run_test( match cfg.spawn(runtest) { Ok(_) => {} Err(err @ io::Error { .. }) if err.kind() == io::ErrorKind::WouldBlock => { - // If spawning a new thread would block, and blocking was - // requested to not occur, just run the test synchronously. + // If we're at the thread limit, just run all tests in a single thread. runtest(); } Err(err) => { From 53e7072c6569c621a626692dc577e076006c9ecb Mon Sep 17 00:00:00 2001 From: Camelid Date: Tue, 20 Oct 2020 16:00:08 -0700 Subject: [PATCH 4/4] Use `if let` instead of `match` --- library/test/src/lib.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/library/test/src/lib.rs b/library/test/src/lib.rs index cce28646f9b53..6570cadef8498 100644 --- a/library/test/src/lib.rs +++ b/library/test/src/lib.rs @@ -470,13 +470,11 @@ pub fn run_test( let supports_threads = !cfg!(target_os = "emscripten") && !cfg!(target_arch = "wasm32"); if concurrency == Concurrent::Yes && supports_threads { let cfg = thread::Builder::new().name(name.as_slice().to_owned()); - match cfg.spawn(runtest) { - Ok(_) => {} - Err(err @ io::Error { .. }) if err.kind() == io::ErrorKind::WouldBlock => { + if let Err(err) = cfg.spawn(runtest) { + if err.kind() == io::ErrorKind::WouldBlock { // If we're at the thread limit, just run all tests in a single thread. runtest(); - } - Err(err) => { + } else { panic!("spawning a test thread failed: {}", err); } }