Skip to main content

adze_concurrency_caps_contract_core/
lib.rs

1//! Contract-focused re-export layer for concurrency caps utilities.
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_core::{
15    init_concurrency_caps, init_rayon_global_once, is_already_initialized_error,
16};
17pub use adze_concurrency_map_core::{
18    ParallelPartitionPlan, bounded_parallel_map, normalized_concurrency,
19};
20
21#[cfg(test)]
22mod tests {
23    use super::*;
24
25    #[test]
26    fn normalized_concurrency_is_never_zero() {
27        assert_eq!(normalized_concurrency(0), 1);
28        assert_eq!(normalized_concurrency(1), 1);
29        assert_eq!(normalized_concurrency(8), 8);
30    }
31
32    #[test]
33    fn bounded_parallel_map_handles_zero_concurrency() {
34        let mut result = bounded_parallel_map((0..64).collect::<Vec<_>>(), 0, |x| x * 2);
35        result.sort_unstable();
36
37        let expected: Vec<i32> = (0..64).map(|x| x * 2).collect();
38        assert_eq!(result, expected);
39    }
40
41    #[test]
42    fn init_is_idempotent() {
43        init_concurrency_caps();
44        init_concurrency_caps();
45    }
46
47    #[test]
48    fn low_level_rayon_init_is_idempotent() {
49        assert!(init_rayon_global_once(1).is_ok());
50        assert!(init_rayon_global_once(8).is_ok());
51    }
52
53    #[test]
54    fn already_initialized_error_classifier_is_case_insensitive() {
55        assert!(is_already_initialized_error(
56            "The GlObAl thread pool has AlReAdY been initialized"
57        ));
58    }
59
60    #[test]
61    fn default_caps_have_expected_constants() {
62        let caps = ConcurrencyCaps::default();
63        assert_eq!(caps.rayon_threads, DEFAULT_RAYON_NUM_THREADS);
64        assert_eq!(caps.tokio_worker_threads, DEFAULT_TOKIO_WORKER_THREADS);
65    }
66
67    #[test]
68    fn env_var_constants_are_correct() {
69        assert_eq!(RAYON_NUM_THREADS_ENV, "RAYON_NUM_THREADS");
70        assert_eq!(TOKIO_WORKER_THREADS_ENV, "TOKIO_WORKER_THREADS");
71    }
72
73    #[test]
74    fn current_caps_returns_valid_values() {
75        let caps = current_caps();
76        assert!(caps.rayon_threads >= 1 || caps.rayon_threads == DEFAULT_RAYON_NUM_THREADS);
77        assert!(
78            caps.tokio_worker_threads >= 1
79                || caps.tokio_worker_threads == DEFAULT_TOKIO_WORKER_THREADS
80        );
81    }
82
83    #[test]
84    fn parse_positive_usize_or_default_basic_behavior() {
85        assert_eq!(parse_positive_usize_or_default(None, 10), 10);
86        assert_eq!(parse_positive_usize_or_default(Some("0"), 5), 5);
87        assert_eq!(parse_positive_usize_or_default(Some("42"), 1), 42);
88        assert_eq!(parse_positive_usize_or_default(Some("invalid"), 3), 3);
89    }
90
91    #[test]
92    fn bounded_parallel_map_single_element() {
93        let result = bounded_parallel_map(vec![99], 4, |x| x + 1);
94        assert_eq!(result, vec![100]);
95    }
96
97    #[test]
98    fn partition_plan_zero_items_is_safe() {
99        let plan = ParallelPartitionPlan::for_item_count(0, 4);
100        assert!(plan.use_direct_parallel_iter);
101        assert!(plan.chunk_size >= 1);
102    }
103}