From: Daniele Varrazzo Date: Sun, 2 Jan 2022 21:19:15 +0000 (+0100) Subject: Hopefully more robust test to identify a closed connection X-Git-Tag: pool-3.1~48 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=09e39dcc5276a8d28c713e31072b202001c2568f;p=thirdparty%2Fpsycopg.git Hopefully more robust test to identify a closed connection --- diff --git a/tests/test_concurrency.py b/tests/test_concurrency.py index a17f0f4d3..3296faa17 100644 --- a/tests/test_concurrency.py +++ b/tests/test_concurrency.py @@ -7,7 +7,6 @@ import sys import time import queue import pytest -import selectors import threading import subprocess as sp from typing import List @@ -187,7 +186,7 @@ def test_cancel(conn, retries): @pytest.mark.slow def test_identify_closure(dsn, retries): def closer(): - time.sleep(0.3) + time.sleep(0.2) conn2.execute( "select pg_terminate_backend(%s)", [conn.pgconn.backend_pid] ) @@ -197,19 +196,16 @@ def test_identify_closure(dsn, retries): conn = psycopg.connect(dsn) conn2 = psycopg.connect(dsn) try: + t = threading.Thread(target=closer) + t.start() t0 = time.time() - with selectors.DefaultSelector() as sel: - sel.register(conn, selectors.EVENT_READ) - t = threading.Thread(target=closer) - t.start() - try: - assert sel.select(timeout=1.0) - with pytest.raises(psycopg.OperationalError): - conn.execute("select 1") - t1 = time.time() - assert 0.3 < t1 - t0 < 0.6 - finally: - t.join() + try: + with pytest.raises(psycopg.OperationalError): + conn.execute("select pg_sleep(1.0)") + t1 = time.time() + assert 0.2 < t1 - t0 < 0.4 + finally: + t.join() finally: conn.close() conn2.close() diff --git a/tests/test_concurrency_async.py b/tests/test_concurrency_async.py index f1aa8498b..995a3acd5 100644 --- a/tests/test_concurrency_async.py +++ b/tests/test_concurrency_async.py @@ -137,7 +137,7 @@ async def test_cancel(aconn, retries): @pytest.mark.slow async def test_identify_closure(dsn, retries): async def closer(): - await asyncio.sleep(0.3) + await asyncio.sleep(0.2) await conn2.execute( "select pg_terminate_backend(%s)", [aconn.pgconn.backend_pid] ) @@ -147,20 +147,15 @@ async def test_identify_closure(dsn, retries): aconn = await psycopg.AsyncConnection.connect(dsn) conn2 = await psycopg.AsyncConnection.connect(dsn) try: - t0 = time.time() - ev = asyncio.Event() - loop = asyncio.get_event_loop() - loop.add_reader(aconn.fileno(), ev.set) t = create_task(closer()) + t0 = time.time() try: - - await asyncio.wait_for(ev.wait(), 1.0) with pytest.raises(psycopg.OperationalError): - await aconn.execute("select 1") + await aconn.execute("select pg_sleep(1.0)") t1 = time.time() - assert 0.3 < t1 - t0 < 0.6 + assert 0.2 < t1 - t0 < 0.4 finally: - await asyncio.gather(*t) + await asyncio.gather(t) finally: await aconn.close() await conn2.close()