From: Daniele Varrazzo Date: Mon, 19 Jul 2021 03:43:09 +0000 (+0200) Subject: Retry flaky tests X-Git-Tag: 3.0.dev2~56 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9a58188c2f5219b211efae741eb73aa34421b383;p=thirdparty%2Fpsycopg.git Retry flaky tests --- diff --git a/tests/test_concurrency.py b/tests/test_concurrency.py index f09cb5006..f943aab5b 100644 --- a/tests/test_concurrency.py +++ b/tests/test_concurrency.py @@ -148,10 +148,7 @@ def test_notifies(conn, dsn): @pytest.mark.slow -def test_cancel(conn): - - errors = [] - +def test_cancel(conn, retries): def canceller(): try: time.sleep(0.5) @@ -159,21 +156,25 @@ def test_cancel(conn): except Exception as exc: errors.append(exc) - cur = conn.cursor() - t = threading.Thread(target=canceller) - t0 = time.time() - t.start() + for retry in retries: + with retry: + errors = [] - with pytest.raises(psycopg.DatabaseError): - cur.execute("select pg_sleep(2)") + cur = conn.cursor() + t = threading.Thread(target=canceller) + t0 = time.time() + t.start() - t1 = time.time() - assert not errors - assert 0.0 < t1 - t0 < 1.0 + with pytest.raises(psycopg.DatabaseError): + cur.execute("select pg_sleep(2)") + + t1 = time.time() + assert not errors + assert 0.0 < t1 - t0 < 1.0 - # still working - conn.rollback() - assert cur.execute("select 1").fetchone()[0] == 1 + # still working + conn.rollback() + assert cur.execute("select 1").fetchone()[0] == 1 @pytest.mark.slow diff --git a/tests/test_concurrency_async.py b/tests/test_concurrency_async.py index f52353f85..f29a7ef66 100644 --- a/tests/test_concurrency_async.py +++ b/tests/test_concurrency_async.py @@ -101,10 +101,7 @@ async def test_notifies(aconn, dsn): @pytest.mark.slow -async def test_cancel(aconn): - - errors = [] - +async def test_cancel(aconn, retries): async def canceller(): try: await asyncio.sleep(0.5) @@ -117,20 +114,23 @@ async def test_cancel(aconn): with pytest.raises(psycopg.DatabaseError): await cur.execute("select pg_sleep(2)") - workers = [worker(), canceller()] - - t0 = time.time() - await asyncio.gather(*workers) + async for retry in retries: + with retry: + errors = [] + workers = [worker(), canceller()] - t1 = time.time() - assert not errors - assert 0.0 < t1 - t0 < 1.0 + t0 = time.time() + await asyncio.gather(*workers) - # still working - await aconn.rollback() - cur = aconn.cursor() - await cur.execute("select 1") - assert await cur.fetchone() == (1,) + t1 = time.time() + assert not errors + assert 0.0 < t1 - t0 < 1.0 + + # still working + await aconn.rollback() + cur = aconn.cursor() + await cur.execute("select 1") + assert await cur.fetchone() == (1,) @pytest.mark.slow