diff --git a/packages/runtime-sdk/tests/workerd-test/asgi-ws-disconnect/tests/test_ws_disconnect.py b/packages/runtime-sdk/tests/workerd-test/asgi-ws-disconnect/tests/test_ws_disconnect.py index d3afd2e1..9e68d6a0 100644 --- a/packages/runtime-sdk/tests/workerd-test/asgi-ws-disconnect/tests/test_ws_disconnect.py +++ b/packages/runtime-sdk/tests/workerd-test/asgi-ws-disconnect/tests/test_ws_disconnect.py @@ -99,10 +99,16 @@ async def _ws_session(path): # Without this the payload arrives as a Blob, which cannot be read # synchronously in the message callback. ws.binaryType = "arraybuffer" + # The app runs on this test's event loop, which pytest-asyncio closes when + # the test ends, cancelling an app still awaiting the disconnect. Its + # server socket then never closes, and workerd keeps the request open + # waiting for it, so wait for the server's close before returning. + closed = _listen(ws, "close") try: yield ws finally: ws.close() + await asyncio.wait_for(closed, TIMEOUT_S) @pytest.mark.asyncio diff --git a/packages/runtime-sdk/tests/workerd-test/sdk/tests/test_sdk.py b/packages/runtime-sdk/tests/workerd-test/sdk/tests/test_sdk.py index 26c26785..3221c6a5 100644 --- a/packages/runtime-sdk/tests/workerd-test/sdk/tests/test_sdk.py +++ b/packages/runtime-sdk/tests/workerd-test/sdk/tests/test_sdk.py @@ -498,8 +498,7 @@ async def test_response_unit_tests(): response_json = Response.json([1, 2, 3]) assert await response_json.text() == "[1, 2, 3]" assert ( - repr(response_json) - == "Response(status=200, status_text='OK', content_type='application/json')" + repr(response_json) == "Response(status=200, content_type='application/json')" ) response_json = Response.from_json([1, 2, 3]) diff --git a/packages/testlib/AGENTS.md b/packages/testlib/AGENTS.md index 235d48b5..83ded2f7 100644 --- a/packages/testlib/AGENTS.md +++ b/packages/testlib/AGENTS.md @@ -50,5 +50,11 @@ via `../packages/testlib` in `[tool.uv.sources]`. suite then fail as "not found in results". - Python 3.12 (Pyodide 0.26.0a2) reports false passes for async in-worker tests; verify failure behaviour on 3.13+. +- The in-worker pytest runs with `--capture=sys`: fd capture breaks on 3.12 + with pytest>=9.2 (`os.fstat` on an unlinked temp file raises ENOENT). +- In-worker tests share the event loop pytest-asyncio creates for each test, + so ASGI app tasks started by the test's own requests are cancelled when the + test ends. Let WebSocket sessions finish their close handshake before + returning, or workerd keeps the server request open. - `pywrangler sync` in tests may need `UV_NATIVE_TLS=1` on hosts with custom CA certificates. diff --git a/packages/testlib/testlib/entrypoint.py b/packages/testlib/testlib/entrypoint.py index 4d280645..ab9271b2 100644 --- a/packages/testlib/testlib/entrypoint.py +++ b/packages/testlib/testlib/entrypoint.py @@ -14,6 +14,12 @@ from .tracebacks import dump_exception +# Workers have no subprocesses whose output needs fd-level capture, and +# pytest>=9.2's fd capture calls os.fstat() on an unlinked temporary file, +# which fails with ENOENT on Python 3.12 (Pyodide 0.26). Forwarded host +# arguments come later, so a host `-s` still takes precedence. +CAPTURE_ARGS = ["--capture=sys"] + class EnvPlugin: def __init__(self, env): @@ -155,7 +161,7 @@ def run_pytest(pytest_args): than via `TestRunner`. Fails the calling test if pytest reports failures. """ with restore_loop(), patch_asyncio(): - exit_code = pytest.main(pytest_args) + exit_code = pytest.main([*CAPTURE_ARGS, *pytest_args]) assert exit_code == 0, f"pytest exit code {exit_code}" @@ -244,7 +250,14 @@ def run_suite(self, suite_name, request=None): redirect_stderr(output), ): exit_code = pytest.main( - ["--pyargs", module, "-p", "no:cacheprovider", *request.pytest_args], + [ + "--pyargs", + module, + "-p", + "no:cacheprovider", + *CAPTURE_ARGS, + *request.pytest_args, + ], plugins=self.plugins(request.keywords), ) if exit_code != 0 and not self.collector.results: