Skip to content

pytest wall time regressed from ~2min to ~20min (closes #469)#507

Merged
FidoCanCode merged 2 commits into
mainfrom
pytest-wall-time-regression
Apr 14, 2026
Merged

pytest wall time regressed from ~2min to ~20min (closes #469)#507
FidoCanCode merged 2 commits into
mainfrom
pytest-wall-time-regression

Conversation

@FidoCanCode
Copy link
Copy Markdown
Owner

@FidoCanCode FidoCanCode commented Apr 14, 2026

Fixes #469.

Shrink HTTPServer poll_interval in test fixtures so shutdown stops blocking 0.5s per test, and replace time.sleep(0.2) calls with deterministic thread joins. Together these cut ~21s of pure waste from the 27s serial run.


Work queue

Completed (2)
  • Use fast poll_interval for HTTPServer in test fixtures
  • Replace time.sleep(0.2) with background-thread join in test_server

Python's HTTPServer.serve_forever() defaults to poll_interval=0.5s,
so every srv.shutdown() call blocks up to 0.5s waiting for the loop
to notice the shutdown event. With ~40 tests using the server fixture
this was adding ~17s of pure wait to the serial test run.

Pass poll_interval=0.01 to serve_forever() in both the shared server
fixture and TestSelfRestart._make_server. Serial suite: 27s → 11s.
The wfile socket is unbuffered (StreamRequestHandler.wbufsize=0), so
_respond() delivers bytes to the client the moment they're written —
before do_POST() finishes.  Tests that asserted on work done after the
ack (background _process_action threads, synchronous _self_restart) were
papering over this race with time.sleep(0.2).

Fix: add two injectable hooks to WebhookHandler:
  _fn_spawn_bg       — spawns the background thread (was inline)
  _fn_after_do_post  — no-op by default; called via try/finally in do_POST

The autouse _restore_handler_fns fixture overrides _fn_after_do_post to
set a threading.Event (_post_done) so _post_webhook can wait for do_POST
to complete.  The server fixture also overrides _fn_spawn_bg to capture
the background thread in _bg_threads; _post_webhook joins it after the
event fires.

Result: all 28 time.sleep(0.2) calls removed; the 108 server tests now
complete in ~1.5s serial (was ~24s) with deterministic synchronisation.
@FidoCanCode FidoCanCode marked this pull request as ready for review April 14, 2026 22:28
@FidoCanCode FidoCanCode requested a review from rhencke April 14, 2026 22:28
@FidoCanCode FidoCanCode merged commit 1e9ef9c into main Apr 14, 2026
3 checks passed
@FidoCanCode FidoCanCode deleted the pytest-wall-time-regression branch April 14, 2026 22: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.

pytest wall time regressed from ~2min to ~20min

2 participants