Skip to main content

adze_concurrency_init_core/
lib.rs

1//! Rayon global thread-pool initialization utilities for process-wide concurrency caps.
2
3#![forbid(unsafe_op_in_unsafe_fn)]
4#![deny(missing_docs)]
5#![cfg_attr(feature = "strict_api", deny(unreachable_pub))]
6#![cfg_attr(not(feature = "strict_api"), warn(unreachable_pub))]
7#![cfg_attr(feature = "strict_docs", deny(missing_docs))]
8#![cfg_attr(not(feature = "strict_docs"), allow(missing_docs))]
9
10pub use adze_concurrency_env_core::{
11    ConcurrencyCaps, DEFAULT_RAYON_NUM_THREADS, DEFAULT_TOKIO_WORKER_THREADS,
12    RAYON_NUM_THREADS_ENV, TOKIO_WORKER_THREADS_ENV, current_caps, parse_positive_usize_or_default,
13};
14pub use adze_concurrency_init_bootstrap_core::init_concurrency_caps;
15pub use adze_concurrency_init_rayon_core::{init_rayon_global_once, is_already_initialized_error};
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20
21    #[test]
22    fn init_is_idempotent() {
23        init_concurrency_caps();
24        init_concurrency_caps();
25    }
26
27    #[test]
28    fn low_level_rayon_init_is_idempotent() {
29        assert!(init_rayon_global_once(1).is_ok());
30        assert!(init_rayon_global_once(8).is_ok());
31    }
32
33    #[test]
34    fn already_initialized_error_classifier_is_case_insensitive() {
35        assert!(is_already_initialized_error(
36            "The GlObAl thread pool has AlReAdY been initialized"
37        ));
38    }
39
40    #[test]
41    fn already_initialized_error_classifier_requires_both_tokens() {
42        assert!(!is_already_initialized_error(
43            "thread pool already initialized"
44        ));
45        assert!(!is_already_initialized_error(
46            "global thread pool initialized"
47        ));
48    }
49
50    #[test]
51    fn default_caps_have_expected_constants() {
52        assert_eq!(DEFAULT_RAYON_NUM_THREADS, 4);
53        assert_eq!(DEFAULT_TOKIO_WORKER_THREADS, 2);
54    }
55
56    #[test]
57    fn current_caps_returns_valid_values() {
58        let caps = current_caps();
59        assert!(caps.rayon_threads >= 1 || caps.rayon_threads == DEFAULT_RAYON_NUM_THREADS);
60        assert!(
61            caps.tokio_worker_threads >= 1
62                || caps.tokio_worker_threads == DEFAULT_TOKIO_WORKER_THREADS
63        );
64    }
65
66    #[test]
67    fn parse_positive_usize_or_default_returns_default_for_none() {
68        assert_eq!(parse_positive_usize_or_default(None, 7), 7);
69    }
70
71    #[test]
72    fn parse_positive_usize_or_default_returns_default_for_zero() {
73        assert_eq!(parse_positive_usize_or_default(Some("0"), 5), 5);
74    }
75
76    #[test]
77    fn parse_positive_usize_or_default_parses_valid_value() {
78        assert_eq!(parse_positive_usize_or_default(Some("42"), 1), 42);
79    }
80
81    #[test]
82    fn env_var_constants_match_expected_names() {
83        assert_eq!(RAYON_NUM_THREADS_ENV, "RAYON_NUM_THREADS");
84        assert_eq!(TOKIO_WORKER_THREADS_ENV, "TOKIO_WORKER_THREADS");
85    }
86
87    #[test]
88    fn concurrency_caps_default_matches_constants() {
89        let caps = ConcurrencyCaps::default();
90        assert_eq!(caps.rayon_threads, DEFAULT_RAYON_NUM_THREADS);
91        assert_eq!(caps.tokio_worker_threads, DEFAULT_TOKIO_WORKER_THREADS);
92    }
93}