feat: Improve asyncio loop handling and test cleanup

This CL enhances asyncio event loop management and test isolation.

-   **BigQuery Analytics Plugin:** Ensure the asyncio event loop is consistently closed within the BigQuery analytics plugin. This prevents potential resource leaks. Add checks to handle potential deadlocks in Python 3.13+ when creating loops during interpreter shutdown.
-   **Test Thread Pool Cleanup:** Introduce a pytest fixture (`cleanup_thread_pools`) to automatically shut down and clear all tool-related thread pools after each test run in `test_functions_thread_pool.py`. This improves test isolation and prevents order-dependent test failures.
-   **Streaming Test Loop Restoration:** Refactor event loop handling in `test_streaming.py`. A new `_run_with_loop` method is introduced in the custom test runners to create a temporary event loop for each test execution, run the coroutine, and crucially, restore the original event loop afterwards. This prevents tests from interfering with each other's loop state.
-   **Resource Closure:** Ensure services are closed properly in tests by adding `await service.close()` in `test_service_factory.py` and using `async with session_service` in `test_session_service.py`.

PiperOrigin-RevId: 863305565
This commit is contained in:
Google Team Member
2026-01-30 10:47:09 -08:00
committed by Copybara-Service
parent 585ebfdac7
commit 00aba2d884
6 changed files with 2334 additions and 2241 deletions
@@ -168,7 +168,8 @@ async def test_create_session_service_respects_app_name_mapping(
assert (agent_dir / ".adk" / "session.db").exists()
def test_create_session_service_fallbacks_to_database(
@pytest.mark.asyncio
async def test_create_session_service_fallbacks_to_database(
tmp_path: Path, monkeypatch
):
registry = mock.create_autospec(ServiceRegistry, instance=True, spec_set=True)
@@ -189,6 +190,7 @@ def test_create_session_service_fallbacks_to_database(
agents_dir=str(tmp_path),
echo=True,
)
await service.close()
def test_create_artifact_service_uses_registry(tmp_path: Path, monkeypatch):