From 9912c19a56c1cf2b9994cb6e6246f52d9bd990b7 Mon Sep 17 00:00:00 2001 From: ammar siddiqui Date: Tue, 26 May 2026 13:16:46 -0400 Subject: [PATCH] fix(test): serialize tests that mutate CORTEX_MAX_TOPOLOGY_BYTES `topology_size_limit_enforced` sets the env var to 16 to force the size guard to fire, then unsets it. `cargo test` runs tests in parallel by default, so a sibling test (`topology_round_trips_on_disk`) that calls `read_topology` while the limit is in effect spuriously fails with `TopologyTooLarge { bytes: 209, limit: 16 }`. Local runs happened to schedule the tests in an order that avoided the race; fresh CI hit it. Fix is a module-level `Mutex` that both env-touching tests acquire. The lock is poison-safe via `unwrap_or_else(|e| e.into_inner())` so one test panicking can't permanently wedge the suite. --- src/disk/mod.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/disk/mod.rs b/src/disk/mod.rs index c3fee6e..10e45a0 100644 --- a/src/disk/mod.rs +++ b/src/disk/mod.rs @@ -353,8 +353,16 @@ mod tests { event::{CortexEventKind, CortexEventRecord}, topology::{NeuronSpec, SynapseSpec, TopologyDefaults}, }; + use std::sync::Mutex; use std::time::SystemTime; + // Serializes any test that reads `CORTEX_MAX_TOPOLOGY_BYTES`, since + // `topology_size_limit_enforced` mutates it as process-global state + // and `cargo test` runs tests in parallel by default. Without this, + // a round-trip test can observe the 16-byte limit set by the + // size-limit test and erroneously fail with `TopologyTooLarge`. + static TOPOLOGY_LIMIT_ENV: Mutex<()> = Mutex::new(()); + fn tmp_root(label: &str) -> PathBuf { let nanos = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) @@ -389,6 +397,8 @@ mod tests { #[test] fn topology_round_trips_on_disk() { + // Lock against `topology_size_limit_enforced`; see TOPOLOGY_LIMIT_ENV. + let _guard = TOPOLOGY_LIMIT_ENV.lock().unwrap_or_else(|e| e.into_inner()); let root = tmp_root("topology"); let t = hh_empty(); write_topology(&root, &t).unwrap(); @@ -399,6 +409,8 @@ mod tests { #[test] fn topology_size_limit_enforced() { + // Holds for the duration of the env-var mutation. See TOPOLOGY_LIMIT_ENV. + let _guard = TOPOLOGY_LIMIT_ENV.lock().unwrap_or_else(|e| e.into_inner()); let root = tmp_root("limit"); let t = hh_empty(); write_topology(&root, &t).unwrap();