python/test-sleep
| Rule | test-sleep |
|---|---|
| Pack | Python |
| Severity | warning |
| Fix | none, edit by hand |
| Library versions | every version |
A test that waits a fixed time is slow and still fails when the wait is too short. Wait for the result itself, such as a join, an event or a poll with a deadline.
def test_worker(): done = threading.Event() start(done) time.sleep(0.5) assert done.is_set() assert done.wait(timeout=5)What the rule reports
Section titled “What the rule reports”The rule reports time.sleep and asyncio.sleep in a test file, but not asyncio.sleep(0).
How to fix it
Section titled “How to fix it”xpd has no automatic fix, so make the change by hand.
A sleep waits a fixed time and hopes the work is done by then, so the test fails on a slow
machine and wastes the time on a fast one. Wait for the result itself, with a join, an event or
a poll that has a deadline. asyncio.sleep(0) hands control to the event loop and waits for
nothing, so the rule leaves it alone.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
This test waits a fixed time. Wait for the result itself, such as a join, an event or a poll with a deadline.Browse the other rules of the Python pack.