pub const CONCURRENCY_FLOOR: u32 = 8;
pub const CONCURRENCY_CEILING: u32 = 256;
pub fn parse_concurrency_env() -> Option<u32> {
let raw = std::env::var_os("AUBE_CONCURRENCY")?;
if let Some(s) = raw.to_str()
&& let Ok(n) = s.parse::<u32>()
&& (CONCURRENCY_FLOOR..=CONCURRENCY_CEILING).contains(&n)
{
return Some(n);
}
tracing::warn!(
code = aube_codes::warnings::WARN_AUBE_CONCURRENCY_ENV_INVALID,
value = ?raw,
floor = CONCURRENCY_FLOOR,
ceiling = CONCURRENCY_CEILING,
"AUBE_CONCURRENCY ignored: must be an integer in [floor, ceiling]; using default"
);
None
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_env::ENV_LOCK;
fn with_env<F: FnOnce()>(value: Option<&str>, f: F) {
let _g = ENV_LOCK.lock().unwrap();
let prev = std::env::var_os("AUBE_CONCURRENCY");
unsafe {
match value {
Some(v) => std::env::set_var("AUBE_CONCURRENCY", v),
None => std::env::remove_var("AUBE_CONCURRENCY"),
}
}
f();
unsafe {
match prev {
Some(v) => std::env::set_var("AUBE_CONCURRENCY", v),
None => std::env::remove_var("AUBE_CONCURRENCY"),
}
}
}
#[test]
fn unset_returns_none() {
with_env(None, || assert_eq!(parse_concurrency_env(), None));
}
#[test]
fn in_range_returns_value() {
with_env(Some("64"), || {
assert_eq!(parse_concurrency_env(), Some(64));
});
}
#[test]
fn below_floor_warns_and_returns_none() {
with_env(Some("1"), || assert_eq!(parse_concurrency_env(), None));
}
#[test]
fn above_ceiling_warns_and_returns_none() {
with_env(Some("99999"), || {
assert_eq!(parse_concurrency_env(), None);
});
}
#[test]
fn non_numeric_warns_and_returns_none() {
with_env(Some("garbage"), || {
assert_eq!(parse_concurrency_env(), None);
});
}
#[test]
fn empty_warns_and_returns_none() {
with_env(Some(""), || assert_eq!(parse_concurrency_env(), None));
}
#[test]
fn floor_and_ceiling_inclusive() {
with_env(Some("8"), || {
assert_eq!(parse_concurrency_env(), Some(CONCURRENCY_FLOOR));
});
with_env(Some("256"), || {
assert_eq!(parse_concurrency_env(), Some(CONCURRENCY_CEILING));
});
}
}