Skip to content

Commit 3d185ad

Browse files
test: extend selected scenarios to REST (a2aproject#1370)
Extend simple streaming, terminal-task cancellation and artifact delivery scenarios to run over HTTP+JSON alongside gRPC, covering both current and legacy handlers. Use a module-scoped Starlette application and HTTPX ASGITransport client. Each scenario binds fresh routes and handler state, with per-test cleanup, while the shared HTTP client closes at module teardown. Validation on Windows / Python 3.10: - Selected scenarios: 20 passed. - Complete scenario module: 122 passed, 1 existing XPASS. - Related end-to-end transport tests: 54 passed. - Verified a single module-level REST fixture setup and teardown. - Ruff lint, formatting, type checks and git diff --check passed. The full suite was not rerun locally for this revision. Earlier Windows runs encountered cross-version subprocess cleanup and FastAPI multiprocessing limitations. Full Linux CI verification remains pending. --------- Co-authored-by: Mateusz Iwaniuk <45974414+Iwaniukooo11@users.noreply.github.com>
1 parent fb6373f commit 3d185ad

1 file changed

Lines changed: 162 additions & 48 deletions

File tree

‎tests/integration/test_scenarios.py‎

Lines changed: 162 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
import collections
33
import logging
44

5-
from typing import Any
5+
from collections.abc import AsyncIterator
6+
from contextlib import AsyncExitStack
7+
from typing import Any, NamedTuple
68

79
import grpc
10+
import httpx
811
import pytest
912
import pytest_asyncio
1013

1114
from a2a.auth.user import User
12-
from a2a.client.client import ClientConfig
15+
from a2a.client.client import Client, ClientConfig
1316
from a2a.client.client_factory import ClientFactory
1417
from a2a.client.errors import A2AClientError
1518
from a2a.helpers.proto_helpers import new_task_from_user_message
@@ -21,10 +24,13 @@
2124
DefaultRequestHandlerV2,
2225
GrpcHandler,
2326
GrpcServerCallContextBuilder,
27+
RequestHandler,
2428
)
2529
from a2a.server.request_handlers.default_request_handler import (
2630
LegacyRequestHandler,
2731
)
32+
from a2a.server.routes.common import DefaultServerCallContextBuilder
33+
from a2a.server.routes.rest_routes import create_rest_routes
2834
from a2a.server.tasks.inmemory_task_store import InMemoryTaskStore
2935
from a2a.types import a2a_pb2_grpc
3036
from a2a.types.a2a_pb2 import (
@@ -54,6 +60,8 @@
5460
TaskNotFoundError,
5561
UnsupportedOperationError,
5662
)
63+
from starlette.applications import Starlette
64+
from starlette.requests import Request
5765

5866

5967
logger = logging.getLogger(__name__)
@@ -101,6 +109,12 @@ def build(self, request: Any) -> ServerCallContext:
101109
)
102110

103111

112+
class MockHttpCallContextBuilder(DefaultServerCallContextBuilder):
113+
def build_user(self, request: Request) -> User:
114+
"""Uses the same test identity as the gRPC setup."""
115+
return MockUser()
116+
117+
104118
class InputRequiredAgent(AgentExecutor):
105119
"""Test agent: 'start' -> INPUT_REQUIRED, anything else -> COMPLETED."""
106120

@@ -167,43 +181,104 @@ def validate_state(event, expected_state):
167181
assert get_state(event) == expected_state
168182

169183

170-
_test_servers = []
184+
class RestSetup(NamedTuple):
185+
app: Starlette
186+
httpx_client: httpx.AsyncClient
187+
188+
189+
@pytest_asyncio.fixture(scope='module')
190+
async def rest_setup() -> AsyncIterator[RestSetup]:
191+
app = Starlette()
192+
async with httpx.AsyncClient(
193+
transport=httpx.ASGITransport(app=app), base_url='http://testserver'
194+
) as httpx_client:
195+
yield RestSetup(app, httpx_client)
196+
197+
198+
_test_resources = AsyncExitStack()
171199

172200

