Skip to content

Commit

Permalink
chore: add test and update message
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwotherspoon committed Oct 1, 2024
1 parent df55bc5 commit d742686
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion google/cloud/sql/connector/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ async def connect_async(
"Running event loop does not match 'connector._loop'. "
"Connector.connect_async() must be called from the event loop "
"the Connector was initialized with. If you need to connect "
"across event loops/threads please use a new Connector object."
"across event loops, please use a new Connector object."
)

if self._keys is None:
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""

import asyncio
from threading import Thread
from typing import Union

from aiohttp import ClientResponseError
Expand Down Expand Up @@ -274,6 +275,38 @@ async def test_Connector_connect_async(
assert connection is True


@pytest.mark.asyncio
async def test_Connector_connect_async_multiple_event_loops(
fake_credentials: Credentials, fake_client: CloudSQLClient
) -> None:
"""Test that Connector.connect_async errors when run on wrong event loop."""

new_loop = asyncio.new_event_loop()
thread = Thread(target=new_loop.run_forever, daemon=True)
thread.start()

async with Connector(
credentials=fake_credentials, loop=asyncio.get_running_loop()
) as connector:
connector._client = fake_client
with pytest.raises(ConnectorLoopError) as exc_info:
future = asyncio.run_coroutine_threadsafe(
connector.connect_async(
"test-project:test-region:test-instance", "asyncpg"
),
loop=new_loop,
)
future.result()
assert (
exc_info.value.args[0] == "Running event loop does not match "
"'connector._loop'. Connector.connect_async() must be called from "
"the event loop the Connector was initialized with. If you need to "
"connect across event loops, please use a new Connector object."
)
new_loop.call_soon_threadsafe(new_loop.stop)
thread.join()


@pytest.mark.asyncio
async def test_create_async_connector(fake_credentials: Credentials) -> None:
"""Test that create_async_connector properly initializes connector
Expand Down

0 comments on commit d742686

Please sign in to comment.