From: Daniele Varrazzo Date: Sat, 27 Feb 2021 11:47:36 +0000 (+0100) Subject: Make cleanup tests more reliable X-Git-Tag: 3.0.dev0~87^2~40 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=94191ba3c3f137d07757d474633574d51c532339;p=thirdparty%2Fpsycopg.git Make cleanup tests more reliable 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. --- diff --git a/psycopg3/psycopg3/pool/async_pool.py b/psycopg3/psycopg3/pool/async_pool.py index 381dbcd9b..45a5bbf7c 100644 --- a/psycopg3/psycopg3/pool/async_pool.py +++ b/psycopg3/psycopg3/pool/async_pool.py @@ -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 diff --git a/psycopg3/psycopg3/pool/base.py b/psycopg3/psycopg3/pool/base.py index 0f74ab597..7397025ef 100644 --- a/psycopg3/psycopg3/pool/base.py +++ b/psycopg3/psycopg3/pool/base.py @@ -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.""" diff --git a/tests/pool/test_pool_async.py b/tests/pool/test_pool_async.py index f407360bc..5739c69f7 100644 --- a/tests/pool/test_pool_async.py +++ b/tests/pool/test_pool_async.py @@ -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()