psycopg.git / summary / log / commit / refs
commit 81ee74d830ea7b54aae365a6febaabdbe59bfeda
Author: Daniele Varrazzo <daniele.varrazzo@gmail.com>
Date: Mon Jan 03 18:52:44 2022 +0000
Raise ValueError if the pool min_size is <= 0
Before it would have created a broken pool, blocking forever on getconn.
docs/news_pool.rst | 7 +++++++
psycopg_pool/psycopg_pool/base.py | 3 +++
tests/pool/test_pool.py | 12 +++++++++++-
tests/pool/test_pool_async.py | 12 +++++++++++-
4 files changed, 32 insertions(+), 2 deletions(-)
diff --git a/docs/news_pool.rst b/docs/news_pool.rst
index 0a6b2055..c7ad9129 100644
--- a/docs/news_pool.rst
+++ b/docs/news_pool.rst
@@ -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
---------------
diff --git a/psycopg_pool/psycopg_pool/base.py b/psycopg_pool/psycopg_pool/base.py
index 70ecd0ed..251256a5 100644
--- a/psycopg_pool/psycopg_pool/base.py
+++ b/psycopg_pool/psycopg_pool/base.py
@@ -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:
diff --git a/tests/pool/test_pool.py b/tests/pool/test_pool.py
index 7f235517..f4d249e7 100644
--- a/tests/pool/test_pool.py
+++ b/tests/pool/test_pool.py
@@ -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
diff --git a/tests/pool/test_pool_async.py b/tests/pool/test_pool_async.py
index f4c918ba..9a0a6d5a 100644
--- a/tests/pool/test_pool_async.py
+++ b/tests/pool/test_pool_async.py
@@ -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)
[parent: 36d13eda37f9]