From d553e4e54f87ef92ae1bfa6d5fccd593afc1371d Mon Sep 17 00:00:00 2001 From: ajianaz Date: Thu, 3 Sep 2026 12:14:55 +0700 Subject: [PATCH] fix(test): serialize retention env-var tests to stop CI flake TRAPFALL_RETENTION_DAYS is process-global; cargo test runs tests on parallel threads, so retention_days_custom_env and retention_days_invalid_falls_back raced each other's set/remove and failed intermittently. Guard both with a shared mutex. --- crates/trapfalld/src/config.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/trapfalld/src/config.rs b/crates/trapfalld/src/config.rs index b39d5db..98b6da9 100644 --- a/crates/trapfalld/src/config.rs +++ b/crates/trapfalld/src/config.rs @@ -398,6 +398,11 @@ mod tests { assert!(cfg.max_ingest_body_bytes < cfg.max_body_bytes, "ingest limit must be tighter than general API limit"); } + /// Serializes tests that mutate `TRAPFALL_RETENTION_DAYS`: cargo runs + /// tests on parallel threads and env vars are process-global, so two + /// tests touching the same var race and fail intermittently. + static RETENTION_ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + #[test] fn retention_days_default() { assert_eq!(default_retention_days(), 90); @@ -407,7 +412,7 @@ mod tests { #[test] fn retention_days_custom_env() { - // SAFETY: single-threaded test, no other code reads this env var concurrently. + let _guard = RETENTION_ENV_LOCK.lock().unwrap_or_else(|p| p.into_inner()); unsafe { std::env::set_var("TRAPFALL_RETENTION_DAYS", "30"); } @@ -419,7 +424,7 @@ mod tests { #[test] fn retention_days_invalid_falls_back() { - // SAFETY: single-threaded test, no other code reads this env var concurrently. + let _guard = RETENTION_ENV_LOCK.lock().unwrap_or_else(|p| p.into_inner()); unsafe { std::env::set_var("TRAPFALL_RETENTION_DAYS", "abc"); }