]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Consider some tests flaky
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 10 Jun 2021 11:57:19 +0000 (12:57 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 10 Jun 2021 11:57:19 +0000 (12:57 +0100)
They fail regularly enough in the github workflow

tests/test_copy.py
tests/test_copy_async.py

index 1de41a436f86ac0eecba87b4c40ce9086ae84d32..e8247970e78c3085519c03f574c221f9b16e102e 100644 (file)
@@ -493,13 +493,12 @@ def test_worker_life(conn, format, buffer):
 @pytest.mark.slow
 @pytest.mark.parametrize("fmt", [Format.TEXT, Format.BINARY])
 @pytest.mark.parametrize("method", ["read", "iter", "row", "rows"])
-def test_copy_to_leaks(dsn, faker, fmt, method):
+def test_copy_to_leaks(dsn, faker, fmt, method, retries):
     faker.format = PgFormat.from_pq(fmt)
     faker.choose_schema(ncols=20)
     faker.make_records(20)
 
-    n = []
-    for i in range(3):
+    def work():
         with psycopg3.connect(dsn) as conn:
             with conn.cursor(binary=fmt) as cur:
                 cur.execute(faker.drop_stmt)
@@ -536,27 +535,28 @@ def test_copy_to_leaks(dsn, faker, fmt, method):
                     elif method == "rows":
                         list(copy.rows())
 
-                    tmp = None
+    for retry in retries:
+        with retry:
+            n = []
+            for i in range(3):
+                work()
+                gc.collect()
+                gc.collect()
+                n.append(len(gc.get_objects()))
 
-        del cur, conn
-        gc.collect()
-        gc.collect()
-        n.append(len(gc.get_objects()))
-
-    assert (
-        n[0] == n[1] == n[2]
-    ), f"objects leaked: {n[1] - n[0]}, {n[2] - n[1]}"
+            assert (
+                n[0] == n[1] == n[2]
+            ), f"objects leaked: {n[1] - n[0]}, {n[2] - n[1]}"
 
 
 @pytest.mark.slow
 @pytest.mark.parametrize("fmt", [Format.TEXT, Format.BINARY])
-def test_copy_from_leaks(dsn, faker, fmt):
+def test_copy_from_leaks(dsn, faker, fmt, retries):
     faker.format = PgFormat.from_pq(fmt)
     faker.choose_schema(ncols=20)
     faker.make_records(20)
 
-    n = []
-    for i in range(3):
+    def work():
         with psycopg3.connect(dsn) as conn:
             with conn.cursor(binary=fmt) as cur:
                 cur.execute(faker.drop_stmt)
@@ -577,12 +577,14 @@ def test_copy_from_leaks(dsn, faker, fmt):
                 for got, want in zip(recs, faker.records):
                     faker.assert_record(got, want)
 
-                del recs
-
-        del cur, conn
-        gc.collect()
-        gc.collect()
-        n.append(len(gc.get_objects()))
+    for retry in retries:
+        with retry:
+            n = []
+            for i in range(3):
+                work()
+                gc.collect()
+                gc.collect()
+                n.append(len(gc.get_objects()))
 
     assert (
         n[0] == n[1] == n[2]
index e679f79a0437d5d71ff7f2fe89cc31f861ce2612..1d0b5f0bf5be0750abfac922036ae7834969ccc4 100644 (file)
@@ -470,13 +470,12 @@ async def test_worker_life(aconn, format, buffer):
 @pytest.mark.slow
 @pytest.mark.parametrize("fmt", [Format.TEXT, Format.BINARY])
 @pytest.mark.parametrize("method", ["read", "iter", "row", "rows"])
-async def test_copy_to_leaks(dsn, faker, fmt, method):
+async def test_copy_to_leaks(dsn, faker, fmt, method, retries):
     faker.format = PgFormat.from_pq(fmt)
     faker.choose_schema(ncols=20)
     faker.make_records(20)
 
-    n = []
-    for i in range(3):
+    async def work():
         async with await psycopg3.AsyncConnection.connect(dsn) as conn:
             async with conn.cursor(binary=fmt) as cur:
                 await cur.execute(faker.drop_stmt)
@@ -515,27 +514,28 @@ async def test_copy_to_leaks(dsn, faker, fmt, method):
                         async for x in copy.rows():
                             pass
 
-                    tmp = None
+    async for retry in retries:
+        with retry:
+            n = []
+            for i in range(3):
+                await work()
+                gc.collect()
+                gc.collect()
+                n.append(len(gc.get_objects()))
 
-        del cur, conn
-        gc.collect()
-        gc.collect()
-        n.append(len(gc.get_objects()))
-
-    assert (
-        n[0] == n[1] == n[2]
-    ), f"objects leaked: {n[1] - n[0]}, {n[2] - n[1]}"
+            assert (
+                n[0] == n[1] == n[2]
+            ), f"objects leaked: {n[1] - n[0]}, {n[2] - n[1]}"
 
 
 @pytest.mark.slow
 @pytest.mark.parametrize("fmt", [Format.TEXT, Format.BINARY])
-async def test_copy_from_leaks(dsn, faker, fmt):
+async def test_copy_from_leaks(dsn, faker, fmt, retries):
     faker.format = PgFormat.from_pq(fmt)
     faker.choose_schema(ncols=20)
     faker.make_records(20)
 
-    n = []
-    for i in range(3):
+    async def work():
         async with await psycopg3.AsyncConnection.connect(dsn) as conn:
             async with conn.cursor(binary=fmt) as cur:
                 await cur.execute(faker.drop_stmt)
@@ -556,16 +556,18 @@ async def test_copy_from_leaks(dsn, faker, fmt):
                 for got, want in zip(recs, faker.records):
                     faker.assert_record(got, want)
 
-                del recs
+    async for retry in retries:
+        with retry:
+            n = []
+            for i in range(3):
+                await work()
+                gc.collect()
+                gc.collect()
+                n.append(len(gc.get_objects()))
 
-        del cur, conn
-        gc.collect()
-        gc.collect()
-        n.append(len(gc.get_objects()))
-
-    assert (
-        n[0] == n[1] == n[2]
-    ), f"objects leaked: {n[1] - n[0]}, {n[2] - n[1]}"
+            assert (
+                n[0] == n[1] == n[2]
+            ), f"objects leaked: {n[1] - n[0]}, {n[2] - n[1]}"
 
 
 async def ensure_table(cur, tabledef, name="copy_in"):