forked from agentclientprotocol/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_connection_recovery.py
More file actions
176 lines (126 loc) · 5.89 KB
/
test_connection_recovery.py
File metadata and controls
176 lines (126 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""Tests for connection recovery from oversized messages (Issue #62).
Verifies that the connection's _receive_loop gracefully handles messages
that exceed the StreamReader buffer limit by catching LimitOverrunError
and continuing to process subsequent valid messages.
"""
from __future__ import annotations
import asyncio
import json
from typing import Any
from unittest.mock import AsyncMock, MagicMock
import pytest
from acp.connection import Connection
async def _noop_handler(method: str, params: Any, is_notification: bool) -> Any:
return None
def _make_connection(
limit: int = 4096,
handler: Any = None,
) -> tuple[Connection, asyncio.StreamReader]:
"""Create a Connection with a StreamReader of the given buffer limit."""
reader = asyncio.StreamReader(limit=limit)
transport = MagicMock()
transport.is_closing.return_value = False
protocol = AsyncMock()
writer = asyncio.StreamWriter(transport, protocol, reader, asyncio.get_running_loop())
conn = Connection(handler or _noop_handler, writer, reader, listening=False)
return conn, reader
@pytest.mark.asyncio
async def test_receive_loop_recovers_from_oversized_message() -> None:
"""The receive loop should skip oversized messages and continue processing."""
conn, reader = _make_connection(limit=128)
# Track which messages were processed
processed: list[dict] = []
original_process = conn._process_message
async def tracking_process(message: dict[str, Any]) -> None:
processed.append(message)
await original_process(message)
conn._process_message = tracking_process # type: ignore[assignment]
# Feed an oversized message (exceeds 128-byte limit)
oversized_msg = json.dumps({"jsonrpc": "2.0", "method": "test.oversized", "params": {"data": "X" * 200}})
reader.feed_data((oversized_msg + "\n").encode())
# Feed a normal message after the oversized one
normal_msg = json.dumps({"jsonrpc": "2.0", "method": "test.normal", "params": {}})
reader.feed_data((normal_msg + "\n").encode())
# Signal EOF
reader.feed_eof()
# Run the receive loop
await conn._receive_loop()
# The oversized message should have been skipped, but the normal one processed
assert len(processed) == 1
assert processed[0]["method"] == "test.normal"
await conn.close()
@pytest.mark.asyncio
async def test_receive_loop_does_not_crash_on_limit_overrun() -> None:
"""The receive loop must not raise LimitOverrunError or ValueError."""
conn, reader = _make_connection(limit=64)
# Feed only an oversized message and then EOF
oversized = "X" * 200 + "\n"
reader.feed_data(oversized.encode())
reader.feed_eof()
# This should NOT raise — the error should be caught and logged
await conn._receive_loop()
await conn.close()
@pytest.mark.asyncio
async def test_receive_loop_handles_normal_messages() -> None:
"""Sanity check: normal messages within the limit are processed correctly."""
processed: list[dict] = []
conn, reader = _make_connection(limit=4096)
original_process = conn._process_message
async def tracking_process(message: dict[str, Any]) -> None:
processed.append(message)
await original_process(message)
conn._process_message = tracking_process # type: ignore[assignment]
msg1 = json.dumps({"jsonrpc": "2.0", "method": "test.one", "params": {}})
msg2 = json.dumps({"jsonrpc": "2.0", "method": "test.two", "params": {}})
reader.feed_data((msg1 + "\n" + msg2 + "\n").encode())
reader.feed_eof()
await conn._receive_loop()
assert len(processed) == 2
assert processed[0]["method"] == "test.one"
assert processed[1]["method"] == "test.two"
await conn.close()
@pytest.mark.asyncio
async def test_receive_loop_recovers_from_multiple_oversized_messages() -> None:
"""Multiple consecutive oversized messages should all be skipped gracefully."""
conn, reader = _make_connection(limit=128)
processed: list[dict] = []
original_process = conn._process_message
async def tracking_process(message: dict[str, Any]) -> None:
processed.append(message)
await original_process(message)
conn._process_message = tracking_process # type: ignore[assignment]
# Feed two oversized messages
for i in range(2):
oversized = json.dumps({"jsonrpc": "2.0", "method": f"test.big{i}", "params": {"data": "Y" * 200}})
reader.feed_data((oversized + "\n").encode())
# Feed a normal message after both oversized ones
normal = json.dumps({"jsonrpc": "2.0", "method": "test.survivor", "params": {}})
reader.feed_data((normal + "\n").encode())
reader.feed_eof()
await conn._receive_loop()
# Only the normal message should be processed
assert len(processed) == 1
assert processed[0]["method"] == "test.survivor"
await conn.close()
@pytest.mark.asyncio
async def test_receive_loop_logs_warning_on_oversized_message(caplog: pytest.LogCaptureFixture) -> None:
"""A warning should be logged when an oversized message is skipped."""
conn, reader = _make_connection(limit=64)
oversized = json.dumps({"jsonrpc": "2.0", "method": "test.huge", "params": {"data": "Z" * 200}})
reader.feed_data((oversized + "\n").encode())
reader.feed_eof()
with caplog.at_level("WARNING"):
await conn._receive_loop()
assert any(
"oversized" in record.message.lower() or "buffer limit" in record.message.lower() for record in caplog.records
)
await conn.close()
@pytest.mark.asyncio
async def test_unrelated_value_error_is_not_swallowed() -> None:
"""A ValueError that is NOT from a limit overrun should propagate."""
conn, reader = _make_connection(limit=4096)
# Inject an unrelated ValueError into the reader
reader.set_exception(ValueError("completely unrelated error"))
with pytest.raises(ValueError, match="completely unrelated"):
await conn._receive_loop()
await conn.close()