|
13 | 13 | # limitations under the License. |
14 | 14 | # |
15 | 15 |
|
| 16 | +import asyncio |
16 | 17 | import os |
17 | 18 | import sys |
18 | 19 | import tempfile |
@@ -379,3 +380,170 @@ def test_public_card_routes_not_shadowed_by_tenant_mount(self): |
379 | 380 | body = resp.json() |
380 | 381 | assert body["name"] == "Test" |
381 | 382 | assert body["skills"][0]["id"] == "hello_world" |
| 383 | + |
| 384 | + |
| 385 | +class TestWrapA2aOperationTimeout: |
| 386 | + """Verifies default and per-call timeout handling in _wrap_a2a_operation.""" |
| 387 | + |
| 388 | + # pylint: disable=protected-access |
| 389 | + |
| 390 | + def test_legacy_wrap_a2a_operation_timeout(self): |
| 391 | + """_agent_engines._wrap_a2a_operation defaults to None and accepts per-call timeout.""" |
| 392 | + agent_card_json = '{"name": "Test Agent", "version": "1.0.0"}' |
| 393 | + wrapped = _agent_engines._wrap_a2a_operation( |
| 394 | + "on_message_send", agent_card_json |
| 395 | + ) |
| 396 | + fake_engine = mock.MagicMock() |
| 397 | + fake_engine.resource_name = _TEST_AGENT_ENGINE_RESOURCE_NAME |
| 398 | + |
| 399 | + async def _empty_stream(): |
| 400 | + if False: # pylint: disable=using-constant-test |
| 401 | + yield None |
| 402 | + |
| 403 | + mock_a2a_client = mock.MagicMock() |
| 404 | + mock_a2a_client.send_message.side_effect = lambda msg: _empty_stream() |
| 405 | + mock_factory = mock.MagicMock() |
| 406 | + mock_factory.create.return_value = mock_a2a_client |
| 407 | + mock_card = mock.MagicMock() |
| 408 | + mock_card.supported_interfaces = [] |
| 409 | + mock_card.capabilities.streaming = False |
| 410 | + |
| 411 | + with ( |
| 412 | + mock.patch.object( |
| 413 | + _agent_engines.httpx, "AsyncClient" |
| 414 | + ) as mock_async_client, |
| 415 | + mock.patch.object( |
| 416 | + _agent_engines, "AgentCard", return_value=mock_card |
| 417 | + ), |
| 418 | + mock.patch.object(_agent_engines, "AgentInterface"), |
| 419 | + mock.patch.object(_agent_engines, "TransportProtocol"), |
| 420 | + mock.patch.object(_agent_engines, "ClientConfig"), |
| 421 | + mock.patch.object( |
| 422 | + _agent_engines, "ClientFactory", return_value=mock_factory |
| 423 | + ), |
| 424 | + mock.patch.object(_agent_engines, "Message") as mock_message, |
| 425 | + ): |
| 426 | + asyncio.run(wrapped(fake_engine, message_id="m1")) |
| 427 | + mock_async_client.assert_called_once() |
| 428 | + assert mock_async_client.call_args.kwargs["timeout"] is None |
| 429 | + mock_message.assert_called_once_with(message_id="m1") |
| 430 | + |
| 431 | + mock_async_client.reset_mock() |
| 432 | + mock_message.reset_mock() |
| 433 | + mock_card.supported_interfaces = [] |
| 434 | + |
| 435 | + asyncio.run(wrapped(fake_engine, message_id="m2", timeout=120.0)) |
| 436 | + mock_async_client.assert_called_once() |
| 437 | + assert mock_async_client.call_args.kwargs["timeout"] == 120.0 |
| 438 | + mock_message.assert_called_once_with(message_id="m2") |
| 439 | + |
| 440 | + def test_genai_wrap_a2a_operation_v03_per_call_timeout(self): |
| 441 | + """_wrap_a2a_operation_v03 pops per-call timeout and overrides http_options.timeout.""" |
| 442 | + # pylint: disable=g-import-not-at-top |
| 443 | + from vertexai._genai import _agent_engines_utils |
| 444 | + |
| 445 | + # pylint: enable=g-import-not-at-top |
| 446 | + agent_card_json = '{"name": "Test Agent", "version": "1.0.0"}' |
| 447 | + wrapped = _agent_engines_utils._wrap_a2a_operation_v03( |
| 448 | + "on_message_send", agent_card_json |
| 449 | + ) |
| 450 | + fake_engine = mock.MagicMock() |
| 451 | + fake_engine.api_resource.name = _TEST_AGENT_ENGINE_RESOURCE_NAME |
| 452 | + fake_engine.api_client._api_client._http_options.base_url = ( |
| 453 | + "https://us-central1-aiplatform.googleapis.com" |
| 454 | + ) |
| 455 | + fake_engine.api_client._api_client._http_options.api_version = "v1beta1" |
| 456 | + fake_engine.api_client._api_client._http_options.timeout = 30000 |
| 457 | + fake_engine.api_client._api_client._credentials.token = "fake-token" |
| 458 | + |
| 459 | + async def _empty_stream(): |
| 460 | + if False: # pylint: disable=using-constant-test |
| 461 | + yield None |
| 462 | + |
| 463 | + mock_a2a_client = mock.MagicMock() |
| 464 | + mock_a2a_client.send_message.side_effect = lambda msg: _empty_stream() |
| 465 | + mock_factory = mock.MagicMock() |
| 466 | + mock_factory.create.return_value = mock_a2a_client |
| 467 | + mock_card = mock.MagicMock() |
| 468 | + mock_card.preferred_transport = None |
| 469 | + mock_card.capabilities.streaming = False |
| 470 | + |
| 471 | + with ( |
| 472 | + mock.patch.object( |
| 473 | + _agent_engines_utils.httpx, "AsyncClient" |
| 474 | + ) as mock_async_client, |
| 475 | + mock.patch.object( |
| 476 | + _agent_engines_utils, "AgentCard", return_value=mock_card |
| 477 | + ), |
| 478 | + mock.patch.object(_agent_engines_utils, "TransportProtocol"), |
| 479 | + mock.patch.object(_agent_engines_utils, "ClientConfig"), |
| 480 | + mock.patch.object( |
| 481 | + _agent_engines_utils, "ClientFactory", return_value=mock_factory |
| 482 | + ), |
| 483 | + mock.patch.object(_agent_engines_utils, "Message") as mock_message, |
| 484 | + ): |
| 485 | + asyncio.run(wrapped(fake_engine, messageId="m1")) |
| 486 | + mock_async_client.assert_called_once() |
| 487 | + assert mock_async_client.call_args.kwargs["timeout"] == 30.0 |
| 488 | + mock_message.assert_called_once_with(messageId="m1") |
| 489 | + |
| 490 | + mock_async_client.reset_mock() |
| 491 | + mock_message.reset_mock() |
| 492 | + |
| 493 | + asyncio.run(wrapped(fake_engine, messageId="m2", timeout=120.0)) |
| 494 | + mock_async_client.assert_called_once() |
| 495 | + assert mock_async_client.call_args.kwargs["timeout"] == 120.0 |
| 496 | + mock_message.assert_called_once_with(messageId="m2") |
| 497 | + |
| 498 | + def test_genai_wrap_a2a_operation_v10_per_call_timeout(self): |
| 499 | + """_wrap_a2a_operation_v10 pops per-call timeout and overrides http_options.timeout.""" |
| 500 | + # pylint: disable=g-import-not-at-top |
| 501 | + from vertexai._genai import _agent_engines_utils |
| 502 | + |
| 503 | + # pylint: enable=g-import-not-at-top |
| 504 | + agent_card_json = ( |
| 505 | + '{"name": "Test Agent", "version": "1.0.0", ' |
| 506 | + '"supportedInterfaces": [{"url": "http://localhost:8080/a2a/rest/", ' |
| 507 | + '"protocolBinding": "HTTP+JSON", "protocolVersion": "1.0"}]}' |
| 508 | + ) |
| 509 | + wrapped = _agent_engines_utils._wrap_a2a_operation_v10( |
| 510 | + "on_message_send", agent_card_json |
| 511 | + ) |
| 512 | + fake_engine = mock.MagicMock() |
| 513 | + fake_engine.api_resource.name = _TEST_AGENT_ENGINE_RESOURCE_NAME |
| 514 | + fake_engine.api_client._api_client._http_options.base_url = ( |
| 515 | + "https://us-central1-aiplatform.googleapis.com" |
| 516 | + ) |
| 517 | + fake_engine.api_client._api_client._http_options.api_version = "v1beta1" |
| 518 | + fake_engine.api_client._api_client._http_options.timeout = 30000 |
| 519 | + fake_engine.api_client._api_client._credentials.token = "fake-token" |
| 520 | + |
| 521 | + async def _empty_stream(): |
| 522 | + if False: # pylint: disable=using-constant-test |
| 523 | + yield None |
| 524 | + |
| 525 | + mock_a2a_client = mock.MagicMock() |
| 526 | + mock_a2a_client.send_message.side_effect = ( |
| 527 | + lambda req, context=None: _empty_stream() |
| 528 | + ) |
| 529 | + mock_factory = mock.MagicMock() |
| 530 | + mock_factory.create.return_value = mock_a2a_client |
| 531 | + |
| 532 | + with ( |
| 533 | + mock.patch.object( |
| 534 | + _agent_engines_utils.httpx, "AsyncClient" |
| 535 | + ) as mock_async_client, |
| 536 | + mock.patch.object(_agent_engines_utils, "ClientConfig"), |
| 537 | + mock.patch.object( |
| 538 | + _agent_engines_utils, "ClientFactory", return_value=mock_factory |
| 539 | + ), |
| 540 | + ): |
| 541 | + asyncio.run(wrapped(fake_engine, request="req1")) |
| 542 | + mock_async_client.assert_called_once() |
| 543 | + assert mock_async_client.call_args.kwargs["timeout"] == 30.0 |
| 544 | + |
| 545 | + mock_async_client.reset_mock() |
| 546 | + |
| 547 | + asyncio.run(wrapped(fake_engine, request="req2", timeout=120.0)) |
| 548 | + mock_async_client.assert_called_once() |
| 549 | + assert mock_async_client.call_args.kwargs["timeout"] == 120.0 |
0 commit comments