|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import json |
| 5 | +from typing import Any |
| 6 | +from unittest.mock import AsyncMock, MagicMock |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from acp.connection import Connection |
| 11 | +from acp.exceptions import RequestError |
| 12 | + |
| 13 | + |
| 14 | +async def _noop_handler(method: str, params: Any, is_notification: bool) -> Any: |
| 15 | + return None |
| 16 | + |
| 17 | + |
| 18 | +def _make_connection( |
| 19 | + *, |
| 20 | + limit: int = 128, |
| 21 | + receive_timeout: float | None = None, |
| 22 | +) -> tuple[Connection, asyncio.StreamReader]: |
| 23 | + reader = asyncio.StreamReader(limit=limit) |
| 24 | + transport = MagicMock() |
| 25 | + transport.is_closing.return_value = False |
| 26 | + protocol = AsyncMock() |
| 27 | + writer = asyncio.StreamWriter(transport, protocol, reader, asyncio.get_running_loop()) |
| 28 | + conn = Connection(_noop_handler, writer, reader, listening=False, receive_timeout=receive_timeout) |
| 29 | + return conn, reader |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.asyncio |
| 33 | +async def test_receive_loop_recovers_from_oversized_frame(caplog: pytest.LogCaptureFixture) -> None: |
| 34 | + conn, reader = _make_connection(limit=128) |
| 35 | + processed: list[str] = [] |
| 36 | + |
| 37 | + async def tracking_process(message: dict[str, Any]) -> None: |
| 38 | + processed.append(message["method"]) |
| 39 | + |
| 40 | + conn._process_message = tracking_process # type: ignore[method-assign] |
| 41 | + oversized = {"jsonrpc": "2.0", "method": "too-large", "params": {"data": "X" * 256}} |
| 42 | + survivor = {"jsonrpc": "2.0", "method": "survivor"} |
| 43 | + reader.feed_data(json.dumps(oversized).encode() + b"\n" + json.dumps(survivor).encode() + b"\n") |
| 44 | + reader.feed_eof() |
| 45 | + |
| 46 | + with caplog.at_level("WARNING"): |
| 47 | + await conn._receive_loop() |
| 48 | + await conn.close() |
| 49 | + |
| 50 | + assert processed == ["survivor"] |
| 51 | + assert any("oversized JSON-RPC frame" in record.message for record in caplog.records) |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.asyncio |
| 55 | +async def test_receive_loop_recovers_from_consecutive_oversized_frames() -> None: |
| 56 | + conn, reader = _make_connection(limit=128) |
| 57 | + processed: list[str] = [] |
| 58 | + |
| 59 | + async def tracking_process(message: dict[str, Any]) -> None: |
| 60 | + processed.append(message["method"]) |
| 61 | + |
| 62 | + conn._process_message = tracking_process # type: ignore[method-assign] |
| 63 | + for index in range(2): |
| 64 | + oversized = {"jsonrpc": "2.0", "method": f"too-large-{index}", "params": {"data": "Y" * 256}} |
| 65 | + reader.feed_data(json.dumps(oversized).encode() + b"\n") |
| 66 | + survivor = {"jsonrpc": "2.0", "method": "survivor"} |
| 67 | + reader.feed_data(json.dumps(survivor).encode() + b"\n") |
| 68 | + reader.feed_eof() |
| 69 | + |
| 70 | + await conn._receive_loop() |
| 71 | + await conn.close() |
| 72 | + |
| 73 | + assert processed == ["survivor"] |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.asyncio |
| 77 | +async def test_receive_loop_handles_eof_during_oversized_frame() -> None: |
| 78 | + conn, reader = _make_connection(limit=64) |
| 79 | + reader.feed_data(b"X" * 256) |
| 80 | + reader.feed_eof() |
| 81 | + |
| 82 | + await conn._receive_loop() |
| 83 | + await conn.close() |
| 84 | + |
| 85 | + assert conn._disconnected is True |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.asyncio |
| 89 | +async def test_receive_loop_keeps_timeout_semantics() -> None: |
| 90 | + conn, _reader = _make_connection(receive_timeout=0.01) |
| 91 | + |
| 92 | + with pytest.raises(RequestError) as exc_info: |
| 93 | + await conn._receive_loop() |
| 94 | + await conn.close() |
| 95 | + |
| 96 | + exc = exc_info.value |
| 97 | + assert isinstance(exc, RequestError) |
| 98 | + assert str(exc) == "Internal error" |
| 99 | + assert exc.data == {"details": "Agent timeout"} |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.asyncio |
| 103 | +async def test_receive_loop_does_not_swallow_unrelated_reader_error() -> None: |
| 104 | + conn, reader = _make_connection() |
| 105 | + reader.set_exception(ValueError("reader failed")) |
| 106 | + |
| 107 | + with pytest.raises(ValueError, match="reader failed"): |
| 108 | + await conn._receive_loop() |
| 109 | + await conn.close() |
0 commit comments