Skip to content

Commit 0998f8d

Browse files
chore: update based on feedback
1 parent ce2c30a commit 0998f8d

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

google/cloud/sql/connector/exceptions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,10 @@ class DnsResolutionError(Exception):
7777
Exception to be raised when an instance connection name can not be resolved
7878
from a DNS record.
7979
"""
80+
81+
82+
class CacheClosedError(Exception):
83+
"""
84+
Exception to be raised when a ConnectionInfoCache can not be accessed after
85+
it is closed.
86+
"""

google/cloud/sql/connector/monitored_cache.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from google.cloud.sql.connector.connection_info import ConnectionInfo
2121
from google.cloud.sql.connector.connection_info import ConnectionInfoCache
22+
from google.cloud.sql.connector.exceptions import CacheClosedError
2223
from google.cloud.sql.connector.instance import RefreshAheadCache
2324
from google.cloud.sql.connector.lazy import LazyRefreshCache
2425
from google.cloud.sql.connector.resolver import DefaultResolver
@@ -95,9 +96,16 @@ async def _check_domain_name(self) -> None:
9596
)
9697

9798
async def connect_info(self) -> ConnectionInfo:
99+
if self.closed:
100+
raise CacheClosedError(
101+
"Can not get connection info, cache has already been closed."
102+
)
98103
return await self.cache.connect_info()
99104

100105
async def force_refresh(self) -> None:
106+
# if cache is closed do not refresh
107+
if self.closed:
108+
return
101109
return await self.cache.force_refresh()
102110

103111
async def close(self) -> None:

tests/unit/test_monitored_cache.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
from google.cloud.sql.connector.client import CloudSQLClient
2727
from google.cloud.sql.connector.connection_name import ConnectionName
28+
from google.cloud.sql.connector.exceptions import CacheClosedError
2829
from google.cloud.sql.connector.lazy import LazyRefreshCache
2930
from google.cloud.sql.connector.monitored_cache import MonitoredCache
3031
from google.cloud.sql.connector.resolver import DefaultResolver
@@ -65,6 +66,28 @@ async def test_MonitoredCache_properties(fake_client: CloudSQLClient) -> None:
6566
assert monitored_cache.closed is True
6667

6768

69+
async def test_MonitoredCache_CacheClosedError(fake_client: CloudSQLClient) -> None:
70+
"""
71+
Test that MonitoredCache.connect_info errors when cache is closed.
72+
"""
73+
conn_name = ConnectionName("test-project", "test-region", "test-instance")
74+
cache = LazyRefreshCache(
75+
conn_name,
76+
client=fake_client,
77+
keys=asyncio.create_task(generate_keys()),
78+
enable_iam_auth=False,
79+
)
80+
monitored_cache = MonitoredCache(cache, 30, DefaultResolver())
81+
# test closed property
82+
assert monitored_cache.closed is False
83+
# close cache and make sure property is updated
84+
await monitored_cache.close()
85+
assert monitored_cache.closed is True
86+
# attempt to get connect info from closed cache
87+
with pytest.raises(CacheClosedError):
88+
await monitored_cache.connect_info()
89+
90+
6891
async def test_MonitoredCache_with_DnsResolver(fake_client: CloudSQLClient) -> None:
6992
"""
7093
Test that MonitoredCache with DnsResolver work as expected.

0 commit comments

Comments
 (0)