Repository navigation
test(client): make the constructor read-timeout case deterministic - #740
Conversation
A socket cannot wait less than about 1 ms, and a local backend answers in about as long, so the 0.00001 s case of test_client_constructor raced against the server and sometimes failed with DID NOT RAISE ReadTimeout. Test it against a local socket that accepts the connection and never responds. The request, the timeout and the exception stay real, and the outcome no longer depends on the speed of the machine. A timer closes the socket after 10 s, so a client that loses its timeout fails the test instead of hanging the job.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #740 +/- ##
==========================================
+ Coverage 95.12% 95.33% +0.20%
==========================================
Files 60 61 +1
Lines 3364 3428 +64
==========================================
+ Hits 3200 3268 +68
+ Misses 164 160 -4
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:
|
MateoLostanlen
left a comment
There was a problem hiding this comment.
Hi @Adelagric, thanks for the PR!
Great root-cause analysis, and the silent socket is a clean way to make this deterministic without mocking.
- (nit) All remaining rows of
test_client_constructornow usetimeout=10, so thetimeoutparameter can go. - (nit) The
silent_hostdocstring says it "accepts connections", but nothing callsaccept(). Maybe "queues connections"?
Happy to merge as is, or after these small tweaks if you want to make them.
…test Every remaining row of test_client_constructor uses the same timeout, so it no longer needs to be a parameter. The silent_host docstring now says the socket queues connections, since nothing calls accept().
|
Thanks for the review. Both tweaks are in The push dismissed your approval, sorry about that. Could you approve it again when you have a moment? |
MateoLostanlen
left a comment
There was a problem hiding this comment.
Hi @Adelagric, thanks for the PR!
Both tweaks look good, approving again. Merging now.
test_client_constructorexpectedClient(..., timeout=0.00001)to raiseReadTimeoutagainst the real backend. It sometimes fails withDID NOT RAISE ReadTimeout, as in this run on #734, which does not touchclient/.Cause. A socket cannot wait less than about 1 ms:
settimeout(1e-5)blocked for 1.0 to 1.15 ms on a silent peer. A full GET to the local backend takes 1.2 to 1.8 ms, so the case is a race and its outcome depends on the machine. Run on its own in separate processes, the old test failed 19 times out of 38 on my laptop. In CI,pytest-clientfailed on 1 of the last 40 runs oftests.yml.Fix. The case moves to its own test, against a local socket that listens and never calls
accept(). The kernel completes the handshake, so the client connects, sends its request and waits. The request, the timeout and the exception stay real, and no server thread is needed.ConnectTimeoutis not aReadTimeout, so it needs some margin.pytest-timeouthere, so a client that loses its timeout would otherwise hang the job.client/pyroclientis unchanged. The other three rows oftest_client_constructorstill use the real backend.Why not mock
requests.get. A mock that raisesReadTimeoutonly checks that the constructor lets it through. It stays green when the constructor stops passing its timeout.Known limit. The first GET times out, so the new test never reaches the token check. A constructor that swallowed the timeout on that second GET alone would still pass. The old case only got there by chance.
Test plan
Each change below was made on a copy of
pyroclient, on macOS and in a Linux container:TimeoutDID NOT RAISE ReadTimeoutTimeouttoValueError/statusGET loses its timeoutConnection reset by peerTimeouton/statusonlyTimeouton the token check onlyThe new test had no failure in 4800 runs:
Against the
docker-compose.dev.ymlstack on macOS, the full client suite passes (19 passed).ruff checkandruff format --checkare clean.I ran these in a Python 3.11 venv with the locked
requests,urllib3andpytestversions, not throughuv run. The CI of this PR will be the first run on a GitHub runner.