Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
6 changes: 6 additions & 0 deletions packages/testlib/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
17 changes: 15 additions & 2 deletions packages/testlib/testlib/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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}"


Expand Down Expand Up @@ -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:
Expand Down
Loading