From: Daniele Varrazzo Date: Mon, 3 Jan 2022 14:37:54 +0000 (+0100) Subject: Report if the pool was closed, or never opened, in getconn() error message X-Git-Tag: pool-3.1~45^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=eb75b16085741f0e5d84c5466c75af52660ee501;p=thirdparty%2Fpsycopg.git Report if the pool was closed, or never opened, in getconn() error message --- diff --git a/psycopg_pool/psycopg_pool/base.py b/psycopg_pool/psycopg_pool/base.py index dc34528fb..4079caebc 100644 --- a/psycopg_pool/psycopg_pool/base.py +++ b/psycopg_pool/psycopg_pool/base.py @@ -9,6 +9,7 @@ from typing import Any, Callable, Dict, Generic, Optional from psycopg.abc import ConnectionType from psycopg import errors as e +from .errors import PoolClosed from ._compat import Counter, Deque @@ -124,6 +125,17 @@ class BasePool(Generic[ConnectionType]): "pool has already been opened/closed and cannot be reused" ) + def _check_open_getconn(self) -> None: + if self._closed: + if self._opened: + raise PoolClosed( + f"the pool {self.name!r} has already been closed" + ) + else: + raise PoolClosed( + f"the pool {self.name!r} has not been opened yet" + ) + def get_stats(self) -> Dict[str, int]: """ Return current stats about the pool usage. diff --git a/psycopg_pool/psycopg_pool/pool.py b/psycopg_pool/psycopg_pool/pool.py index 693108297..beacc0fb4 100644 --- a/psycopg_pool/psycopg_pool/pool.py +++ b/psycopg_pool/psycopg_pool/pool.py @@ -137,8 +137,7 @@ class ConnectionPool(BasePool[Connection[Any]]): # Critical section: decide here if there's a connection ready # or if the client needs to wait. with self._lock: - if self._closed: - raise PoolClosed(f"the pool {self.name!r} is closed") + self._check_open_getconn() pos: Optional[WaitingClient] = None if self._pool: diff --git a/psycopg_pool/psycopg_pool/pool_async.py b/psycopg_pool/psycopg_pool/pool_async.py index daa432678..d52551b3c 100644 --- a/psycopg_pool/psycopg_pool/pool_async.py +++ b/psycopg_pool/psycopg_pool/pool_async.py @@ -111,8 +111,7 @@ class AsyncConnectionPool(BasePool[AsyncConnection[Any]]): # Critical section: decide here if there's a connection ready # or if the client needs to wait. async with self._lock: - if self._closed: - raise PoolClosed(f"the pool {self.name!r} is closed") + self._check_open_getconn() pos: Optional[AsyncClient] = None if self._pool: diff --git a/tests/pool/test_pool.py b/tests/pool/test_pool.py index 898c2ba03..441e4764e 100644 --- a/tests/pool/test_pool.py +++ b/tests/pool/test_pool.py @@ -687,7 +687,7 @@ def test_closed_queue(dsn): def test_open_explicit(dsn): p = pool.ConnectionPool(dsn, open=False) assert p.closed - with pytest.raises(pool.PoolClosed): + with pytest.raises(pool.PoolClosed, match="has not been opened yet"): p.getconn() with pytest.raises(pool.PoolClosed): @@ -705,6 +705,9 @@ def test_open_explicit(dsn): finally: p.close() + with pytest.raises(pool.PoolClosed, match="has already been closed"): + p.getconn() + def test_open_context(dsn): p = pool.ConnectionPool(dsn, open=False) diff --git a/tests/pool/test_pool_async.py b/tests/pool/test_pool_async.py index 876a1e8cd..5a5162a8a 100644 --- a/tests/pool/test_pool_async.py +++ b/tests/pool/test_pool_async.py @@ -679,7 +679,7 @@ async def test_open_explicit(dsn): with pytest.raises(pool.PoolClosed): await p.getconn() - with pytest.raises(pool.PoolClosed): + with pytest.raises(pool.PoolClosed, match="has not been opened yet"): async with p.connection(): pass @@ -694,6 +694,9 @@ async def test_open_explicit(dsn): finally: await p.close() + with pytest.raises(pool.PoolClosed, match="has already been closed"): + await p.getconn() + async def test_open_context(dsn): p = pool.AsyncConnectionPool(dsn, open=False)