173201
@pytest_asyncio.fixture(autouse=True)
174-
async def cleanup_test_servers():
175-
yield
176-
for server in _test_servers:
177-
await server.stop(None)
178-
_test_servers.clear()
179-
180-
181-
# TODO: Test different transport (e.g. HTTP_JSON hangs for some tests).
182-
async def create_client(handler, agent_card, streaming=False):
183-
server = grpc.aio.server()
184-
port = server.add_insecure_port('[::]:0')
185-
server_address = f'localhost:{port}'
186-
187-
agent_card.supported_interfaces[0].url = server_address
188-
agent_card.supported_interfaces[0].protocol_binding = TransportProtocol.GRPC
189-
190-
servicer = GrpcHandler(
191-
request_handler=handler, context_builder=MockCallContextBuilder()
192-
)
193-
a2a_pb2_grpc.add_A2AServiceServicer_to_server(servicer, server)
194-
await server.start()
195-
_test_servers.append(server)
196-
197-
factory = ClientFactory(
198-
config=ClientConfig(
199-
grpc_channel_factory=grpc.aio.insecure_channel,
200-
supported_protocol_bindings=[TransportProtocol.GRPC],
201-
streaming=streaming,
202-
)
203-
)
204-
client = factory.create(agent_card)
205-
client._server = server # Keep reference to prevent garbage collection
206-
return client
202+
async def cleanup_test_servers() -> AsyncIterator[None]:
203+
try:
204+
yield
205+
finally:
206+
await _test_resources.aclose()
207+
208+
209+
async def close_scenario_handler(handler: RequestHandler) -> None:
210+
"""Stops agent work owned by a scenario, including legacy background work."""
211+
if isinstance(handler, DefaultRequestHandlerV2):
212+
await handler.aclose()
213+
elif isinstance(handler, LegacyRequestHandler):
214+
with handler._running_agents_lock:
215+
producers = list(handler._running_agents.values())
216+
for producer in producers:
217+
producer.cancel()
218+
if producers:
219+
await asyncio.gather(*producers, return_exceptions=True)
220+
background_tasks = list(handler._background_tasks)
221+
if background_tasks:
222+
await asyncio.gather(*background_tasks, return_exceptions=True)
223+
224+
225+
async def create_client(
226+
handler: RequestHandler,
227+
agent_card: AgentCard,
228+
streaming: bool = False,
229+
*,
230+
transport: TransportProtocol = TransportProtocol.GRPC,
231+
rest_setup: RestSetup | None = None,
232+
) -> Client:
233+
"""Creates a client for a scenario test."""
234+
if transport not in (TransportProtocol.GRPC, TransportProtocol.HTTP_JSON):
235+
raise ValueError(f'Unsupported scenario transport: {transport}')
236+
237+
async with AsyncExitStack() as resources:
238+
resources.push_async_callback(close_scenario_handler, handler)
239+
240+
if transport == TransportProtocol.HTTP_JSON:
241+
if rest_setup is None:
242+
raise ValueError(
243+
'REST scenarios require the rest_setup fixture'
244+
)
245+
rest_setup.app.router.routes[:] = create_rest_routes(
246+
request_handler=handler,
247+
context_builder=MockHttpCallContextBuilder(),
248+
)
249+
resources.callback(rest_setup.app.router.routes.clear)
250+
server_address = 'http://testserver'
251+
config = ClientConfig(
252+
httpx_client=rest_setup.httpx_client,
253+
supported_protocol_bindings=[transport],
254+
streaming=streaming,
255+
)
256+
else:
257+
server = grpc.aio.server()
258+
resources.push_async_callback(server.stop, None)
259+
port = server.add_insecure_port('[::]:0')
260+
server_address = f'localhost:{port}'
261+
262+
servicer = GrpcHandler(
263+
request_handler=handler,
264+
context_builder=MockCallContextBuilder(),
265+
)
266+
a2a_pb2_grpc.add_A2AServiceServicer_to_server(servicer, server)
267+
await server.start()
268+
config = ClientConfig(
269+
grpc_channel_factory=grpc.aio.insecure_channel,
270+
supported_protocol_bindings=[transport],
271+
streaming=streaming,
272+
)
273+
274+
agent_card.supported_interfaces[0].url = server_address
275+
agent_card.supported_interfaces[0].protocol_binding = transport
276+
277+
factory = ClientFactory(config=config)
278+
client = factory.create(agent_card)
279+
resources.push_async_callback(client.close)
280+
_test_resources.push_async_callback(resources.pop_all().aclose)
281+
return client
207282

208283

209284
def create_handler(
@@ -232,28 +307,42 @@ def create_handler(
232307
# This also covers test_scenario_7_cancel_terminal_task from test_handler_comparison
233308
@pytest.mark.timeout(2.0)
234309
@pytest.mark.asyncio
310+
@pytest.mark.parametrize(
311+
'transport',
312+
[TransportProtocol.GRPC, TransportProtocol.HTTP_JSON],
313+
ids=['grpc', 'rest'],
314+
)
235315
@pytest.mark.parametrize('use_legacy', [False, True], ids=['v2', 'legacy'])
236316
@pytest.mark.parametrize(
237317
'streaming', [False, True], ids=['blocking', 'streaming']
238318
)
239-
async def test_scenario_1_cancel_terminal_task(use_legacy, streaming):
319+
async def test_scenario_1_cancel_terminal_task(
320+
use_legacy: bool,
321+
streaming: bool,
322+
transport: TransportProtocol,
323+
rest_setup: RestSetup,
324+
) -> None:
240325
class DummyAgentExecutor(AgentExecutor):
241326
async def execute(
242327
self, context: RequestContext, event_queue: EventQueue
243-
):
328+
) -> None:
244329
pass
245330

246331
async def cancel(
247332
self, context: RequestContext, event_queue: EventQueue
248-
):
333+
) -> None:
249334
pass
250335

