Skip to content

Add environment variable mutexes for test isolation#3

Open
skypher wants to merge 2 commits intomainfrom
fix/remaining-test-failures
Open

Add environment variable mutexes for test isolation#3
skypher wants to merge 2 commits intomainfrom
fix/remaining-test-failures

Conversation

@skypher
Copy link
Copy Markdown
Owner

@skypher skypher commented Nov 15, 2025

Summary

Adds global mutexes for PATH and HOME environment variables to prevent race conditions in parallel test execution.

Problem

Tests that modify environment variables using unsafe { std::env::set_var } affect the entire process. Even with per-module test mutexes, these modifications can race when tests run in parallel, causing non-deterministic failures.

Solution

Created src/test_utils.rs with global environment variable mutexes:

  • lock_path_mutex() - Serializes all PATH modifications
  • lock_home_mutex() - Serializes all HOME modifications

Updated 29 test files across the codebase to acquire these mutexes before modifying environment variables.

Results

Test Failure Reduction:

Before: 17-20 failures (parallel run)
After:  14 failures (parallel run)
Reduction: ~20-30% fewer failures

Sequential Tests:

All 207 tests pass with --test-threads=1 ✅

Remaining 14 Failures

The remaining parallel test failures are due to deeper architectural issues:

  1. Global Index State: Multiple tests modify the same global index singleton
  2. Disk Cache Sharing: Tests write to overlapping cache directories
  3. Timing Dependencies: Some tests have race conditions in async operations

These require significant refactoring:

  • Making index state per-test instead of global
  • Using unique temp directories per test
  • Better async test synchronization

Files Changed

New:

  • src/test_utils.rs - Global environment variable mutexes

Updated (29 files):

  • src/lib.rs - Adds test_utils module
  • src/index/*.rs - 5 files with PATH guards
  • src/logic/**/*.rs - 3 files with PATH guards
  • src/sources/*.rs - 2 files with PATH guards
  • src/install/*.rs - 5 files with PATH guards
  • src/events/mod.rs - PATH guards
  • src/state/app_state.rs - HOME guards
  • src/theme/**/*.rs - 3 files with HOME guards

Dependencies

This PR builds on top of:

Testing

# Sequential (all pass)
cargo test --lib -- --test-threads=1

# Parallel (193/207 pass)
cargo test --lib

# With limited parallelism (better success rate)
cargo test --lib -- --test-threads=4

Adds poison-recovery helpers to prevent cascading test failures when a
test panics while holding a mutex lock.

Changes:
- Add lock_test_mutex() helper functions to all modules with test mutexes
- Replace all test_mutex().lock().unwrap() calls with lock_test_mutex()
- Automatically recover poisoned mutexes using unwrap_or_else

Impact:
- Reduced parallel test failures significantly
- All 207 tests pass when run sequentially (--test-threads=1)
- Remaining parallel failures are due to other isolation issues (PATH,
  temp files) that require per-test refactoring

Modules updated:
- src/index/mod.rs
- src/logic/mod.rs
- src/state/mod.rs
- src/sources/mod.rs
- src/theme/mod.rs
- All test files using test mutexes
Fixes race conditions from parallel tests modifying PATH and HOME
environment variables.

Changes:
- Created src/test_utils.rs with global PATH and HOME mutexes
- Added lock_path_mutex() helper for PATH-modifying tests
- Added lock_home_mutex() helper for HOME-modifying tests
- Updated 23 test files to use PATH mutex
- Updated 6 test files to use HOME mutex

Results:
Before: 17-20 test failures in parallel
After: 14 test failures in parallel
Improvement: ~20-30% reduction in parallel test failures

All 207 tests still pass when run sequentially (--test-threads=1)

Remaining Issues:
The 14 remaining parallel failures are due to:
- Tests sharing global index singleton state
- Tests modifying the same disk cache paths
- These require deeper refactoring beyond environment isolation

Files Updated:
- src/test_utils.rs (new file)
- src/lib.rs (adds test_utils module)
- src/index/*.rs (5 files with PATH guards)
- src/logic/**/*.rs (3 files with PATH guards)
- src/sources/*.rs (2 files with PATH guards)
- src/install/*.rs (5 files with PATH guards)
- src/events/mod.rs (PATH guards)
- src/state/app_state.rs (HOME guards)
- src/theme/**/*.rs (3 files with HOME guards)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant