Skip to content

Commit d7b180d

Browse files
chore: shorten duration of connector unit tests (#444)
test_connector.py and test_async_connector.py were taking around a minute to run. The reason for this is that the AlloyDB Admin API client sets a default deadline of 60s to retry failed API requests. This increases the time for certain unit tests, which send actual API requests. This PR overrides the timeout to 1s for those unit tests, to speed up their execution.
1 parent 984dd80 commit d7b180d

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

tests/unit/test_async_connector.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from typing import Union
1717

1818
from google.api_core.exceptions import RetryError
19+
from google.api_core.retry.retry_unary_async import AsyncRetry
1920
from mock import patch
2021
from mocks import FakeAlloyDBClient
2122
from mocks import FakeConnectionInfo
@@ -24,6 +25,7 @@
2425

2526
from google.cloud.alloydb.connector import AsyncConnector
2627
from google.cloud.alloydb.connector import IPTypes
28+
from google.cloud.alloydb.connector.client import AlloyDBClient
2729
from google.cloud.alloydb.connector.exceptions import ClosedConnectorError
2830
from google.cloud.alloydb.connector.exceptions import IPTypeNotFoundError
2931
from google.cloud.alloydb.connector.instance import RefreshAheadCache
@@ -335,6 +337,19 @@ async def test_Connector_remove_cached_bad_instance(
335337
"""
336338
instance_uri = "projects/test-project/locations/test-region/clusters/test-cluster/instances/bad-test-instance"
337339
async with AsyncConnector(credentials=credentials) as connector:
340+
# The timeout of AlloyDB API methods is set to 60s by default.
341+
# We override it to 1s to shorten the duration of the test.
342+
connector._client = AlloyDBClient(
343+
"alloydb.googleapis.com", "test-project", credentials, driver="asyncpg"
344+
)
345+
transport = connector._client._client.transport
346+
transport._wrapped_methods[transport.get_connection_info]._retry = AsyncRetry(
347+
timeout=1
348+
)
349+
transport._wrapped_methods[
350+
transport.generate_client_certificate
351+
]._retry = AsyncRetry(timeout=1)
352+
338353
with pytest.raises(RetryError):
339354
await connector.connect(instance_uri, "asyncpg")
340355
assert instance_uri not in connector._cache

tests/unit/test_connector.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from typing import Union
1818

1919
from google.api_core.exceptions import RetryError
20+
from google.api_core.retry.retry_unary import Retry
2021
from mock import patch
2122
from mocks import FakeAlloyDBClient
2223
from mocks import FakeCredentials
@@ -25,6 +26,7 @@
2526

2627
from google.cloud.alloydb.connector import Connector
2728
from google.cloud.alloydb.connector import IPTypes
29+
from google.cloud.alloydb.connector.client import AlloyDBClient
2830
from google.cloud.alloydb.connector.exceptions import ClosedConnectorError
2931
from google.cloud.alloydb.connector.exceptions import IPTypeNotFoundError
3032
from google.cloud.alloydb.connector.instance import RefreshAheadCache
@@ -252,6 +254,19 @@ def test_Connector_remove_cached_bad_instance(
252254
"""
253255
instance_uri = "projects/test-project/locations/test-region/clusters/test-cluster/instances/bad-test-instance"
254256
with Connector(credentials) as connector:
257+
# The timeout of AlloyDB API methods is set to 60s by default.
258+
# We override it to 1s to shorten the duration of the test.
259+
connector._client = AlloyDBClient(
260+
"alloydb.googleapis.com", "test-project", credentials, driver="pg8000"
261+
)
262+
transport = connector._client._client.transport
263+
transport._wrapped_methods[transport.get_connection_info]._retry = Retry(
264+
timeout=1
265+
)
266+
transport._wrapped_methods[
267+
transport.generate_client_certificate
268+
]._retry = Retry(timeout=1)
269+
255270
with pytest.raises(RetryError):
256271
connector.connect(instance_uri, "pg8000")
257272
assert instance_uri not in connector._cache

0 commit comments

Comments
 (0)