]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Retry flaky tests
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 19 Jul 2021 03:43:09 +0000 (05:43 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 19 Jul 2021 03:43:09 +0000 (05:43 +0200)
tests/test_concurrency.py
tests/test_concurrency_async.py

index f09cb50068e59f79f900421488598dda3be27ba2..f943aab5b7c8ed4091b5e692ae791b917ee7ead2 100644 (file)
@@ -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
index f52353f859d17370e3a05867b3695160873c2061..f29a7ef66fdecb7378a16fa68005d3e4fbc62305 100644 (file)
@@ -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