From: Daniele Varrazzo Date: Mon, 2 Jun 2025 10:37:26 +0000 (+0100) Subject: test: make sure tests don't hang if default connection points to a closed port X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8aac986a3a081ba6de5b3070facc10cc86965b52;p=thirdparty%2Fpsycopg.git test: make sure tests don't hang if default connection points to a closed port They might hang is the server doesn't accept connections on the standard port. Tested by forcing the default connection to go to an unresponsive port, using: export PGHOST=192.0.2.1 export PSYCOPG_TEST_DSN="host=localhost dbname=psycopg_test" and fixing all the tests hanging because they try to connect to the unroutable hostname, not taking into account the test dsn. --- diff --git a/tests/pq/test_pgconn.py b/tests/pq/test_pgconn.py index 657cab9b8..f6f7f2e93 100644 --- a/tests/pq/test_pgconn.py +++ b/tests/pq/test_pgconn.py @@ -16,6 +16,7 @@ import pytest import psycopg import psycopg.generators from psycopg import pq +from psycopg.conninfo import make_conninfo if TYPE_CHECKING: from psycopg.pq.abc import PGcancelConn, PGconn @@ -49,8 +50,8 @@ def test_connectdb(dsn): assert conn.status == pq.ConnStatus.OK, conn.error_message -def test_connectdb_error(): - conn = pq.PGconn.connect(b"dbname=psycopg_test_not_for_real") +def test_connectdb_error(dsn): + conn = pq.PGconn.connect(make_conninfo(dsn, dbname="nosuchdb").encode()) assert conn.status == pq.ConnStatus.BAD @@ -179,7 +180,7 @@ def test_ping(dsn): rv = pq.PGconn.ping(dsn.encode()) assert rv == pq.Ping.OK - rv = pq.PGconn.ping(b"port=9999") + rv = pq.PGconn.ping(make_conninfo(dsn, port=9999, connect_timeout=3).encode()) assert rv == pq.Ping.NO_RESPONSE diff --git a/tests/test_connection.py b/tests/test_connection.py index c340205c7..50c27ef6a 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -34,9 +34,9 @@ def test_connect(conn_cls, dsn): conn.close() -def test_connect_bad(conn_cls): +def test_connect_bad(conn_cls, dsn): with pytest.raises(psycopg.OperationalError): - conn_cls.connect("dbname=nosuchdb") + conn_cls.connect(dsn, dbname="nosuchdb") @pytest.mark.slow diff --git a/tests/test_connection_async.py b/tests/test_connection_async.py index ea7e6f77d..ef1efb80b 100644 --- a/tests/test_connection_async.py +++ b/tests/test_connection_async.py @@ -31,9 +31,9 @@ async def test_connect(aconn_cls, dsn): await conn.close() -async def test_connect_bad(aconn_cls): +async def test_connect_bad(aconn_cls, dsn): with pytest.raises(psycopg.OperationalError): - await aconn_cls.connect("dbname=nosuchdb") + await aconn_cls.connect(dsn, dbname="nosuchdb") @pytest.mark.slow diff --git a/tests/test_errors.py b/tests/test_errors.py index 536cbf3e6..3c875b0c6 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -286,18 +286,18 @@ def test_unknown_sqlstate(conn): assert pexc.sqlstate == code -def test_pgconn_error(conn_cls): +def test_pgconn_error(conn_cls, dsn): with pytest.raises(psycopg.OperationalError) as excinfo: - conn_cls.connect("dbname=nosuchdb") + conn_cls.connect(dsn, dbname="nosuchdb") exc = excinfo.value assert exc.pgconn assert exc.pgconn.db == b"nosuchdb" -def test_pgconn_error_pickle(conn_cls): +def test_pgconn_error_pickle(conn_cls, dsn): with pytest.raises(psycopg.OperationalError) as excinfo: - conn_cls.connect("dbname=nosuchdb") + conn_cls.connect(dsn, dbname="nosuchdb") exc = pickle.loads(pickle.dumps(excinfo.value)) assert exc.pgconn is None diff --git a/tests/test_gevent.py b/tests/test_gevent.py index 68df41314..0d39fa8f9 100644 --- a/tests/test_gevent.py +++ b/tests/test_gevent.py @@ -53,7 +53,7 @@ print(json.dumps(dts)) dts = json.loads(rv.stdout) for dt in dts: - assert TICK <= dt < TICK * 1.1 + assert TICK <= dt < TICK * 1.2 @pytest.mark.skipif("not psycopg._cmodule._psycopg") diff --git a/tests/test_waiting.py b/tests/test_waiting.py index 22ce74ef9..fc5130386 100644 --- a/tests/test_waiting.py +++ b/tests/test_waiting.py @@ -8,6 +8,7 @@ import pytest import psycopg from psycopg import generators, waiting from psycopg.pq import ConnStatus, ExecStatus +from psycopg.conninfo import make_conninfo skip_if_not_linux = pytest.mark.skipif( not sys.platform.startswith("linux"), reason="non-Linux platform" @@ -39,7 +40,7 @@ def test_wait_conn(dsn, timeout): def test_wait_conn_bad(dsn): - gen = generators.connect("dbname=nosuchdb") + gen = generators.connect(make_conninfo(dsn, dbname="nosuchdb")) with pytest.raises(psycopg.OperationalError): waiting.wait_conn(gen) @@ -155,7 +156,7 @@ async def test_wait_conn_async(dsn, timeout): @pytest.mark.anyio async def test_wait_conn_async_bad(dsn): - gen = generators.connect("dbname=nosuchdb") + gen = generators.connect(make_conninfo(dsn, dbname="nosuchdb")) with pytest.raises(psycopg.OperationalError): await waiting.wait_conn_async(gen)