Repository navigation
test(apps): hold the port the retry test targets #800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,55 @@ | ||
| """shared fixtures for apps tests.""" | ||
|
|
||
| import socket | ||
| import struct | ||
| import threading | ||
| from unittest.mock import AsyncMock | ||
|
|
||
| import pytest | ||
|
|
||
| from runpod.apps import gpu as _gpu | ||
|
|
||
| ACCEPT_POLL_SECONDS = 0.05 | ||
| # struct linger {l_onoff=1, l_linger=0}: close() sends RST instead of FIN | ||
| RESET_ON_CLOSE = struct.pack("ii", 1, 0) | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def offline_gpu_catalog(monkeypatch): | ||
| """payload builders read the live gpu catalog; keep tests offline and | ||
| deterministic on the static pool table unless a test overrides it.""" | ||
| monkeypatch.setattr(_gpu, "live_pool_members", lambda: None) | ||
| monkeypatch.setattr(_gpu, "live_pool_members_async", AsyncMock(return_value=None)) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def resetting_tcp_port(): | ||
| """A localhost port that resets every connection for the whole test. | ||
|
|
||
| pytest-asyncio's unused_tcp_port releases the port before the test runs, | ||
| so another process (a parallel test run, say) can bind it and receive the | ||
| requests meant to fail. This fixture keeps the port held by a listener | ||
| that accepts each connection and closes it with SO_LINGER 0, which sends | ||
| a TCP reset. A bound socket that never listens would be simpler, but | ||
| macOS drops connects to it until they time out instead of refusing them. | ||
| """ | ||
| stop = threading.Event() | ||
| with socket.socket() as listener: | ||
| listener.bind(("127.0.0.1", 0)) | ||
| listener.listen() | ||
| listener.settimeout(ACCEPT_POLL_SECONDS) | ||
|
|
||
| def reset_connections(): | ||
| while not stop.is_set(): | ||
| try: | ||
| conn, _ = listener.accept() | ||
| except TimeoutError: | ||
| continue | ||
| conn.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, RESET_ON_CLOSE) | ||
| conn.close() | ||
|
|
||
| thread = threading.Thread(target=reset_connections, daemon=True) | ||
| thread.start() | ||
| yield listener.getsockname()[1] | ||
| stop.set() | ||
| thread.join() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -136,9 +136,23 @@ async def test_client_4xx_not_retried(flaky_server): | |
| assert flaky_server.state["hits"] == 1 | ||
|
|
||
|
|
||
| async def test_connection_error_retried(unused_tcp_port): | ||
| # nothing listening: pure connection failures, all attempts consumed | ||
| url = f"http://127.0.0.1:{unused_tcp_port}/run" | ||
| def test_resetting_port_resets_connections(resetting_tcp_port): | ||
| with socket.create_connection(("127.0.0.1", resetting_tcp_port), timeout=2) as client: | ||
| with pytest.raises(ConnectionResetError): | ||
| client.sendall(b"GET /run HTTP/1.1\r\nHost: x\r\n\r\n") | ||
| client.recv(1) | ||
|
|
||
|
|
||
| def test_resetting_port_cannot_be_taken_by_another_server(resetting_tcp_port): | ||
| """Requests sent to the port must not reach a server started elsewhere, | ||
| such as a parallel test run that binds the same ephemeral port.""" | ||
| with pytest.raises(OSError): | ||
| ThreadingHTTPServer(("127.0.0.1", resetting_tcp_port), BaseHTTPRequestHandler) | ||
|
|
||
|
|
||
| async def test_connection_error_retried(resetting_tcp_port): | ||
| # every connection is reset: connection failures, all attempts consumed | ||
| url = f"http://127.0.0.1:{resetting_tcp_port}/run" | ||
| with patch("runpod.apps.targets.RETRY_BASE_DELAY", 0.01): | ||
| with pytest.raises(aiohttp.ClientConnectionError): | ||
| await _request_json("GET", url, {}, timeout=5) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Medium: Resetting during asyncio transport setup can leave the old transport registered, so a later retry raises |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Medium: If the listener resets before
sendall, macOS raisesBrokenPipeError, so this new test flakes even though the fixture worked; reproduced in the 5,000-connection stress run. Assertrecvbefore sending, or acceptBrokenPipeErroras another reset outcome.