Skip to main content

adze_concurrency_init_bootstrap_policy_core/
lib.rs

1//! Pure policy for bootstrap concurrency-caps normalization.
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
10use adze_concurrency_env_core::ConcurrencyCaps;
11use adze_concurrency_normalize_core::normalized_concurrency;
12
13/// Normalize bootstrap caps to a safe, process-init-ready configuration.
14#[must_use]
15pub fn bootstrap_caps(caps: ConcurrencyCaps) -> ConcurrencyCaps {
16    ConcurrencyCaps {
17        rayon_threads: normalized_concurrency(caps.rayon_threads),
18        tokio_worker_threads: caps.tokio_worker_threads,
19    }
20}
21
22#[cfg(test)]
23mod tests {
24    use super::bootstrap_caps;
25    use adze_concurrency_env_core::{ConcurrencyCaps, DEFAULT_TOKIO_WORKER_THREADS};
26
27    #[test]
28    fn bootstrap_caps_normalizes_zero_rayon_threads() {
29        let caps = bootstrap_caps(ConcurrencyCaps {
30            rayon_threads: 0,
31            tokio_worker_threads: DEFAULT_TOKIO_WORKER_THREADS,
32        });
33
34        assert_eq!(caps.rayon_threads, 1);
35        assert_eq!(caps.tokio_worker_threads, DEFAULT_TOKIO_WORKER_THREADS);
36    }
37
38    #[test]
39    fn bootstrap_caps_preserves_nonzero_rayon_threads() {
40        let caps = bootstrap_caps(ConcurrencyCaps {
41            rayon_threads: 16,
42            tokio_worker_threads: 4,
43        });
44        assert_eq!(caps.rayon_threads, 16);
45    }
46
47    #[test]
48    fn bootstrap_caps_preserves_tokio_worker_threads_unchanged() {
49        for tokio in [0, 1, 5, 100] {
50            let caps = bootstrap_caps(ConcurrencyCaps {
51                rayon_threads: 2,
52                tokio_worker_threads: tokio,
53            });
54            assert_eq!(caps.tokio_worker_threads, tokio);
55        }
56    }
57
58    #[test]
59    fn bootstrap_caps_is_idempotent() {
60        let input = ConcurrencyCaps {
61            rayon_threads: 0,
62            tokio_worker_threads: 7,
63        };
64        let once = bootstrap_caps(input);
65        let twice = bootstrap_caps(once);
66        assert_eq!(once, twice);
67    }
68
69    #[test]
70    fn bootstrap_caps_one_thread_stays_one() {
71        let caps = bootstrap_caps(ConcurrencyCaps {
72            rayon_threads: 1,
73            tokio_worker_threads: 1,
74        });
75        assert_eq!(caps.rayon_threads, 1);
76    }
77}