Skip to content

test: make HTTP deadline tests deterministic - #816

Merged
uniqueg merged 3 commits into
devfrom
test/815-flaky-deadline-tests
Nov 22, 2026
Merged

uniqueg merged 3 commits into
devfrom
test/815-flaky-deadline-tests

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Closes #815

Summary

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 ran out before the first connect attempt (DeadlineExceeded: No time left to connect, assert 0 == 1).

  • test_address_fallback_bounded now runs on a fake clock of foca.utils.http (clock fixture) 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_deadline and test_deadline_bounds_reads give requests 1.0 s (CONNECT_ROOM) before their deadline, against servers that keep sending until the client hangs up (/slow now 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_bounded now 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

  • Reproduced both CI failures deterministically before the fix with a pytest plugin that adds 0.1 s of overhead before connecting (patched requests.Session.prepare_request): both tests failed exactly as in CI. With 0.35 s, test_deadline_bounds_reads failed the same way.
  • After the fix: the whole module passes with 0.4 s of injected overhead; test_address_fallback_bounded passes with 1.5 s.
  • pytest --count 50 on 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.
  • CI: all checks pass (Test on Python 3.11-3.15, MongoDB 7.0/8.0, lowest dependencies; integration; codecov).

Declined review findings

  • Remaining real-time dependency of test_deadline and test_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.
  • /slow handler and trickle fixture overlap: kept, they cover HTTP/1.0 via http.server and 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.
  • All tests of the module pass with 0.4 s of injected client overhead (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

uniqueg and others added 2 commits November 22, 2026 06:36
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]>
@uniqueg uniqueg self-assigned this Nov 22, 2026
@sourcery-ai

sourcery-ai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Tests

Make the slow HTTP handler stream until the client hangs up, then give deadline tests a larger connection window while retaining a bounded completion check.

  • tests/utils/test_http.py L75-82, L325-329, L595-597

Add a fake HTTP-module clock and use it to assert exact shared connect budgets and that the final address error is reported.

  • tests/utils/test_http.py L130-183, L682-693, L699-706 (+1 small edits)

Widen the overlapping concurrent-deadline test timings while preserving that the second request finishes after the first request’s deadline.

  • tests/utils/test_http.py L826, L837-840

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (e0822b9) to head (18e323a).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #816   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           76        76           
  Lines        10640     10640           
=========================================
  Hits         10640     10640           
Flag Coverage Δ
test_unit 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@uniqueg
uniqueg marked this pull request as ready for review November 22, 2026 06:51
@sourcery-ai

sourcery-ai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Linked issues

Checked at commit 18e323a

#815 test: fix flaky deadline tests of the HTTP client · 2 of 2 met

# Requirement Status Notes
1 Use a fake clock for test_address_fallback_bounded so the shared connect budget is exact. ✅ Met The clock fixture replaces foca.utils.http's monotonic clock, and the fake connect advances it; the test checks the exact timeout sequences [2.0, 1.0] and [0.5].
2 Give test_deadline and test_deadline_bounds_reads enough time to connect, with the server continuing to send until the client gives up so the read reaches the deadline. ✅ Met Both tests use CONNECT_ROOM=1.0; /slow sends for up to 5 seconds and the trickle server keeps sending until stopped, while the tests assert a read_timeout.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread tests/utils/test_http.py Resolved
Comment thread tests/utils/test_http.py Resolved
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]>
@uniqueg
uniqueg merged commit 8b6ab78 into dev Nov 22, 2026
83 checks passed
@uniqueg
uniqueg deleted the test/815-flaky-deadline-tests branch November 22, 2026 07:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test: fix flaky deadline tests of the HTTP client

1 participant