|
25 | 25 |
|
26 | 26 | from google.cloud.sql.connector.client import CloudSQLClient |
27 | 27 | from google.cloud.sql.connector.connection_name import ConnectionName |
| 28 | +from google.cloud.sql.connector.exceptions import CacheClosedError |
28 | 29 | from google.cloud.sql.connector.lazy import LazyRefreshCache |
29 | 30 | from google.cloud.sql.connector.monitored_cache import MonitoredCache |
30 | 31 | from google.cloud.sql.connector.resolver import DefaultResolver |
@@ -65,6 +66,28 @@ async def test_MonitoredCache_properties(fake_client: CloudSQLClient) -> None: |
65 | 66 | assert monitored_cache.closed is True |
66 | 67 |
|
67 | 68 |
|
| 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 | + |
68 | 91 | async def test_MonitoredCache_with_DnsResolver(fake_client: CloudSQLClient) -> None: |
69 | 92 | """ |
70 | 93 | Test that MonitoredCache with DnsResolver work as expected. |
|
0 commit comments