1
2
3
4
5
6
7
8
9
10
11
12
13
14
//! 帮助程序模块,它有助于确定测试执行期间要使用的线程数量。
//!
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."),
        }
    } else {
        thread::available_parallelism().map(|n| n.get()).unwrap_or(1)
    }
}