-
Notifications
You must be signed in to change notification settings - Fork 1
Ensure Gemini Cloud Project sessions are closed #633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
matdev83
wants to merge
2
commits into
dev
Choose a base branch
from
codex/fix-improper-resource-management-bug-qmjf4d
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
tests/unit/connectors/test_gemini_cloud_project_resource_management.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import asyncio | ||
| from typing import Any | ||
|
|
||
| import httpx | ||
| import pytest | ||
|
|
||
| from src.connectors.gemini_cloud_project import GeminiCloudProjectConnector | ||
| from src.core.config.app_config import AppConfig | ||
| from src.core.services.translation_service import TranslationService | ||
|
|
||
|
|
||
| class _DummyResponse: | ||
| def __init__(self, status_code: int = 200, json_data: dict[str, Any] | None = None) -> None: | ||
| self.status_code = status_code | ||
| self._json_data = json_data or {} | ||
| self.text = "" | ||
|
|
||
| def json(self) -> dict[str, Any]: | ||
| return self._json_data | ||
|
|
||
|
|
||
| class _DummySession: | ||
| def __init__(self) -> None: | ||
| self.closed = False | ||
|
|
||
| def close(self) -> None: | ||
| self.closed = True | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def connector() -> GeminiCloudProjectConnector: | ||
| cfg = AppConfig() | ||
| client = httpx.AsyncClient() | ||
| backend = GeminiCloudProjectConnector( | ||
| client, | ||
| cfg, | ||
| translation_service=TranslationService(), | ||
| gcp_project_id="test-project", | ||
| ) | ||
| backend.gemini_api_base_url = "https://example.com" | ||
| return backend | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_validate_project_access_closes_session( | ||
| connector: GeminiCloudProjectConnector, monkeypatch: pytest.MonkeyPatch | ||
| ) -> None: | ||
| class _Session(_DummySession): | ||
| def request(self, *args: Any, **kwargs: Any) -> _DummyResponse: | ||
| return _DummyResponse( | ||
| json_data={"cloudaicompanionProject": {"id": connector.gcp_project_id}} | ||
| ) | ||
|
|
||
| session = _Session() | ||
|
|
||
| async def _immediate_to_thread(func: Any, *args: Any, **kwargs: Any) -> Any: | ||
| return func(*args, **kwargs) | ||
|
|
||
| monkeypatch.setattr(connector, "_get_adc_authorized_session", lambda: session) | ||
| monkeypatch.setattr(asyncio, "to_thread", _immediate_to_thread) | ||
|
|
||
| await connector._validate_project_access() | ||
|
|
||
| assert session.closed is True | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_perform_health_check_closes_session( | ||
| connector: GeminiCloudProjectConnector, monkeypatch: pytest.MonkeyPatch | ||
| ) -> None: | ||
| class _Credentials: | ||
| def __init__(self) -> None: | ||
| self.token = "token" | ||
|
|
||
| def refresh(self, request: Any) -> None: # pragma: no cover - simple stub | ||
| self.token = "new-token" | ||
|
|
||
| class _Session(_DummySession): | ||
| def __init__(self) -> None: | ||
| super().__init__() | ||
| self.credentials = _Credentials() | ||
|
|
||
| async def _fake_get(url: str, headers: dict[str, str], timeout: float) -> Any: | ||
| return _DummyResponse(status_code=200) | ||
|
|
||
| session = _Session() | ||
|
|
||
| monkeypatch.setattr(connector, "_get_adc_authorized_session", lambda: session) | ||
| monkeypatch.setattr(connector.client, "get", _fake_get) | ||
|
|
||
| result = await connector._perform_health_check() | ||
|
|
||
| assert result is True | ||
| assert session.closed is True | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure the AsyncClient is closed in the fixture
httpx.AsyncClientkeeps background resources alive untilaclose()runs; leaving it open in the fixture causes resource leaks and runtime warnings after the tests complete. Convert the fixture to an async fixture thatasync with-closes the client so each test frees the session.Remember to import
pytest_asyncioalongside the other pytest imports.📝 Committable suggestion
🤖 Prompt for AI Agents