Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
test(python-sdk): drop httpcore-era stream reader tests after the pyqwest migration (#1690)
cursor[bot]fc3496120516
diff --git a/packages/python-sdk/tests/bugs/test_envelope_decode.py b/packages/python-sdk/tests/bugs/test_envelope_decode.pydeleted file mode 100644index 6bc359547..000000000--- a/packages/python-sdk/tests/bugs/test_envelope_decode.py+++ /dev/null@@ -1,45 +0,0 @@-from e2b.sandbox.commands.command_handle import CommandExitException--import pytest--from e2b import Sandbox---class Desktop(Sandbox):- default_template = "desktop"-- @staticmethod- def _wrap_pyautogui_code(code: str):- return f"""-import pyautogui-import os-import Xlib.display--display = Xlib.display.Display(os.environ["DISPLAY"])-pyautogui._pyautogui_x11._display = display--{code}-exit(0)-"""-- def pyautogui(self, pyautogui_code: str):- code_path = "/home/user/code-4f3a0850-1a83-47b2-8402-67b039a084ae.py"- print(code_path)-- code = self._wrap_pyautogui_code(pyautogui_code)-- self.files.write(code_path, code)-- self.commands.run(f"python {code_path}")---def test_envelope_decode():- with Desktop(timeout=30) as desktop:- for _ in range(10):- with pytest.raises(CommandExitException):- desktop.pyautogui(- """-pyautogui.write("Hello, ")-"""- )diff --git a/packages/python-sdk/tests/test_file_stream_reader.py b/packages/python-sdk/tests/test_file_stream_reader.pyindex f9f340b9f..f15cedda2 100644--- a/packages/python-sdk/tests/test_file_stream_reader.py+++ b/packages/python-sdk/tests/test_file_stream_reader.py@@ -1,11 +1,12 @@"""Unit tests for the streamed-read helpers.-These exercise connection lifecycle (consume / context manager / explicit-close / idle timeout / abandonment) without hitting a real sandbox, using a-local chunked HTTP server.+These exercise the readers' own lifecycle (consume / context manager /+explicit close / idle timeout) against a local chunked HTTP server. They+assert on the reader's contract — the underlying response is closed — rather+than on connection-pool internals, which are private to the transport and+absent on the pyqwest transports the SDK ships."""-import asyncioimport socketimport threadingimport time@@ -29,9 +30,9 @@ def _start_chunked_server() -> int:"""Start a one-shot HTTP server that replies with a chunked body.- When ``stall_before`` is set, the server sleeps ``stall_seconds`` before- sending that chunk index, so a reader with a shorter idle timeout times out.- Returns the server's port.+ When ``stall_before`` is not None, the server sleeps ``stall_seconds``+ before sending that chunk index, so a reader with a shorter idle timeout+ times out. Returns the server's port."""sock = socket.socket()sock.bind(("127.0.0.1", 0))@@ -49,7 +50,7 @@ def _start_chunked_server(b"Transfer-Encoding: chunked\r\n\r\n")for idx, chunk in enumerate(CHUNKS):- if stall_before is not None and idx == stall_before:+ if idx == stall_before:time.sleep(stall_seconds)conn.sendall(f"{len(chunk):x}\r\n".encode() + chunk + b"\r\n")conn.sendall(b"0\r\n\r\n")@@ -63,163 +64,119 @@ def _start_chunked_server(return port-def _active_connections(client) -> int:- # Count connections that are still checked out (a leaked/in-use stream),- # not the total pool size. A fully consumed stream returns its connection- # to the pool, where it may linger as an idle keep-alive entry until the- # server-side close is observed; that lingering idle connection is not a- # leak. Asserting on total pool size makes this racy under load (the basis- # of a CI flake); counting only non-idle connections is deterministic.- return sum(1 for conn in client._transport._pool.connections if not conn.is_idle())+def _start_truncating_server() -> int:+ """One-shot server that sends the head and one chunk, then drops the+ connection without the terminating zero-length chunk, so a mid-body read+ raises a protocol error."""+ sock = socket.socket()+ sock.bind(("127.0.0.1", 0))+ sock.listen(1)+ port = sock.getsockname()[1]++ def serve():+ try:+ conn, _ = sock.accept()+ while b"\r\n\r\n" not in conn.recv(65536):+ pass+ conn.sendall(+ b"HTTP/1.1 200 OK\r\n"+ b"Content-Type: application/octet-stream\r\n"+ b"Transfer-Encoding: chunked\r\n\r\n"+ )+ chunk = CHUNKS[0]+ conn.sendall(f"{len(chunk):x}\r\n".encode() + chunk + b"\r\n")+ conn.close()+ except OSError:+ pass+ finally:+ sock.close()++ threading.Thread(target=serve, daemon=True).start()+ return port-def _open_stream(client, port, read_timeout: Optional[float] = None):+def _open_stream(client, port):request = client.build_request("GET", f"http://127.0.0.1:{port}/files", timeout=httpx.Timeout(5.0))- if read_timeout is not None:- # Mirror the SDK: the per-chunk `read` timeout bounds idle gaps.- request.extensions["timeout"]["read"] = read_timeoutreturn client.send(request, stream=True)def test_sync_full_consume_releases_connection():with httpx.Client() as client:port = _start_chunked_server()- reader = FileStreamReader(_open_stream(client, port))+ response = _open_stream(client, port)+ reader = FileStreamReader(response)assert b"".join(reader) == EXPECTED- assert _active_connections(client) == 0+ assert response.is_closeddef test_sync_context_manager_releases_on_exit():with httpx.Client() as client:port = _start_chunked_server()- with FileStreamReader(_open_stream(client, port)) as reader:+ response = _open_stream(client, port)+ with FileStreamReader(response) as reader:assert next(iter(reader)) == CHUNKS[0]- # Exiting the context releases the connection even though the stream+ assert not response.is_closed+ # Exiting the context releases the response even though the stream# was only partially consumed.- assert _active_connections(client) == 0+ assert response.is_closeddef test_sync_close_is_idempotent():with httpx.Client() as client:port = _start_chunked_server()- reader = FileStreamReader(_open_stream(client, port))+ response = _open_stream(client, port)+ reader = FileStreamReader(response)reader.close()reader.close()- assert _active_connections(client) == 0+ assert response.is_closed-def test_sync_idle_timeout_releases_connection():+def test_sync_read_error_releases_response():with httpx.Client() as client:- # The server stalls before the second chunk for longer than the- # reader's idle (read) timeout.- port = _start_chunked_server(stall_before=1, stall_seconds=0.5)- reader = FileStreamReader(_open_stream(client, port, read_timeout=0.05))+ port = _start_truncating_server()+ response = _open_stream(client, port)+ reader = FileStreamReader(response)it = iter(reader)- assert next(it)- # The stalled read trips the idle timeout, which propagates and- # releases the connection.- with pytest.raises(httpx.ReadTimeout):+ assert next(it) == CHUNKS[0]+ # A mid-body error propagates and the reader releases the response.+ with pytest.raises(httpx.RemoteProtocolError):next(it)- assert _active_connections(client) == 0---def test_sync_slow_consumer_does_not_trip_idle_timeout():- # The server sends every chunk promptly; the consumer then pauses far- # longer than the read (idle) timeout between iterations. httpx's read- # timeout only counts while it's waiting on the wire, so a slow consumer- # must not trip it (parity with the JS wire-only idle timeout).- with httpx.Client() as client:- port = _start_chunked_server()- reader = FileStreamReader(_open_stream(client, port, read_timeout=0.05))- out = []- for chunk in reader:- out.append(chunk)- time.sleep(0.2)- assert b"".join(out) == EXPECTED---def test_sync_abandoned_reader_is_reclaimed_on_client_close():- client = httpx.Client()- port = _start_chunked_server()- reader = FileStreamReader(_open_stream(client, port))- assert _active_connections(client) == 1-- # The sync reader has no GC safety net: dropping it without closing keeps- # the connection checked out (an idle timeout would reclaim a stalled one).- del reader- assert _active_connections(client) == 1-- # Closing the client reclaims the abandoned connection.- client.close()- assert _active_connections(client) == 0+ assert response.is_closedasync def test_async_full_consume_releases_connection():async with httpx.AsyncClient() as client:port = _start_chunked_server()request = client.build_request("GET", f"http://127.0.0.1:{port}/files")- reader = AsyncFileStreamReader(await client.send(request, stream=True))+ response = await client.send(request, stream=True)+ reader = AsyncFileStreamReader(response)collected = b"".join([chunk async for chunk in reader])assert collected == EXPECTED- assert _active_connections(client) == 0+ assert response.is_closedasync def test_async_context_manager_releases_on_exit():async with httpx.AsyncClient() as client:port = _start_chunked_server()request = client.build_request("GET", f"http://127.0.0.1:{port}/files")- async with AsyncFileStreamReader(- await client.send(request, stream=True)- ) as reader:+ response = await client.send(request, stream=True)+ async with AsyncFileStreamReader(response) as reader:assert await reader.__anext__() == CHUNKS[0]- assert _active_connections(client) == 0+ assert not response.is_closed+ assert response.is_closedasync def test_async_aclose_is_idempotent():async with httpx.AsyncClient() as client:port = _start_chunked_server()request = client.build_request("GET", f"http://127.0.0.1:{port}/files")- reader = AsyncFileStreamReader(await client.send(request, stream=True))+ response = await client.send(request, stream=True)+ reader = AsyncFileStreamReader(response)await reader.aclose()await reader.aclose()- assert _active_connections(client) == 0---async def test_async_idle_timeout_releases_connection():- async with httpx.AsyncClient() as client:- port = _start_chunked_server(stall_before=1, stall_seconds=0.5)- request = client.build_request(- "GET", f"http://127.0.0.1:{port}/files", timeout=httpx.Timeout(5.0)- )- request.extensions["timeout"]["read"] = 0.05- reader = AsyncFileStreamReader(await client.send(request, stream=True))- it = reader.__aiter__()- assert await it.__anext__()- # The stalled read trips the idle timeout, which propagates and- # releases the connection.- with pytest.raises(httpx.ReadTimeout):- await it.__anext__()- assert _active_connections(client) == 0---async def test_async_abandoned_reader_is_reclaimed_on_client_close():- client = httpx.AsyncClient()- port = _start_chunked_server()- request = client.build_request("GET", f"http://127.0.0.1:{port}/files")- reader = AsyncFileStreamReader(await client.send(request, stream=True))- assert _active_connections(client) == 1-- # The async reader has no GC safety net: dropping it without closing keeps- # the connection checked out (releasing one requires awaiting aclose()).- del reader- await asyncio.sleep(0.05)- assert _active_connections(client) == 1-- # Closing the client reclaims the abandoned connection.- await client.aclose()- assert _active_connections(client) == 0+ assert response.is_closedasync def test_async_reader_explicit_idle_timeout_bounds_each_read():@@ -228,25 +185,23 @@ async def test_async_reader_explicit_idle_timeout_bounds_each_read():async with httpx.AsyncClient() as client:port = _start_chunked_server(stall_before=1, stall_seconds=0.5)request = client.build_request("GET", f"http://127.0.0.1:{port}/files")- reader = AsyncFileStreamReader(- await client.send(request, stream=True), idle_timeout=0.05- )+ response = await client.send(request, stream=True)+ reader = AsyncFileStreamReader(response, idle_timeout=0.05)assert await reader.__anext__() == CHUNKS[0]with pytest.raises(httpx.ReadTimeout):await reader.__anext__()- assert _active_connections(client) == 0+ assert response.is_closedasync def test_async_reader_explicit_idle_timeout_allows_prompt_chunks():async with httpx.AsyncClient() as client:port = _start_chunked_server()request = client.build_request("GET", f"http://127.0.0.1:{port}/files")- reader = AsyncFileStreamReader(- await client.send(request, stream=True), idle_timeout=5.0- )+ response = await client.send(request, stream=True)+ reader = AsyncFileStreamReader(response, idle_timeout=5.0)collected = b"".join([chunk async for chunk in reader])assert collected == EXPECTED- assert _active_connections(client) == 0+ assert response.is_closedif __name__ == "__main__":