]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Report if the pool was closed, or never opened, in getconn() error message
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 3 Jan 2022 14:37:54 +0000 (15:37 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 3 Jan 2022 15:41:10 +0000 (16:41 +0100)
psycopg_pool/psycopg_pool/base.py
psycopg_pool/psycopg_pool/pool.py
psycopg_pool/psycopg_pool/pool_async.py
tests/pool/test_pool.py
tests/pool/test_pool_async.py

index dc34528fbebbe7f647346208282d2b5326f8b3eb..4079caebc23463163fd271d97c564c0be89deff5 100644 (file)
@@ -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.
index 69310829751f31010f48102cd8d00e139d29a9b6..beacc0fb43681f649c866bf26db2246f83b724f3 100644 (file)
@@ -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:
index daa432678cd54bb76b7e7fcf5922f377a62e06da..d52551b3caa475da43f044b27de99ad4bdffef42 100644 (file)
@@ -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:
index 898c2ba0303d297693dd0e05d5ddc6651c1c91a2..441e4764e5b14f5f2d1b3eefa7b3110d6d4c4277 100644 (file)
@@ -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)
index 876a1e8cd7fccb22292277076fcc90d51939a8fd..5a5162a8afe6e830b0452cca4413c5c070bfd51e 100644 (file)
@@ -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)