|
15 | 15 | """ |
16 | 16 |
|
17 | 17 | import asyncio |
| 18 | +from threading import Thread |
18 | 19 | from typing import Union |
19 | 20 |
|
20 | 21 | from aiohttp import ClientResponseError |
@@ -274,6 +275,38 @@ async def test_Connector_connect_async( |
274 | 275 | assert connection is True |
275 | 276 |
|
276 | 277 |
|
| 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 | + |
277 | 310 | @pytest.mark.asyncio |
278 | 311 | async def test_create_async_connector(fake_credentials: Credentials) -> None: |
279 | 312 | """Test that create_async_connector properly initializes connector |
|
0 commit comments