From: Daniele Varrazzo Date: Sat, 17 Jul 2021 01:52:51 +0000 (+0200) Subject: Retry some flaky tests X-Git-Tag: 3.0.dev1~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=34066772e865b727587776016da8ae328c405d46;p=thirdparty%2Fpsycopg.git Retry some flaky tests --- diff --git a/tests/pool/test_pool.py b/tests/pool/test_pool.py index 105094838..e8727a343 100644 --- a/tests/pool/test_pool.py +++ b/tests/pool/test_pool.py @@ -929,7 +929,7 @@ def test_stats_measures(dsn): @pytest.mark.slow @pytest.mark.timing -def test_stats_usage(dsn): +def test_stats_usage(dsn, retries): def worker(n): try: with p.connection(timeout=0.3) as conn: @@ -937,29 +937,31 @@ def test_stats_usage(dsn): except pool.PoolTimeout: pass - with pool.ConnectionPool(dsn, min_size=3) as p: - p.wait(2.0) + for retry in retries: + with retry: + with pool.ConnectionPool(dsn, min_size=3) as p: + p.wait(2.0) - ts = [Thread(target=worker, args=(i,)) for i in range(7)] - [t.start() for t in ts] - [t.join() for t in ts] - stats = p.get_stats() - assert stats["requests_num"] == 7 - assert stats["requests_queued"] == 4 - assert 850 <= stats["requests_wait_ms"] <= 950 - assert stats["requests_errors"] == 1 - assert 1150 <= stats["usage_ms"] <= 1250 - assert stats.get("returns_bad", 0) == 0 + ts = [Thread(target=worker, args=(i,)) for i in range(7)] + [t.start() for t in ts] + [t.join() for t in ts] + stats = p.get_stats() + assert stats["requests_num"] == 7 + assert stats["requests_queued"] == 4 + assert 850 <= stats["requests_wait_ms"] <= 950 + assert stats["requests_errors"] == 1 + assert 1150 <= stats["usage_ms"] <= 1250 + assert stats.get("returns_bad", 0) == 0 - with p.connection() as conn: - conn.close() - p.wait() - stats = p.pop_stats() - assert stats["requests_num"] == 8 - assert stats["returns_bad"] == 1 - with p.connection(): - pass - assert p.get_stats()["requests_num"] == 1 + with p.connection() as conn: + conn.close() + p.wait() + stats = p.pop_stats() + assert stats["requests_num"] == 8 + assert stats["returns_bad"] == 1 + with p.connection(): + pass + assert p.get_stats()["requests_num"] == 1 @pytest.mark.slow diff --git a/tests/pool/test_pool_async.py b/tests/pool/test_pool_async.py index bbf859208..37a6733d7 100644 --- a/tests/pool/test_pool_async.py +++ b/tests/pool/test_pool_async.py @@ -940,7 +940,7 @@ async def test_stats_measures(dsn): @pytest.mark.slow @pytest.mark.timing -async def test_stats_usage(dsn): +async def test_stats_usage(dsn, retries): async def worker(n): try: async with p.connection(timeout=0.3) as conn: @@ -948,28 +948,30 @@ async def test_stats_usage(dsn): except pool.PoolTimeout: pass - async with pool.AsyncConnectionPool(dsn, min_size=3) as p: - await p.wait(2.0) + async for retry in retries: + with retry: + async with pool.AsyncConnectionPool(dsn, min_size=3) as p: + await p.wait(2.0) - ts = [create_task(worker(i)) for i in range(7)] - await asyncio.gather(*ts) - stats = p.get_stats() - assert stats["requests_num"] == 7 - assert stats["requests_queued"] == 4 - assert 850 <= stats["requests_wait_ms"] <= 950 - assert stats["requests_errors"] == 1 - assert 1150 <= stats["usage_ms"] <= 1250 - assert stats.get("returns_bad", 0) == 0 + ts = [create_task(worker(i)) for i in range(7)] + await asyncio.gather(*ts) + stats = p.get_stats() + assert stats["requests_num"] == 7 + assert stats["requests_queued"] == 4 + assert 850 <= stats["requests_wait_ms"] <= 950 + assert stats["requests_errors"] == 1 + assert 1150 <= stats["usage_ms"] <= 1250 + assert stats.get("returns_bad", 0) == 0 - async with p.connection() as conn: - await conn.close() - await p.wait() - stats = p.pop_stats() - assert stats["requests_num"] == 8 - assert stats["returns_bad"] == 1 - async with p.connection(): - pass - assert p.get_stats()["requests_num"] == 1 + async with p.connection() as conn: + await conn.close() + await p.wait() + stats = p.pop_stats() + assert stats["requests_num"] == 8 + assert stats["returns_bad"] == 1 + async with p.connection(): + pass + assert p.get_stats()["requests_num"] == 1 @pytest.mark.slow diff --git a/tests/test_concurrency.py b/tests/test_concurrency.py index b25bf4b48..f09cb5006 100644 --- a/tests/test_concurrency.py +++ b/tests/test_concurrency.py @@ -15,7 +15,7 @@ import psycopg @pytest.mark.slow -def test_concurrent_execution(dsn): +def test_concurrent_execution(dsn, retries): def worker(): cnn = psycopg.connect(dsn) cur = cnn.cursor() @@ -23,14 +23,16 @@ def test_concurrent_execution(dsn): cur.close() cnn.close() - t1 = threading.Thread(target=worker) - t2 = threading.Thread(target=worker) - t0 = time.time() - t1.start() - t2.start() - t1.join() - t2.join() - assert time.time() - t0 < 0.8, "something broken in concurrency" + for retry in retries: + with retry: + t1 = threading.Thread(target=worker) + t2 = threading.Thread(target=worker) + t0 = time.time() + t1.start() + t2.start() + t1.join() + t2.join() + assert time.time() - t0 < 0.8, "something broken in concurrency" @pytest.mark.slow diff --git a/tests/test_concurrency_async.py b/tests/test_concurrency_async.py index 190b6478e..3b509f17d 100644 --- a/tests/test_concurrency_async.py +++ b/tests/test_concurrency_async.py @@ -42,7 +42,7 @@ async def test_commit_concurrency(aconn): @pytest.mark.slow -async def test_concurrent_execution(dsn): +async def test_concurrent_execution(dsn, retries): async def worker(): cnn = await psycopg.AsyncConnection.connect(dsn) cur = cnn.cursor() @@ -50,10 +50,12 @@ async def test_concurrent_execution(dsn): await cur.close() await cnn.close() - workers = [worker(), worker()] - t0 = time.time() - await asyncio.gather(*workers) - assert time.time() - t0 < 0.8, "something broken in concurrency" + async for retry in retries: + with retry: + workers = [worker(), worker()] + t0 = time.time() + await asyncio.gather(*workers) + assert time.time() - t0 < 0.8, "something broken in concurrency" @pytest.mark.slow