]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Make cleanup tests more reliable
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 27 Feb 2021 11:47:36 +0000 (12:47 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 12 Mar 2021 04:07:25 +0000 (05:07 +0100)
Unfortunately there is some race condition on delete and for now just
using a timer.

In order to limit the problem don't actually run the StopWorker task but
bail out from the workers as soon as seen.

psycopg3/psycopg3/pool/async_pool.py
psycopg3/psycopg3/pool/base.py
tests/pool/test_pool_async.py

index 381dbcd9b8441417e304e2b47e36a17d58caec5f..45a5bbf7cd414e6ef90f6c8d795095212cf2ccd4 100644 (file)
@@ -24,7 +24,7 @@ if sys.version_info >= (3, 7):
     get_running_loop = asyncio.get_running_loop
 
 else:
-    from .utils.context import asynccontextmanager
+    from ..utils.context import asynccontextmanager
 
     get_running_loop = asyncio.get_event_loop
 
index 0f74ab5975039c859cc461a38e5e0b1cdeccc21f..7397025ef25e4bef85d11da44040d4f1c918b48d 100644 (file)
@@ -168,6 +168,13 @@ class BasePool(Generic[ConnectionType]):
             except Empty:
                 continue
 
+            if isinstance(task, tasks.StopWorker):
+                logger.debug(
+                    "terminating working thread %s",
+                    threading.current_thread().name,
+                )
+                return
+
             # Run the task. Make sure don't die in the attempt.
             try:
                 task.run()
@@ -176,13 +183,6 @@ class BasePool(Generic[ConnectionType]):
                     "task run %s failed: %s: %s", task, e.__class__.__name__, e
                 )
 
-            if isinstance(task, tasks.StopWorker):
-                logger.debug(
-                    "terminating working thread %s",
-                    threading.current_thread().name,
-                )
-                return
-
 
 class ConnectionAttempt:
     """Keep the state of a connection attempt."""
index f407360bc934e4ff97c3cfcc99afd5f04b54eb46..5739c69f7753ec6e75ea84333fc9e5342361e238 100644 (file)
@@ -423,6 +423,7 @@ async def test_del_no_warning(dsn, recwarn):
     await p.wait_ready()
     ref = weakref.ref(p)
     del p
+    await asyncio.sleep(0.1)  # TODO: I wish it wasn't needed
     assert not ref()
     assert not recwarn
 
@@ -432,7 +433,7 @@ async def test_del_stop_threads(dsn):
     p = pool.AsyncConnectionPool(dsn)
     ts = [p._sched_runner] + p._workers
     del p
-    await asyncio.sleep(0.2)
+    await asyncio.sleep(0.1)
     for t in ts:
         assert not t.is_alive()