ab ovo
This commit is contained in:
133
tests/README_RACE_TESTS.md
Normal file
133
tests/README_RACE_TESTS.md
Normal file
@@ -0,0 +1,133 @@
|
||||
# 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:
|
||||
|
||||
1. **UploadState** - Concurrent byte counting and upload lifecycle management
|
||||
2. **DownloadState** - Concurrent download tracking and piece completion
|
||||
3. **RateLimiter** - Concurrent bandwidth limiting with CAS operations
|
||||
4. **FileSharingManager** - Iterator-while-modifying protection during cleanup
|
||||
5. **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:
|
||||
|
||||
```bash
|
||||
zig build test --summary all
|
||||
```
|
||||
|
||||
### Option 2: Manual test execution
|
||||
|
||||
You need to provide all module dependencies manually. This is complex and not recommended:
|
||||
|
||||
```bash
|
||||
# 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`:
|
||||
|
||||
```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 test
|
||||
- `OPS_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:
|
||||
|
||||
1. **Check thread counts** - Some systems may have limits on thread creation
|
||||
2. **Verify atomic operations** - Ensure all atomic fields use correct memory ordering
|
||||
3. **Look for timing dependencies** - Tests use minimal sleeps; adjust if needed
|
||||
4. **Enable sanitizers** - Use `-fsanitize=thread` to detect data races
|
||||
5. **Increase verbosity** - Add logging to track test progress
|
||||
|
||||
## Race Conditions Fixed
|
||||
|
||||
These tests verify the fixes for the following race conditions:
|
||||
|
||||
1. **UploadState/DownloadState**: Non-transactional atomic updates (documented as acceptable)
|
||||
2. **FileSharingManager**: Iterator-while-modifying in `cleanupCompletedSessions` (fixed with two-phase approach)
|
||||
3. **RateLimiter**: Read-modify-write race in `startTransfer` and `completeTransfer` (fixed with atomic operations and CAS)
|
||||
4. **NetworkTopology**: Multiple timestamp() calls (fixed by calling once and reusing)
|
||||
5. **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
|
||||
Reference in New Issue
Block a user