]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Hopefully more robust test to identify a closed connection
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 2 Jan 2022 21:19:15 +0000 (22:19 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 2 Jan 2022 21:19:15 +0000 (22:19 +0100)
tests/test_concurrency.py
tests/test_concurrency_async.py

index a17f0f4d32b4d87b24d94e48417607331c9430e0..3296faa17f8663e5bc0b60ba8d47e2f13f3972b5 100644 (file)
@@ -7,7 +7,6 @@ import sys
 import time
 import queue
 import pytest
-import selectors
 import threading
 import subprocess as sp
 from typing import List
@@ -187,7 +186,7 @@ def test_cancel(conn, retries):
 @pytest.mark.slow
 def test_identify_closure(dsn, retries):
     def closer():
-        time.sleep(0.3)
+        time.sleep(0.2)
         conn2.execute(
             "select pg_terminate_backend(%s)", [conn.pgconn.backend_pid]
         )
@@ -197,19 +196,16 @@ def test_identify_closure(dsn, retries):
             conn = psycopg.connect(dsn)
             conn2 = psycopg.connect(dsn)
             try:
+                t = threading.Thread(target=closer)
+                t.start()
                 t0 = time.time()
-                with selectors.DefaultSelector() as sel:
-                    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()
+                try:
+                    with pytest.raises(psycopg.OperationalError):
+                        conn.execute("select pg_sleep(1.0)")
+                    t1 = time.time()
+                    assert 0.2 < t1 - t0 < 0.4
+                finally:
+                    t.join()
             finally:
                 conn.close()
                 conn2.close()
index f1aa8498b523835a51dcf6e827f518e0a4c1005e..995a3acd50743a9cb9791fdc3cd49636c5747e29 100644 (file)
@@ -137,7 +137,7 @@ async def test_cancel(aconn, retries):
 @pytest.mark.slow
 async def test_identify_closure(dsn, retries):
     async def closer():
-        await asyncio.sleep(0.3)
+        await asyncio.sleep(0.2)
         await conn2.execute(
             "select pg_terminate_backend(%s)", [aconn.pgconn.backend_pid]
         )
@@ -147,20 +147,15 @@ async def test_identify_closure(dsn, retries):
             aconn = await psycopg.AsyncConnection.connect(dsn)
             conn2 = await psycopg.AsyncConnection.connect(dsn)
             try:
-                t0 = time.time()
-                ev = asyncio.Event()
-                loop = asyncio.get_event_loop()
-                loop.add_reader(aconn.fileno(), ev.set)
                 t = create_task(closer())
+                t0 = time.time()
                 try:
-
-                    await asyncio.wait_for(ev.wait(), 1.0)
                     with pytest.raises(psycopg.OperationalError):
-                        await aconn.execute("select 1")
+                        await aconn.execute("select pg_sleep(1.0)")
                     t1 = time.time()
-                    assert 0.3 < t1 - t0 < 0.6
+                    assert 0.2 < t1 - t0 < 0.4
                 finally:
-                    await asyncio.gather(*t)
+                    await asyncio.gather(t)
             finally:
                 await aconn.close()
                 await conn2.close()