From 5f4d651376d029d294dde7c9be51833b430e6a38 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 8 Mar 2021 03:32:34 +0100 Subject: [PATCH] Rename pool.wait_ready() to pool.wait() --- psycopg3/psycopg3/pool/async_pool.py | 3 ++- psycopg3/psycopg3/pool/pool.py | 3 ++- tests/pool/test_pool.py | 32 ++++++++++++++-------------- tests/pool/test_pool_async.py | 30 +++++++++++++------------- 4 files changed, 35 insertions(+), 33 deletions(-) diff --git a/psycopg3/psycopg3/pool/async_pool.py b/psycopg3/psycopg3/pool/async_pool.py index 592e5bc63..579f5ed31 100644 --- a/psycopg3/psycopg3/pool/async_pool.py +++ b/psycopg3/psycopg3/pool/async_pool.py @@ -108,7 +108,7 @@ class AsyncConnectionPool(BasePool[AsyncConnection]): "task run %s failed: %s: %s", task, e.__class__.__name__, e ) - async def wait_ready(self, timeout: float = 30.0) -> None: + async def wait(self, timeout: float = 30.0) -> None: """ Wait for the pool to be full after init. @@ -129,6 +129,7 @@ class AsyncConnectionPool(BasePool[AsyncConnection]): ) async with self._lock: + assert self._pool_full_event self._pool_full_event = None @asynccontextmanager diff --git a/psycopg3/psycopg3/pool/pool.py b/psycopg3/psycopg3/pool/pool.py index 08a7435c4..ece4a7c32 100644 --- a/psycopg3/psycopg3/pool/pool.py +++ b/psycopg3/psycopg3/pool/pool.py @@ -89,7 +89,7 @@ class ConnectionPool(BasePool[Connection]): for i in range(len(self._workers)): self.run_task(StopWorker(self)) - def wait_ready(self, timeout: float = 30.0) -> None: + def wait(self, timeout: float = 30.0) -> None: """ Wait for the pool to be full after init. @@ -108,6 +108,7 @@ class ConnectionPool(BasePool[Connection]): ) with self._lock: + assert self._pool_full_event self._pool_full_event = None @contextmanager diff --git a/tests/pool/test_pool.py b/tests/pool/test_pool.py index ea15d5cb3..a1fe95301 100644 --- a/tests/pool/test_pool.py +++ b/tests/pool/test_pool.py @@ -87,7 +87,7 @@ def test_concurrent_filling(dsn, monkeypatch, retries): t0 = time() with pool.ConnectionPool(dsn, minconn=5, num_workers=2) as p: - p.wait_ready(1.0) + p.wait(1.0) want_times = [0.1, 0.1, 0.2, 0.2, 0.3] assert len(times) == len(want_times) for got, want in zip(times, want_times): @@ -99,14 +99,14 @@ def test_wait_ready(dsn, monkeypatch): delay_connection(monkeypatch, 0.1) with pytest.raises(pool.PoolTimeout): with pool.ConnectionPool(dsn, minconn=4, num_workers=1) as p: - p.wait_ready(0.3) + p.wait(0.3) with pool.ConnectionPool(dsn, minconn=4, num_workers=1) as p: - p.wait_ready(0.5) + p.wait(0.5) with pool.ConnectionPool(dsn, minconn=4, num_workers=2) as p: - p.wait_ready(0.3) - p.wait_ready(0.0001) # idempotent + p.wait(0.3) + p.wait(0.0001) # idempotent @pytest.mark.slow @@ -115,7 +115,7 @@ def test_setup_no_timeout(dsn, proxy): with pool.ConnectionPool( proxy.client_dsn, minconn=1, num_workers=1 ) as p: - p.wait_ready(0.2) + p.wait(0.2) with pool.ConnectionPool(proxy.client_dsn, minconn=1, num_workers=1) as p: sleep(0.5) @@ -161,7 +161,7 @@ def test_configure_broken(dsn, caplog): with pool.ConnectionPool(minconn=1, configure=configure) as p: with pytest.raises(pool.PoolTimeout): - p.wait_ready(timeout=0.5) + p.wait(timeout=0.5) assert caplog.records assert "WAT" in caplog.records[0].message @@ -417,7 +417,7 @@ def test_del_no_warning(dsn, recwarn): with p.connection() as conn: conn.execute("select 1") - p.wait_ready() + p.wait() ref = weakref.ref(p) del p assert not ref() @@ -505,7 +505,7 @@ def test_grow(dsn, monkeypatch, retries): with pool.ConnectionPool( dsn, minconn=2, maxconn=4, num_workers=3 ) as p: - p.wait_ready(1.0) + p.wait(1.0) results = [] ts = [Thread(target=worker, args=(i,)) for i in range(6)] @@ -539,7 +539,7 @@ def test_shrink(dsn, monkeypatch): conn.execute("select pg_sleep(0.1)") with pool.ConnectionPool(dsn, minconn=2, maxconn=4, max_idle=0.2) as p: - p.wait_ready(5.0) + p.wait(5.0) assert p.max_idle == 0.2 ts = [Thread(target=worker, args=(i,)) for i in range(4)] @@ -561,7 +561,7 @@ def test_reconnect(proxy, caplog, monkeypatch): proxy.start() with pool.ConnectionPool(proxy.client_dsn, minconn=1) as p: - p.wait_ready(2.0) + p.wait(2.0) proxy.stop() with pytest.raises(psycopg3.OperationalError): @@ -570,7 +570,7 @@ def test_reconnect(proxy, caplog, monkeypatch): sleep(1.0) proxy.start() - p.wait_ready() + p.wait() with p.connection() as conn: conn.execute("select 1") @@ -604,7 +604,7 @@ def test_reconnect_failure(proxy): reconnect_timeout=1.0, reconnect_failed=failed, ) as p: - p.wait_ready(2.0) + p.wait(2.0) proxy.stop() with pytest.raises(psycopg3.OperationalError): @@ -701,11 +701,11 @@ def test_max_lifetime(dsn): def test_check(dsn, caplog): caplog.set_level(logging.WARNING, logger="psycopg3.pool") with pool.ConnectionPool(dsn, minconn=4) as p: - p.wait_ready(1.0) + p.wait(1.0) with p.connection() as conn: pid = conn.pgconn.backend_pid - p.wait_ready(1.0) + p.wait(1.0) pids = set(conn.pgconn.backend_pid for conn in p._pool) assert pid in pids conn.close() @@ -713,7 +713,7 @@ def test_check(dsn, caplog): assert len(caplog.records) == 0 p.check() assert len(caplog.records) == 1 - p.wait_ready(1.0) + p.wait(1.0) pids2 = set(conn.pgconn.backend_pid for conn in p._pool) assert len(pids & pids2) == 3 assert pid not in pids2 diff --git a/tests/pool/test_pool_async.py b/tests/pool/test_pool_async.py index 123fb12a4..501f99da2 100644 --- a/tests/pool/test_pool_async.py +++ b/tests/pool/test_pool_async.py @@ -99,7 +99,7 @@ async def test_concurrent_filling(dsn, monkeypatch, retries): async with pool.AsyncConnectionPool( dsn, minconn=5, num_workers=2 ) as p: - await p.wait_ready(1.0) + await p.wait(1.0) want_times = [0.1, 0.1, 0.2, 0.2, 0.3] assert len(times) == len(want_times) for got, want in zip(times, want_times): @@ -113,14 +113,14 @@ async def test_wait_ready(dsn, monkeypatch): async with pool.AsyncConnectionPool( dsn, minconn=4, num_workers=1 ) as p: - await p.wait_ready(0.3) + await p.wait(0.3) async with pool.AsyncConnectionPool(dsn, minconn=4, num_workers=1) as p: - await p.wait_ready(0.5) + await p.wait(0.5) async with pool.AsyncConnectionPool(dsn, minconn=4, num_workers=2) as p: - await p.wait_ready(0.3) - await p.wait_ready(0.0001) # idempotent + await p.wait(0.3) + await p.wait(0.0001) # idempotent @pytest.mark.slow @@ -129,7 +129,7 @@ async def test_setup_no_timeout(dsn, proxy): async with pool.AsyncConnectionPool( proxy.client_dsn, minconn=1, num_workers=1 ) as p: - await p.wait_ready(0.2) + await p.wait(0.2) async with pool.AsyncConnectionPool( proxy.client_dsn, minconn=1, num_workers=1 @@ -177,7 +177,7 @@ async def test_configure_broken(dsn, caplog): async with pool.AsyncConnectionPool(minconn=1, configure=configure) as p: with pytest.raises(pool.PoolTimeout): - await p.wait_ready(timeout=0.5) + await p.wait(timeout=0.5) assert caplog.records assert "WAT" in caplog.records[0].message @@ -509,7 +509,7 @@ async def test_grow(dsn, monkeypatch, retries): async with pool.AsyncConnectionPool( dsn, minconn=2, maxconn=4, num_workers=3 ) as p: - await p.wait_ready(1.0) + await p.wait(1.0) ts = [] results = [] @@ -545,7 +545,7 @@ async def test_shrink(dsn, monkeypatch): async with pool.AsyncConnectionPool( dsn, minconn=2, maxconn=4, max_idle=0.2 ) as p: - await p.wait_ready(5.0) + await p.wait(5.0) assert p.max_idle == 0.2 ts = [create_task(worker(i)) for i in range(4)] @@ -565,7 +565,7 @@ async def test_reconnect(proxy, caplog, monkeypatch): proxy.start() async with pool.AsyncConnectionPool(proxy.client_dsn, minconn=1) as p: - await p.wait_ready(2.0) + await p.wait(2.0) proxy.stop() with pytest.raises(psycopg3.OperationalError): @@ -574,7 +574,7 @@ async def test_reconnect(proxy, caplog, monkeypatch): await asyncio.sleep(1.0) proxy.start() - await p.wait_ready() + await p.wait() async with p.connection() as conn: await conn.execute("select 1") @@ -613,7 +613,7 @@ async def test_reconnect_failure(proxy): reconnect_timeout=1.0, reconnect_failed=failed, ) as p: - await p.wait_ready(2.0) + await p.wait(2.0) proxy.stop() with pytest.raises(psycopg3.OperationalError): @@ -711,11 +711,11 @@ async def test_max_lifetime(dsn): async def test_check(dsn, caplog): caplog.set_level(logging.WARNING, logger="psycopg3.pool") async with pool.AsyncConnectionPool(dsn, minconn=4) as p: - await p.wait_ready(1.0) + await p.wait(1.0) async with p.connection() as conn: pid = conn.pgconn.backend_pid - await p.wait_ready(1.0) + await p.wait(1.0) pids = set(conn.pgconn.backend_pid for conn in p._pool) assert pid in pids await conn.close() @@ -723,7 +723,7 @@ async def test_check(dsn, caplog): assert len(caplog.records) == 0 await p.check() assert len(caplog.records) == 1 - await p.wait_ready(1.0) + await p.wait(1.0) pids2 = set(conn.pgconn.backend_pid for conn in p._pool) assert len(pids & pids2) == 3 assert pid not in pids2 -- 2.47.2