From: Daniele Varrazzo Date: Wed, 8 Jan 2025 12:36:26 +0000 (+0100) Subject: test: add test to verify that a server disconnection now raises AdminShutdown X-Git-Tag: 3.2.4~6^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8ba5470c22ac74530795508c0b46d53ab10dbdff;p=thirdparty%2Fpsycopg.git test: add test to verify that a server disconnection now raises AdminShutdown 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. --- diff --git a/docs/news.rst b/docs/news.rst index 94975446d..ff760ad86 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -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`). diff --git a/tests/test_connection.py b/tests/test_connection.py index 039d85218..64e5c9632 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -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): diff --git a/tests/test_connection_async.py b/tests/test_connection_async.py index 51fc9bc82..a4635f790 100644 --- a/tests/test_connection_async.py +++ b/tests/test_connection_async.py @@ -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):