diff --git a/tests/test_apps/conftest.py b/tests/test_apps/conftest.py index d95469fb..0c0bba9b 100644 --- a/tests/test_apps/conftest.py +++ b/tests/test_apps/conftest.py @@ -1,11 +1,18 @@ """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): @@ -13,3 +20,36 @@ def offline_gpu_catalog(monkeypatch): 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() diff --git a/tests/test_apps/test_retry.py b/tests/test_apps/test_retry.py index 95aa5dd8..7f1c781d 100644 --- a/tests/test_apps/test_retry.py +++ b/tests/test_apps/test_retry.py @@ -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)