|
7 | 7 | from tempfile import TemporaryDirectory
|
8 | 8 | from threading import Thread
|
9 | 9 | from unittest import IsolatedAsyncioTestCase, SkipTest
|
| 10 | +from unittest.mock import patch |
10 | 11 |
|
11 | 12 | import aiosqlite
|
12 | 13 | from .helpers import setup_logger
|
@@ -351,6 +352,23 @@ async def test_connect_error(self):
|
351 | 352 | with self.assertRaisesRegex(OperationalError, "unable to open database"):
|
352 | 353 | await aiosqlite.connect(bad_db)
|
353 | 354 |
|
| 355 | + async def test_connect_base_exception(self): |
| 356 | + # Check if connect task is cancelled, thread is properly closed. |
| 357 | + def _raise_cancelled_error(*_, **__): |
| 358 | + raise asyncio.CancelledError("I changed my mind") |
| 359 | + |
| 360 | + connection = aiosqlite.Connection(lambda: sqlite3.connect(":memory:"), 64) |
| 361 | + with ( |
| 362 | + patch.object(sqlite3, "connect", side_effect=_raise_cancelled_error), |
| 363 | + self.assertRaisesRegex(asyncio.CancelledError, "I changed my mind"), |
| 364 | + ): |
| 365 | + async with connection: |
| 366 | + ... |
| 367 | + # Terminate the thread here if the test fails to have a clear error. |
| 368 | + if connection._running: |
| 369 | + connection._stop_running() |
| 370 | + raise AssertionError("connection thread was not stopped") |
| 371 | + |
354 | 372 | async def test_iterdump(self):
|
355 | 373 | async with aiosqlite.connect(":memory:") as db:
|
356 | 374 | await db.execute("create table foo (i integer, k charvar(250))")
|
|
0 commit comments