Skip to content

Commit d742686

Browse files
chore: add test and update message
1 parent df55bc5 commit d742686

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

google/cloud/sql/connector/connector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ async def connect_async(
268268
"Running event loop does not match 'connector._loop'. "
269269
"Connector.connect_async() must be called from the event loop "
270270
"the Connector was initialized with. If you need to connect "
271-
"across event loops/threads please use a new Connector object."
271+
"across event loops, please use a new Connector object."
272272
)
273273

274274
if self._keys is None:

tests/unit/test_connector.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""
1616

1717
import asyncio
18+
from threading import Thread
1819
from typing import Union
1920

2021
from aiohttp import ClientResponseError
@@ -274,6 +275,38 @@ async def test_Connector_connect_async(
274275
assert connection is True
275276

276277

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

0 commit comments

Comments
 (0)