]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
test: add test to verify that a server disconnection now raises AdminShutdown
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 8 Jan 2025 12:36:26 +0000 (13:36 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 10 Jan 2025 00:39:29 +0000 (01:39 +0100)
Previously it was raising OperationalError, but this was just the result
of the issue reported in #988. Note that AdminShutdown is a subclass of
OperationalError, therefore this change is backward compatible.

docs/news.rst
tests/test_connection.py
tests/test_connection_async.py

index 94975446d40086a31d278afa40121bebf9997085..ff760ad864b4f0c0318f75ac9c13e4e8ee6a5a93 100644 (file)
@@ -17,8 +17,9 @@ Psycopg 3.2.4 (unreleased)
   is not running (:ticket:`#962`).
 - Make sure that the notifies callback is called during the use of the
   `~Connection.notifies()` generator (:ticket:`#972`).
-- Raise `~errors.IdleInTransactionSessionTimeout` instead of a generic
-  `OperationalError` upon hitting an idle-in-transaction timeout
+- Raise the correct error returned by the database (such as `!AdminShutdown`
+  or `!IdleInTransactionSessionTimeout`) instead of a generic
+  `OperationalError` when a server error causes a client disconnection
   (:ticket:`#988`).
 
 
index 039d85218876f87c76a386791048a1393531893d..64e5c963222b3f6f36a3bef59da68e39ccb97cf0 100644 (file)
@@ -877,8 +877,14 @@ def test_resolve_hostaddr_conn(conn_cls, monkeypatch, fake_resolve):
     assert conninfo_to_dict(got) == {"host": "foo.com", "hostaddr": "1.1.1.1"}
 
 
-@pytest.mark.slow
+@pytest.mark.crdb_skip("pg_terminate_backend")
 def test_right_exception_on_server_disconnect(conn):
+    with pytest.raises(e.AdminShutdown):
+        conn.execute("select pg_terminate_backend(%s)", [conn.pgconn.backend_pid])
+
+
+@pytest.mark.slow
+def test_right_exception_on_session_timeout(conn):
     conn.execute("SET SESSION idle_in_transaction_session_timeout = 100")
     sleep(0.2)
     with pytest.raises(e.IdleInTransactionSessionTimeout):
index 51fc9bc82b4537c552e90f248090fc297126cb07..a4635f790906dc11726eae796e1816c8810910e1 100644 (file)
@@ -881,8 +881,16 @@ async def test_resolve_hostaddr_conn(aconn_cls, monkeypatch, fake_resolve):
     assert conninfo_to_dict(got) == {"host": "foo.com", "hostaddr": "1.1.1.1"}
 
 
-@pytest.mark.slow
+@pytest.mark.crdb_skip("pg_terminate_backend")
 async def test_right_exception_on_server_disconnect(aconn):
+    with pytest.raises(e.AdminShutdown):
+        await aconn.execute(
+            "select pg_terminate_backend(%s)", [aconn.pgconn.backend_pid]
+        )
+
+
+@pytest.mark.slow
+async def test_right_exception_on_session_timeout(aconn):
     await aconn.execute("SET SESSION idle_in_transaction_session_timeout = 100")
     await asleep(0.2)
     with pytest.raises(e.IdleInTransactionSessionTimeout):