Repository navigation
test(fuzz): seed random per fuzz test for reproducibility - #531
Conversation
Closes one item in #478 ("unseeded fuzz, mock overuse"). The fuzz tests in tests/fuzz/ each call random.randint inside loops of 500-1000 iterations. Without a per-test seed, an iteration-N failure inside the loop has no recipe to reproduce — pytest-randomly's session-wide seed makes the run ID reproducible, but not each test in isolation. Add tests/fuzz/conftest.py with an autouse fixture that seeds random per test. The seed is a stable XOR of a project base seed and the test's nodeid hash, so every fuzz test gets a unique deterministic stream regardless of test order or pytest-randomly's session seed. Re-running the same test produces the same byte stream — failures become reproducible by name alone. Verified by repeating the fuzz suite twice with identical outcomes (305 passed, identical timing). Co-Authored-By: Claude <[email protected]>
|
Warning Rate limit exceeded
To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5c163f87e2
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
## Summary Addresses one item from #478 ("unseeded fuzz, mock overuse"). Adds an autouse conftest fixture that seeds the \`random\` module per fuzz test, so iteration-N failures inside 500-1000-iteration loops become reproducible by test name alone. Ref #478. ## Problem Each fuzz test in \`tests/fuzz/\` (\`fuzz_fts5_escape.py\`, \`fuzz_path_sanitizer.py\`, \`fuzz_timestamp.py\`, \`fuzz_json_parsers.py\`) follows the shape: \`\`\`python def test_X_random(self) -> None: import random for _ in range(1000): length = random.randint(1, 200) data = bytes(random.randint(0, 255) for _ in range(length)) ... \`\`\` \`pytest-randomly\` seeds the global random module at session start, but inside a long-running test the per-iteration random byte stream varies with anything else that touched \`random\` earlier in the session — and an iteration-N failure has no recipe to reproduce by test name. The audit flagged this as the "unseeded fuzz" gap. ## Solution \`tests/fuzz/conftest.py\` with an autouse fixture: \`\`\`python @pytest.fixture(autouse=True) def _seed_fuzz_random(request: pytest.FixtureRequest) -> None: test_seed = _BASE_SEED ^ (hash(request.node.nodeid) & 0xFFFFFFFF) random.seed(test_seed) \`\`\` Each fuzz test gets a stable per-nodeid seed, so: - Re-running the same test produces the same byte stream. - Different fuzz tests get distinct streams (no cross-test repetition). - The session-wide \`pytest-randomly\` seed is irrelevant — re-seeding inside the test function overrides it. The fixture is scoped to \`tests/fuzz/\` only; non-fuzz tests are unaffected. ## Verification \`\`\` $ nix develop -c pytest -q tests/fuzz/fuzz_fts5_escape.py tests/fuzz/fuzz_path_sanitizer.py tests/fuzz/fuzz_timestamp.py tests/fuzz/fuzz_json_parsers.py 305 passed in 8.59s $ <repeat> 305 passed in 8.95s $ nix develop -c devtools verify --quick verify: all checks passed \`\`\` Re-runs give identical outcomes. Failures (none expected today) would now be reproducible by re-running the same test alone. ## Risks and Follow-ups The fuzz suite isn't auto-collected by default \`pytest tests/\` (the files are named \`fuzz_*.py\`, not \`test_*.py\`). This change is correct for any explicit invocation; widening collection is out of scope here. Other items from #478 (operations/archive coverage gap, site/ coverage gap, runtime test mock overuse, mypy gaps) are individually larger and remain open for follow-up PRs. Co-authored-by: Claude <[email protected]>
|
Historical Codex triage: evidence checked at 5ebcfe5. This records that source revision; it does not claim a fresh review of current master.
Only the rows listed above are dispositioned. Held findings remain open. |
Summary
Addresses one item from #478 ("unseeded fuzz, mock overuse"). Adds an autouse conftest fixture that seeds the `random` module per fuzz test, so iteration-N failures inside 500-1000-iteration loops become reproducible by test name alone.
Ref #478.
Problem
Each fuzz test in `tests/fuzz/` (`fuzz_fts5_escape.py`, `fuzz_path_sanitizer.py`, `fuzz_timestamp.py`, `fuzz_json_parsers.py`) follows the shape:
```python
def test_X_random(self) -> None:
import random
for _ in range(1000):
length = random.randint(1, 200)
data = bytes(random.randint(0, 255) for _ in range(length))
...
```
`pytest-randomly` seeds the global random module at session start, but inside a long-running test the per-iteration random byte stream varies with anything else that touched `random` earlier in the session — and an iteration-N failure has no recipe to reproduce by test name. The audit flagged this as the "unseeded fuzz" gap.
Solution
`tests/fuzz/conftest.py` with an autouse fixture:
```python
@pytest.fixture(autouse=True)
def _seed_fuzz_random(request: pytest.FixtureRequest) -> None:
test_seed = _BASE_SEED ^ (hash(request.node.nodeid) & 0xFFFFFFFF)
random.seed(test_seed)
```
Each fuzz test gets a stable per-nodeid seed, so:
The fixture is scoped to `tests/fuzz/` only; non-fuzz tests are unaffected.
Verification
```
$ nix develop -c pytest -q tests/fuzz/fuzz_fts5_escape.py tests/fuzz/fuzz_path_sanitizer.py tests/fuzz/fuzz_timestamp.py tests/fuzz/fuzz_json_parsers.py
305 passed in 8.59s
$
305 passed in 8.95s
$ nix develop -c devtools verify --quick
verify: all checks passed
```
Re-runs give identical outcomes. Failures (none expected today) would now be reproducible by re-running the same test alone.
Risks and Follow-ups
The fuzz suite isn't auto-collected by default `pytest tests/` (the files are named `fuzz_.py`, not `test_.py`). This change is correct for any explicit invocation; widening collection is out of scope here.
Other items from #478 (operations/archive coverage gap, site/ coverage gap, runtime test mock overuse, mypy gaps) are individually larger and remain open for follow-up PRs.