Skip to content

Commit dc2212a

Browse files
jackwotherspoonhessjcg
authored andcommitted
chore: add test and update message
1 parent 94e50ba commit dc2212a

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

google/cloud/sql/connector/connector.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from google.cloud.sql.connector.enums import DriverMapping
3535
from google.cloud.sql.connector.enums import IPTypes
3636
from google.cloud.sql.connector.enums import RefreshStrategy
37+
from google.cloud.sql.connector.exceptions import ConnectorLoopError
3738
from google.cloud.sql.connector.instance import RefreshAheadCache
3839
from google.cloud.sql.connector.lazy import LazyRefreshCache
3940
from google.cloud.sql.connector.monitored_cache import MonitoredCache
@@ -287,7 +288,7 @@ async def connect_async(
287288
"Running event loop does not match 'connector._loop'. "
288289
"Connector.connect_async() must be called from the event loop "
289290
"the Connector was initialized with. If you need to connect "
290-
"across event loops/threads please use a new Connector object."
291+
"across event loops, please use a new Connector object."
291292
)
292293

293294
if self._keys is None:

tests/unit/test_connector.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import asyncio
1818
import os
19+
from threading import Thread
1920
from typing import Union
2021

2122
from aiohttp import ClientResponseError
@@ -29,6 +30,7 @@
2930
from google.cloud.sql.connector.client import CloudSQLClient
3031
from google.cloud.sql.connector.connection_name import ConnectionName
3132
from google.cloud.sql.connector.exceptions import CloudSQLIPTypeError
33+
from google.cloud.sql.connector.exceptions import ConnectorLoopError
3234
from google.cloud.sql.connector.exceptions import IncompatibleDriverError
3335
from google.cloud.sql.connector.instance import RefreshAheadCache
3436

@@ -280,6 +282,38 @@ async def test_Connector_connect_async(
280282
assert connection is True
281283

282284

285+
@pytest.mark.asyncio
286+
async def test_Connector_connect_async_multiple_event_loops(
287+
fake_credentials: Credentials, fake_client: CloudSQLClient
288+
) -> None:
289+
"""Test that Connector.connect_async errors when run on wrong event loop."""
290+
291+
new_loop = asyncio.new_event_loop()
292+
thread = Thread(target=new_loop.run_forever, daemon=True)
293+
thread.start()
294+
295+
async with Connector(
296+
credentials=fake_credentials, loop=asyncio.get_running_loop()
297+
) as connector:
298+
connector._client = fake_client
299+
with pytest.raises(ConnectorLoopError) as exc_info:
300+
future = asyncio.run_coroutine_threadsafe(
301+
connector.connect_async(
302+
"test-project:test-region:test-instance", "asyncpg"
303+
),
304+
loop=new_loop,
305+
)
306+
future.result()
307+
assert (
308+
exc_info.value.args[0] == "Running event loop does not match "
309+
"'connector._loop'. Connector.connect_async() must be called from "
310+
"the event loop the Connector was initialized with. If you need to "
311+
"connect across event loops, please use a new Connector object."
312+
)
313+
new_loop.call_soon_threadsafe(new_loop.stop)
314+
thread.join()
315+
316+
283317
@pytest.mark.asyncio
284318
async def test_create_async_connector(fake_credentials: Credentials) -> None:
285319
"""Test that create_async_connector properly initializes connector

0 commit comments

Comments
 (0)