Skip to content

Commit 8984749

Browse files
committed
test: add tests for BaseApiClient partial initialization
1 parent 5c5d3c2 commit 8984749

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

google/genai/tests/client/test_client_close.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"""Tests for closing the clients and context managers."""
1717
import asyncio
1818
from unittest import mock
19-
19+
import gc
2020
from google.oauth2 import credentials
2121
import pytest
2222
try:
@@ -194,3 +194,26 @@ async def run():
194194
assert async_client._api_client._aiohttp_session.closed
195195

196196
asyncio.run(run())
197+
198+
199+
def test_partial_init_validation_error_sync_safety():
200+
"""Verifies that BaseApiClient.close() does not crash on a partially initialized client."""
201+
202+
with pytest.raises(ValueError):
203+
api_client.BaseApiClient(api_key=None, vertexai=False)
204+
205+
# force GC to trigger __del__
206+
gc.collect()
207+
208+
@pytest.mark.asyncio
209+
async def test_partial_init_validation_error_async_safety():
210+
"""Verifies that BaseApiClient.aclose() does not crash the event loop on a partially initialized client."""
211+
212+
with pytest.raises(ValueError):
213+
api_client.BaseApiClient(api_key=None, vertexai=False)
214+
215+
# force GC to trigger __del__
216+
gc.collect()
217+
218+
# make sure the event loop runs to completion
219+
await asyncio.sleep(0)

0 commit comments

Comments
 (0)