Repository navigation
test: make HTTP deadline tests deterministic - #816
Conversation
test_deadline and test_address_fallback_bounded gave requests 80 to 100 ms of real time and expected the client to reach the connect step within it. On slow CI runners (Python 3.15, coverage), the client's own work before connecting took longer, so the budget was gone before the first connect attempt. - test_address_fallback_bounded runs on a fake clock of foca.utils.http that the fake connect advances; the shared connect budget is now exact (timeouts [2.0, 1.0], then [0.5]). - test_deadline and test_deadline_bounds_reads give requests 0.5 s before their deadline (CONNECT_ROOM), against servers that keep sending until the client hangs up, so only the read can reach it. Closes #815 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The first address of test_address_fallback_bounded now times out and the second refuses, so keeping the first error instead of the last one fails the test. Also note that the fake time module has monotonic only. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideTestsMake the slow HTTP handler stream until the client hangs up, then give deadline tests a larger connection window while retaining a bounded completion check.
Add a fake HTTP-module clock and use it to assert exact shared connect budgets and that the final address error is reported.
Widen the overlapping concurrent-deadline test timings while preserving that the second request finishes after the first request’s deadline.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #816 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 76 76
Lines 10640 10640
=========================================
Hits 10640 10640
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Linked issuesChecked at commit 18e323a #815 test: fix flaky deadline tests of the HTTP client · 2 of 2 met
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="tests/utils/test_http.py" line_range="133" />
<code_context>
+# Seconds that requests in deadline tests get before their deadline: enough
+# for the client to connect also on slow, busy runners (coverage, debug
+# logs), so that only the read can reach the deadline
+CONNECT_ROOM = 0.5
+
+
</code_context>
<issue_to_address>
**Read-timeout tests fail before reading**
When client setup or runner scheduling consumes the 0.5-second margin before the request is sent, `_new_conn` or `GuardedClient.request` finds the deadline expired and raises `DeadlineExceeded`, so the tests fail instead of observing the expected `RequestMaybeSent` read timeout.
Give the read-timeout tests a deadline margin large enough to cover setup and scheduling delays before the request is sent.
Also at `tests/utils/test_http.py:327`, `tests/utils/test_http.py:595`.
</issue_to_address>
### Comment 2
<location path="tests/utils/test_http.py" line_range="133" />
<code_context>
+# Seconds that requests in deadline tests get before their deadline: enough
+# for the client to connect also on slow, busy runners (coverage, debug
+# logs), so that only the read can reach the deadline
+CONNECT_ROOM = 0.5
+
+
</code_context>
<issue_to_address>
**Concurrent deadline test remains flaky**
When request preparation takes more than 0.15 s before the second request’s server delay, `test_concurrent_deadlines` still gives the second request a 1 s timeout for an 0.85 s server delay, so more than 0.15 s of request-preparation overhead makes it time out and the results assertion fail.
Give the concurrent test enough timeout margin or control its timing deterministically as well.
</issue_to_address>Sourcery assessment
Approval pending. 2 findings to address first.
Blocking findings: tests/utils/test_http.py:133, tests/utils/test_http.py:133
CONNECT_ROOM goes from 0.5 s to 1.0 s (bounds from 2 s to 3 s), and test_concurrent_deadlines gives each request 0.5 s or more of room before its own deadline (was 0.15 s), keeping the requests overlapping and the second one ending after the first one's deadline. All tests of the module pass with 0.4 s of injected client overhead. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes #815
Summary
test_deadlineandtest_address_fallback_boundedgave requests 80 to 100 ms of real time and expected the client to reach the connect step within it. On slow CI runners (Python 3.15, coverage), the client's own work before connecting took longer, so the budget ran out before the first connect attempt (DeadlineExceeded: No time left to connect,assert 0 == 1).test_address_fallback_boundednow runs on a fake clock offoca.utils.http(clockfixture) that the fake connect advances. The shared connect budget is exact, and the test is stricter than before: the attempts get[2.0, 1.0]seconds, then[0.5]with no time left for the second address.test_deadlineandtest_deadline_bounds_readsgive requests 1.0 s (CONNECT_ROOM) before their deadline, against servers that keep sending until the client hangs up (/slownow sends for up to 5 s instead of 0.4 s), so only the read can reach the deadline. They still check that the request ends long before the server would (< 3 s). These need a real deadline (timer and socket timeouts), so they keep a margin rather than a fake clock.test_address_fallback_boundednow also checks that the last connect error counts (first address times out, second refuses); before, both attempts raised the same error, so keeping the first error would have passed (checked by mutating_connect_any).Test-only change; no docs affected.
Verification
requests.Session.prepare_request): both tests failed exactly as in CI. With 0.35 s,test_deadline_bounds_readsfailed the same way.test_address_fallback_boundedpasses with 1.5 s.pytest --count 50on the affected tests: 300 passed.pytest -m "not mongodb": 5292 passed, 2 skipped. MongoDB tests not run locally (need a server on localhost:12345); CI runs them.ruff check,ruff format --check,mypy,ast-grep scan: pass.Declined review findings
test_deadlineandtest_deadline_bounds_reads(stall > 1 s before sending): accepted after raising the margin to 1 s (Sourcery); passes with 0.4 s injected overhead and under full CPU load. Removing it entirely would need a change to the client (start the deadline at send), which would change its semantics./slowhandler andtricklefixture overlap: kept, they cover HTTP/1.0 viahttp.serverand a raw socket server respectively.Notes
test_concurrent_deadlines(Sourcery): margins widened from 0.15 s to 0.5 s or more before each request's own deadline; still overlapping, the second request still ends after the first one's deadline.test_reused_connections_bounded, 0.5 s timeout, is the next one to fail at 0.5 s; it has not flaked in CI).🤖 Generated with Claude Code