4.7 KiB
4.7 KiB
Race Condition Tests
This directory contains comprehensive race condition tests for the P2P library.
Test Coverage
The tests in test_race_conditions.zig verify thread safety of:
- UploadState - Concurrent byte counting and upload lifecycle management
- DownloadState - Concurrent download tracking and piece completion
- RateLimiter - Concurrent bandwidth limiting with CAS operations
- FileSharingManager - Iterator-while-modifying protection during cleanup
- Mixed operations - Realistic concurrent usage scenarios
Running the Tests
These tests require the full build system to run. They cannot be run standalone because they depend on the core, network, and files modules.
Option 1: Using build.zig (Recommended)
If your build.zig has test support:
zig build test --summary all
Option 2: Manual test execution
You need to provide all module dependencies manually. This is complex and not recommended:
# Example (exact flags depend on your build configuration)
zig test tests/test_race_conditions.zig \
-Mcore=/path/to/core \
-Mnetwork=/path/to/network \
-Mfiles=/path/to/files \
...
Option 3: Integration into build.zig
Add to your build.zig:
const race_tests = b.addTest(.{
.root_source_file = b.path("tests/test_race_conditions.zig"),
.target = target,
.optimize = optimize,
});
// Add module dependencies
race_tests.root_module.addImport("core", core_module);
race_tests.root_module.addImport("files", files_module);
// ... other dependencies
const run_race_tests = b.addRunArtifact(race_tests);
test_step.dependOn(&run_race_tests.step);
Test Configuration
The tests use the following configuration (defined in the test file):
NUM_THREADS = 8- Number of concurrent threads per testOPS_PER_THREAD = 1000- Number of operations each thread performs
You can adjust these values to:
- Increase stress (more threads, more operations)
- Reduce test time (fewer threads, fewer operations)
- Test specific race conditions (adjust specific test parameters)
What the Tests Verify
1. UploadState Tests
- Concurrent
addUploadedBytes()maintains correct totals - Concurrent
startUpload()/completeUpload()lifecycle is correct - No lost updates due to race conditions
2. DownloadState Tests
- Concurrent byte tracking is accurate
- Concurrent piece completion counting is correct
- Downloaded and verified bytes are properly synchronized
3. RateLimiter Tests
- CAS-based
completeTransfer()handles high contention correctly - Bandwidth accounting remains consistent under concurrent load
- Active transfer count is accurate after all operations complete
- Saturating subtraction prevents bandwidth underflow
4. FileSharingManager Tests
- Session cleanup doesn't crash when called concurrently
- Two-phase iterator approach prevents iterator-while-modifying bugs
- Concurrent
processSessions()calls are safe
5. Mixed Operation Tests
- Realistic scenario with uploads, downloads, and rate limiting
- Multiple types of atomic operations running concurrently
- System remains consistent after all operations complete
Expected Results
All tests should pass with:
- No crashes or assertion failures
- Correct final state (counters match expected values)
- No race conditions detected by thread sanitizers (if enabled)
- Consistent behavior across multiple runs
Troubleshooting
If tests fail:
- Check thread counts - Some systems may have limits on thread creation
- Verify atomic operations - Ensure all atomic fields use correct memory ordering
- Look for timing dependencies - Tests use minimal sleeps; adjust if needed
- Enable sanitizers - Use
-fsanitize=threadto detect data races - Increase verbosity - Add logging to track test progress
Race Conditions Fixed
These tests verify the fixes for the following race conditions:
- UploadState/DownloadState: Non-transactional atomic updates (documented as acceptable)
- FileSharingManager: Iterator-while-modifying in
cleanupCompletedSessions(fixed with two-phase approach) - RateLimiter: Read-modify-write race in
startTransferandcompleteTransfer(fixed with atomic operations and CAS) - NetworkTopology: Multiple timestamp() calls (fixed by calling once and reusing)
- P2PStatistics: Error counters are atomic (already correct, documented)
Notes
- These are stress tests designed to expose race conditions through concurrent load
- Some tests use
std.time.sleep()to create timing windows for races - Tests may take longer on systems with limited CPU cores
- The tests verify that atomic operations work correctly, not that they're the fastest solution