about summary refs log tree commit diff
path: root/library/test/src/helpers/concurrency.rs
blob: 6648b669125f7179b3d7485e842e07bbe922dfec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//! Helper module which helps to determine amount of threads to be used
//! during tests execution.

use std::num::NonZero;
use std::{env, thread};

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