251336
task_store = InMemoryTaskStore()
252337
handler = create_handler(
253338
DummyAgentExecutor(), use_legacy, task_store=task_store
254339
)
255340
client = await create_client(
256-
handler, agent_card=agent_card(), streaming=streaming
341+
handler,
342+
agent_card=agent_card(),
343+
streaming=streaming,
344+
transport=transport,
345+
rest_setup=rest_setup,
257346
)
258347

259348
task_id = 'terminal-task'
@@ -268,12 +357,19 @@ async def cancel(
268357

269358

270359
@pytest.mark.asyncio
360+
@pytest.mark.parametrize(
361+
'transport',
362+
[TransportProtocol.GRPC, TransportProtocol.HTTP_JSON],
363+
ids=['grpc', 'rest'],
364+
)
271365
@pytest.mark.parametrize('use_legacy', [False, True], ids=['v2', 'legacy'])
272-
async def test_scenario_4_simple_streaming(use_legacy):
366+
async def test_scenario_4_simple_streaming(
367+
use_legacy: bool, transport: TransportProtocol, rest_setup: RestSetup
368+
) -> None:
273369
class DummyAgentExecutor(AgentExecutor):
274370
async def execute(
275371
self, context: RequestContext, event_queue: EventQueue
276-
):
372+
) -> None:
277373
task = new_task_from_user_message(context.message)
278374
task.status.state = TaskState.TASK_STATE_WORKING
279375
await event_queue.enqueue_event(task)
@@ -287,12 +383,16 @@ async def execute(
287383

288384
async def cancel(
289385
self, context: RequestContext, event_queue: EventQueue
290-
):
386+
) -> None:
291387
pass
292388

293389
handler = create_handler(DummyAgentExecutor(), use_legacy)
294390
client = await create_client(
295-
handler, agent_card=agent_card(), streaming=True
391+
handler,
392+
agent_card=agent_card(),
393+
streaming=True,
394+
transport=transport,
395+
rest_setup=rest_setup,
296396
)
297397
msg = Message(
298398
message_id='test-msg', role=Role.ROLE_USER, parts=[Part(text='hello')]
@@ -1576,15 +1676,25 @@ async def cancel(
15761676
# Scenario: Publish ArtifactUpdateEvent
15771677
@pytest.mark.timeout(2.0)
15781678
@pytest.mark.asyncio
1679+
@pytest.mark.parametrize(
1680+
'transport',
1681+
[TransportProtocol.GRPC, TransportProtocol.HTTP_JSON],
1682+
ids=['grpc', 'rest'],
1683+
)
15791684
@pytest.mark.parametrize('use_legacy', [False, True], ids=['v2', 'legacy'])
15801685
@pytest.mark.parametrize(
15811686
'streaming', [False, True], ids=['blocking', 'streaming']
15821687
)
1583-
async def test_scenario_publish_artifact(use_legacy, streaming):
1688+
async def test_scenario_publish_artifact(
1689+
use_legacy: bool,
1690+
streaming: bool,
1691+
transport: TransportProtocol,
1692+
rest_setup: RestSetup,
1693+
) -> None:
15841694
class ArtifactAgent(AgentExecutor):
15851695
async def execute(
15861696
self, context: RequestContext, event_queue: EventQueue
1587-
):
1697+
) -> None:
15881698
task = new_task_from_user_message(context.message)
15891699
task.status.state = TaskState.TASK_STATE_WORKING
15901700
await event_queue.enqueue_event(task)
@@ -1607,12 +1717,16 @@ async def execute(
16071717

16081718
async def cancel(
16091719
self, context: RequestContext, event_queue: EventQueue
1610-
):
1720+
) -> None:
16111721
pass
16121722

16131723
handler = create_handler(ArtifactAgent(), use_legacy)
16141724
client = await create_client(
1615-
handler, agent_card=agent_card(), streaming=streaming
1725+
handler,
1726+
agent_card=agent_card(),
1727+
streaming=streaming,
1728+
transport=transport,
1729+
rest_setup=rest_setup,
16161730
)
16171731

16181732
msg = Message(

0 commit comments

Comments
 (0)