From: Daniele Varrazzo Date: Sun, 14 Nov 2021 19:34:22 +0000 (+0100) Subject: Dispose more carefully of connections and threads in concurrency tests X-Git-Tag: 3.0.4~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=75544572069696aaf031cae83fa7271253faf3a0;p=thirdparty%2Fpsycopg.git Dispose more carefully of connections and threads in concurrency tests I have seen some segfault on Windows sometimes, probably with the test_concurrency thread stomping over the memory of the process forked in test_multiprocess_close. Async tests refactored the same way, although they don't exhibit the same problem. --- diff --git a/tests/test_concurrency.py b/tests/test_concurrency.py index 0fd628ab8..981d8e262 100644 --- a/tests/test_concurrency.py +++ b/tests/test_concurrency.py @@ -149,6 +149,8 @@ def test_notifies(conn, dsn): assert n.payload == "2" assert t1 - t0 == pytest.approx(0.5, abs=0.05) + t.join() + @pytest.mark.slow def test_cancel(conn, retries): @@ -179,6 +181,8 @@ def test_cancel(conn, retries): conn.rollback() assert cur.execute("select 1").fetchone()[0] == 1 + t.join() + @pytest.mark.slow def test_identify_closure(dsn, retries): @@ -192,18 +196,20 @@ def test_identify_closure(dsn, retries): with retry: conn = psycopg.connect(dsn) conn2 = psycopg.connect(dsn) - - t0 = time.time() - sel = selectors.DefaultSelector() - sel.register(conn, selectors.EVENT_READ) - t = threading.Thread(target=closer) - t.start() - - 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 - - conn.close() - conn2.close() + try: + t0 = time.time() + sel = selectors.DefaultSelector() + 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() + finally: + conn.close() + conn2.close() diff --git a/tests/test_concurrency_async.py b/tests/test_concurrency_async.py index 3192a7abe..f1aa8498b 100644 --- a/tests/test_concurrency_async.py +++ b/tests/test_concurrency_async.py @@ -146,18 +146,21 @@ async def test_identify_closure(dsn, retries): with retry: aconn = await psycopg.AsyncConnection.connect(dsn) conn2 = await psycopg.AsyncConnection.connect(dsn) - - t0 = time.time() - ev = asyncio.Event() - loop = asyncio.get_event_loop() - loop.add_reader(aconn.fileno(), ev.set) - create_task(closer()) - - await asyncio.wait_for(ev.wait(), 1.0) - with pytest.raises(psycopg.OperationalError): - await aconn.execute("select 1") - t1 = time.time() - assert 0.3 < t1 - t0 < 0.6 - - await aconn.close() - await conn2.close() + try: + t0 = time.time() + ev = asyncio.Event() + loop = asyncio.get_event_loop() + loop.add_reader(aconn.fileno(), ev.set) + t = create_task(closer()) + try: + + await asyncio.wait_for(ev.wait(), 1.0) + with pytest.raises(psycopg.OperationalError): + await aconn.execute("select 1") + t1 = time.time() + assert 0.3 < t1 - t0 < 0.6 + finally: + await asyncio.gather(*t) + finally: + await aconn.close() + await conn2.close()