From: Daniele Varrazzo Date: Mon, 3 Jan 2022 18:52:44 +0000 (+0100) Subject: Raise ValueError if the pool min_size is <= 0 X-Git-Tag: pool-3.1~41^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=81ee74d830ea7b54aae365a6febaabdbe59bfeda;p=thirdparty%2Fpsycopg.git Raise ValueError if the pool min_size is <= 0 Before it would have created a broken pool, blocking forever on getconn. --- diff --git a/docs/news_pool.rst b/docs/news_pool.rst index 0a6b2055b..c7ad91298 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 70ecd0ed1..251256a57 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 7f2355177..f4d249e7a 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 f4c918ba7..9a0a6d5a4 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)