Do not wait forever for test server to start or stop

Closed
AK
4mo ago
+31-4

Summary

I was wondering why the test suite just hung forever, and finally with pytest tests/ -vvvx --log-cli-level=DEBUG -s I found that:

[Errno 48] error while attempting to bind on address ('127.0.0.1', 8000): [errno 48] address already in use

โ€“ yep, I was working on something else on port 8000 โ€“ and so, server.running never becomes True, and tests just hang.

This PR:

  • Makes sure errors from server.run() (in the secondary thread) are caught and propagated immediately to the controlling loop
  • Adds timeouts, so even if something even more unexpected happens, the suite will error out instead of hanging forever.

Port of PR https://github.com/encode/httpx/pull/3775

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
    • No separate tests for testing the test fixture ๐Ÿ˜„
  • I've updated the documentation accordingly.
    • No user-facing or developer documentation changes needed. (Noting that port 8000 must not be in use is a separate thing to maybe document, I think...)