Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable thread support in libtest if RUST_TEST_THREADS=1 #107396

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions library/test/src/helpers/concurrency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,30 @@
use std::{env, num::NonZeroUsize, thread};

pub fn get_concurrency() -> usize {
if let Ok(value) = env::var("RUST_TEST_THREADS") {
match value.parse::<NonZeroUsize>().ok() {
Some(n) => n.get(),
_ => panic!("RUST_TEST_THREADS is `{value}`, should be a positive integer."),
}
rust_test_threads_from_env()
.unwrap_or_else(|| thread::available_parallelism().map(|n| n.get()).unwrap_or(1))
}

pub fn supports_threads() -> bool {
if cfg!(target_os = "emscripten") || cfg!(target_family = "wasm") {
return false;
}

// Accommodate libraries that may rely on shared thread-local storage (e.g.
// integrating with old C libraries).
if let Some(1) = rust_test_threads_from_env() {
return false;
}

true
}

fn rust_test_threads_from_env() -> Option<usize> {
let value = env::var("RUST_TEST_THREADS").ok()?;

if let Ok(value) = value.parse::<NonZeroUsize>() {
Some(value.get())
} else {
thread::available_parallelism().map(|n| n.get()).unwrap_or(1)
panic!("RUST_TEST_THREADS is `{value}`, should be a positive integer.", value = value)
}
}
5 changes: 2 additions & 3 deletions library/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ mod tests;

use core::any::Any;
use event::{CompletedTest, TestEvent};
use helpers::concurrency::get_concurrency;
use helpers::concurrency::{get_concurrency, supports_threads};
use helpers::exit_code::get_exit_code;
use helpers::shuffle::{get_shuffle_seed, shuffle_tests};
use options::RunStrategy;
Expand Down Expand Up @@ -592,8 +592,7 @@ pub fn run_test(
// 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") && !cfg!(target_family = "wasm");
if supports_threads {
if supports_threads() {
let cfg = thread::Builder::new().name(name.as_slice().to_owned());
let mut runtest = Arc::new(Mutex::new(Some(runtest)));
let runtest2 = runtest.clone();
Expand Down