Skip to content

Commit 7d2003c

Browse files
echobtfactorydroid
andauthored
fix(cortex-common): fix flaky signal_safety and cwd_guard tests (#478)
The tests were failing intermittently due to shared mutable state (static atomic variables and current working directory) being modified by multiple tests running in parallel. Fixes: - Added serial_test dependency to cortex-common dev-dependencies - Added #[serial] attribute to all signal_safety tests to prevent parallel execution of tests that use global signal handling state - Added #[serial] attribute to all cwd_guard tests to prevent parallel execution of tests that modify the current working directory - Fixed test_cleanup_guard by separating guard unwrap from assertion to keep the guard alive while testing lock exclusivity Co-authored-by: Droid Agent <droid@factory.ai>
1 parent 34beb21 commit 7d2003c

4 files changed

Lines changed: 62 additions & 1 deletion

File tree

Cargo.lock

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cortex-common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ async = ["tokio"]
3838

3939
[dev-dependencies]
4040
tempfile = { workspace = true }
41+
serial_test = { workspace = true }

cortex-common/src/cwd_guard.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,11 @@ pub fn validate_directory(dir: impl AsRef<Path>) -> io::Result<()> {
306306
#[cfg(test)]
307307
mod tests {
308308
use super::*;
309+
use serial_test::serial;
309310
use tempfile::TempDir;
310311

311312
#[test]
313+
#[serial]
312314
fn test_cwd_guard_restores() {
313315
let original = env::current_dir().unwrap();
314316
let temp_dir = TempDir::new().unwrap();
@@ -326,6 +328,7 @@ mod tests {
326328
}
327329

328330
#[test]
331+
#[serial]
329332
fn test_cwd_guard_restores_on_panic() {
330333
let original = env::current_dir().unwrap();
331334
let temp_dir = TempDir::new().unwrap();
@@ -341,6 +344,7 @@ mod tests {
341344
}
342345

343346
#[test]
347+
#[serial]
344348
fn test_cwd_guard_disabled() {
345349
let original = env::current_dir().unwrap();
346350
let temp_dir = TempDir::new().unwrap();
@@ -359,6 +363,7 @@ mod tests {
359363
}
360364

361365
#[test]
366+
#[serial]
362367
fn test_cwd_guard_restore_now() {
363368
let original = env::current_dir().unwrap();
364369
let temp_dir = TempDir::new().unwrap();
@@ -373,6 +378,7 @@ mod tests {
373378
}
374379

375380
#[test]
381+
#[serial]
376382
fn test_in_directory() {
377383
let original = env::current_dir().unwrap();
378384
let temp_dir = TempDir::new().unwrap();
@@ -385,6 +391,7 @@ mod tests {
385391
}
386392

387393
#[test]
394+
#[serial]
388395
fn test_in_directory_result() {
389396
let original = env::current_dir().unwrap();
390397
let temp_dir = TempDir::new().unwrap();
@@ -399,6 +406,7 @@ mod tests {
399406
}
400407

401408
#[test]
409+
#[serial]
402410
fn test_save_current() {
403411
let original = env::current_dir().unwrap();
404412
let temp_dir = TempDir::new().unwrap();

cortex-common/src/signal_safety.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,15 @@ impl Drop for SignalCleanupGuard {
171171
#[cfg(test)]
172172
mod tests {
173173
use super::*;
174+
use serial_test::serial;
174175

175176
fn reset_state() {
176177
release_signal_lock();
177178
reset_signal_count();
178179
}
179180

180181
#[test]
182+
#[serial]
181183
fn test_try_acquire_signal_lock() {
182184
reset_state();
183185

@@ -195,6 +197,7 @@ mod tests {
195197
}
196198

197199
#[test]
200+
#[serial]
198201
fn test_signal_count() {
199202
reset_state();
200203

@@ -211,6 +214,7 @@ mod tests {
211214
}
212215

213216
#[test]
217+
#[serial]
214218
fn test_should_force_exit() {
215219
reset_state();
216220

@@ -229,18 +233,23 @@ mod tests {
229233
}
230234

231235
#[test]
236+
#[serial]
232237
fn test_cleanup_guard() {
233238
reset_state();
234239

235240
// First guard should succeed
236241
{
237242
let guard = SignalCleanupGuard::try_new();
238243
assert!(guard.is_some());
239-
assert!(guard.unwrap().owns_lock());
244+
let guard = guard.unwrap();
245+
assert!(guard.owns_lock());
240246

241247
// While guard is alive, another should fail
242248
let guard2 = SignalCleanupGuard::try_new();
243249
assert!(guard2.is_none());
250+
251+
// Keep guard alive until end of scope
252+
drop(guard);
244253
}
245254

246255
// After guard is dropped, new guard should succeed
@@ -251,6 +260,7 @@ mod tests {
251260
}
252261

253262
#[test]
263+
#[serial]
254264
fn test_safe_signal_handler() {
255265
reset_state();
256266

0 commit comments

Comments
 (0)