Python · testing
sleep-with-computed-arg-in-test
python:sleep-with-computed-arg-in-test Computed `sleep()` in a test body — synchronize on the signal, don't guess a delay.
- Code
- SARJ047
- Default
- error
- Fix
- none
- Languages
- python
Why
A calculated delay still races the system under load and makes test duration depend on guessed timing.
Fix
Await completion, wait on an event, or poll the expected condition with a bounded deadline.
Before / after
Executed by this rule’s unit tests.
Before
Test guesses a computed delay
import asyncio
async def test_worker():
await asyncio.sleep(POLL_INTERVAL * 4)
assert finished()
After
Test waits for completion
async def test_worker():
finished = start_worker()
await finished.wait()
assert finished.is_set()
Limits
- Only `time.sleep` and `asyncio.sleep` calls directly inside test functions are analyzed.
- Numeric literal delays belong to SARJ031; helper-owned and nested-function sleeps are excluded.