]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
use correct exception for terminate catch + test
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 13 Feb 2024 13:45:53 +0000 (08:45 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 13 Feb 2024 13:47:41 +0000 (08:47 -0500)
Fixed regression caused by just-released fix for :ticket:`10863` where an
invalid exception class were added to the "except" block, which does not
get exercised unless such a catch actually happens.   A mock-style test has
been added to ensure this catch is exercised in unit tests.

Fixes: #11005
Change-Id: I5a65403fb7bb35296ff44ae3cf6a336f8e0bda97

doc/build/changelog/unreleased_20/11005.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/postgresql/asyncpg.py
test/dialect/postgresql/test_dialect.py

diff --git a/doc/build/changelog/unreleased_20/11005.rst b/doc/build/changelog/unreleased_20/11005.rst
new file mode 100644 (file)
index 0000000..7c9292e
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: bug, postgresql, regression
+    :tickets: 11005
+
+    Fixed regression caused by just-released fix for :ticket:`10863` where an
+    invalid exception class were added to the "except" block, which does not
+    get exercised unless such a catch actually happens.   A mock-style test has
+    been added to ensure this catch is exercised in unit tests.
+
index 590823ad1c5f28652552369476e0ec018d6fc943..b8e815168bfd7016e90289d52a7a03e3fe45209b 100644 (file)
@@ -913,7 +913,11 @@ class AsyncAdapt_asyncpg_connection(AsyncAdapt_dbapi_connection):
                 # try to gracefully close; see #10717
                 # timeout added in asyncpg 0.14.0 December 2017
                 await_(self._connection.close(timeout=2))
-            except (asyncio.TimeoutError, OSError, self.dbapi.PostgresError):
+            except (
+                asyncio.TimeoutError,
+                OSError,
+                self.dbapi.asyncpg.PostgresError,
+            ):
                 # in the case where we are recycling an old connection
                 # that may have already been disconnected, close() will
                 # fail with the above timeout.  in this case, terminate
index 32a5a84ac8dc73ba801f81ff8ee400026f6df64d..40718ee2dff3dcedd187492f395f22fad074e6a1 100644 (file)
@@ -178,6 +178,28 @@ class DialectTest(fixtures.TestBase):
         with expect_raises(dataclasses.FrozenInstanceError):
             r1.lower = 8  # type: ignore
 
+    @testing.only_on("postgresql+asyncpg")
+    def test_asyncpg_terminate_catch(self):
+        """test for #11005"""
+
+        with testing.db.connect() as connection:
+            emulated_dbapi_connection = connection.connection.dbapi_connection
+
+            async def boom():
+                raise OSError("boom")
+
+            with mock.patch.object(
+                emulated_dbapi_connection,
+                "_connection",
+                mock.Mock(close=mock.Mock(return_value=boom())),
+            ) as mock_asyncpg_connection:
+                emulated_dbapi_connection.terminate()
+
+            eq_(
+                mock_asyncpg_connection.mock_calls,
+                [mock.call.close(timeout=2), mock.call.terminate()],
+            )
+
     def test_version_parsing(self):
         def mock_conn(res):
             return mock.Mock(