]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Raise ValueError if the pool min_size is <= 0
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 3 Jan 2022 18:52:44 +0000 (19:52 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 5 Jan 2022 22:13:28 +0000 (23:13 +0100)
Before it would have created a broken pool, blocking forever on getconn.

docs/news_pool.rst
psycopg_pool/psycopg_pool/base.py
tests/pool/test_pool.py
tests/pool/test_pool_async.py

index 0a6b2055b3c1a9758f760c9ffa7d886d7e985d1a..c7ad91298e20e6b328f6f684c3a7ca5fb88d0362 100644 (file)
@@ -17,6 +17,13 @@ psycopg_pool 3.1.0 (unreleased)
   (:ticket:`#151`).
 
 
+psycopg_pool 3.0.3 (unreleased)
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+- Throw `!ValueError` if the pool `!min_size` is set to 0 (instead of
+  hanging).
+
+
 Current release
 ---------------
 
index 70ecd0ed1ea1251a21c292ece4b694bdbf7cb183..251256a5746b1adab9f07178d4b810a508206874 100644 (file)
@@ -121,6 +121,9 @@ class BasePool(Generic[ConnectionType]):
     def _check_size(
         self, min_size: int, max_size: Optional[int]
     ) -> Tuple[int, int]:
+        if min_size <= 0:
+            raise ValueError("min_size must be greater than 0")
+
         if max_size is None:
             max_size = min_size
         if max_size < min_size:
index 7f23551770a95297b7bfe1712e430e1e4cc87776..f4d249e7a3a373bd222d53835fb8df1c30ca88f6 100644 (file)
@@ -49,8 +49,11 @@ def test_min_size_max_size(dsn):
         assert p.min_size == 2
         assert p.max_size == 4
 
+
+@pytest.mark.parametrize("min_size, max_size", [(0, 0), (-1, None), (4, 2)])
+def test_bad_size(dsn, min_size, max_size):
     with pytest.raises(ValueError):
-        pool.ConnectionPool(dsn, min_size=4, max_size=2)
+        pool.ConnectionPool(min_size=min_size, max_size=max_size)
 
 
 def test_connection_class(dsn):
@@ -954,6 +957,13 @@ def test_resize(dsn):
     assert size == [2, 1, 3, 4, 3, 2, 2]
 
 
+@pytest.mark.parametrize("min_size, max_size", [(0, 0), (-1, None), (4, 2)])
+def test_bad_resize(dsn, min_size, max_size):
+    with pool.ConnectionPool() as p:
+        with pytest.raises(ValueError):
+            p.resize(min_size=min_size, max_size=max_size)
+
+
 def test_jitter():
     rnds = [pool.ConnectionPool._jitter(30, -0.1, +0.2) for i in range(100)]
     assert 27 <= min(rnds) <= 28
index f4c918ba706619bb7b4df0d38c244136fea83a37..9a0a6d5a46f4d9da3dc107e11e890e818b023cdb 100644 (file)
@@ -44,8 +44,11 @@ async def test_min_size_max_size(dsn):
         assert p.min_size == 2
         assert p.max_size == 4
 
+
+@pytest.mark.parametrize("min_size, max_size", [(0, 0), (-1, None), (4, 2)])
+async def test_bad_size(dsn, min_size, max_size):
     with pytest.raises(ValueError):
-        pool.AsyncConnectionPool(dsn, min_size=4, max_size=2)
+        pool.AsyncConnectionPool(min_size=min_size, max_size=max_size)
 
 
 async def test_connection_class(dsn):
@@ -929,6 +932,13 @@ async def test_resize(dsn):
     assert size == [2, 1, 3, 4, 3, 2, 2]
 
 
+@pytest.mark.parametrize("min_size, max_size", [(0, 0), (-1, None), (4, 2)])
+async def test_bad_resize(dsn, min_size, max_size):
+    async with pool.AsyncConnectionPool() as p:
+        with pytest.raises(ValueError):
+            await p.resize(min_size=min_size, max_size=max_size)
+
+
 def test_jitter():
     rnds = [
         pool.AsyncConnectionPool._jitter(30, -0.1, +0.2) for i in range(100)