2424
2525from google .cloud .alloydb .connector import AsyncConnector
2626from google .cloud .alloydb .connector import IPTypes
27+ from google .cloud .alloydb .connector .exceptions import ClosedConnectorError
2728from google .cloud .alloydb .connector .exceptions import IPTypeNotFoundError
2829from google .cloud .alloydb .connector .instance import RefreshAheadCache
2930
@@ -42,6 +43,7 @@ async def test_AsyncConnector_init(credentials: FakeCredentials) -> None:
4243 assert connector ._client is None
4344 assert connector ._credentials == credentials
4445 assert connector ._enable_iam_auth is False
46+ assert connector ._closed is False
4547 await connector .close ()
4648
4749
@@ -109,7 +111,7 @@ async def test_AsyncConnector_init_bad_ip_type(credentials: FakeCredentials) ->
109111 )
110112
111113
112- def test_AsyncConnector_init_alloydb_api_endpoint_with_http_prefix (
114+ async def test_AsyncConnector_init_alloydb_api_endpoint_with_http_prefix (
113115 credentials : FakeCredentials ,
114116) -> None :
115117 """
@@ -120,10 +122,10 @@ def test_AsyncConnector_init_alloydb_api_endpoint_with_http_prefix(
120122 alloydb_api_endpoint = "http://alloydb.googleapis.com" , credentials = credentials
121123 )
122124 assert connector ._alloydb_api_endpoint == "alloydb.googleapis.com"
123- connector .close ()
125+ await connector .close ()
124126
125127
126- def test_AsyncConnector_init_alloydb_api_endpoint_with_https_prefix (
128+ async def test_AsyncConnector_init_alloydb_api_endpoint_with_https_prefix (
127129 credentials : FakeCredentials ,
128130) -> None :
129131 """
@@ -134,7 +136,7 @@ def test_AsyncConnector_init_alloydb_api_endpoint_with_https_prefix(
134136 alloydb_api_endpoint = "https://alloydb.googleapis.com" , credentials = credentials
135137 )
136138 assert connector ._alloydb_api_endpoint == "alloydb.googleapis.com"
137- connector .close ()
139+ await connector .close ()
138140
139141
140142@pytest .mark .asyncio
@@ -357,3 +359,26 @@ async def test_Connector_remove_cached_no_ip_type(credentials: FakeCredentials)
357359 await connector .connect (instance_uri , "asyncpg" , ip_type = "private" )
358360 # check that cache has been removed from dict
359361 assert instance_uri not in connector ._cache
362+
363+
364+ async def test_close_sets_connector_as_closed (credentials : FakeCredentials ) -> None :
365+ """
366+ Test that when connector is closed, it marked as closed.
367+ """
368+ async with AsyncConnector (credentials = credentials ) as connector :
369+ assert connector ._closed is False
370+ assert connector ._closed is True
371+
372+
373+ async def test_connect_when_closed (credentials : FakeCredentials ) -> None :
374+ """
375+ Test that connector.connect errors when the connection is closed.
376+ """
377+ connector = AsyncConnector (credentials = credentials )
378+ await connector .close ()
379+ with pytest .raises (ClosedConnectorError ) as exc_info :
380+ await connector .connect ("" , "" )
381+ assert (
382+ exc_info .value .args [0 ]
383+ == "Connection attempt failed because the connector has already been closed."
384+ )
0 commit comments