From 63c84e64189ae8e3134af2fb0d39fd502ae49b6e Mon Sep 17 00:00:00 2001 From: Romain Fliedel Date: Fri, 6 Oct 2023 01:03:44 +0200 Subject: [PATCH] Fix BlockingConnectionPool.from_url parsing of timeout in query args #2983 --- redis/asyncio/connection.py | 1 + redis/connection.py | 1 + tests/test_asyncio/test_connection_pool.py | 25 ++++++++++++++++++++++ tests/test_connection_pool.py | 25 ++++++++++++++++++++++ 4 files changed, 52 insertions(+) diff --git a/redis/asyncio/connection.py b/redis/asyncio/connection.py index 1ef9960ff3..34d555d46f 100644 --- a/redis/asyncio/connection.py +++ b/redis/asyncio/connection.py @@ -861,6 +861,7 @@ def to_bool(value) -> Optional[bool]: "max_connections": int, "health_check_interval": int, "ssl_check_hostname": to_bool, + "timeout": float, } ) diff --git a/redis/connection.py b/redis/connection.py index b39ba28f76..fead6135e0 100644 --- a/redis/connection.py +++ b/redis/connection.py @@ -853,6 +853,7 @@ def to_bool(value): "max_connections": int, "health_check_interval": int, "ssl_check_hostname": to_bool, + "timeout": float, } diff --git a/tests/test_asyncio/test_connection_pool.py b/tests/test_asyncio/test_connection_pool.py index c93fa91a39..ed90fc73fc 100644 --- a/tests/test_asyncio/test_connection_pool.py +++ b/tests/test_asyncio/test_connection_pool.py @@ -454,6 +454,31 @@ def test_invalid_scheme_raises_error(self): ) +class TestBlockingConnectionPoolURLParsing: + def test_extra_typed_querystring_options(self): + pool = redis.BlockingConnectionPool.from_url( + "redis://localhost/2?socket_timeout=20&socket_connect_timeout=10" + "&socket_keepalive=&retry_on_timeout=Yes&max_connections=10&timeout=13.37" + ) + + assert pool.connection_class == redis.Connection + assert pool.connection_kwargs == { + "host": "localhost", + "db": 2, + "socket_timeout": 20.0, + "socket_connect_timeout": 10.0, + "retry_on_timeout": True, + } + assert pool.max_connections == 10 + assert pool.timeout == 13.37 + + def test_invalid_extra_typed_querystring_options(self): + with pytest.raises(ValueError): + redis.BlockingConnectionPool.from_url( + "redis://localhost/2?timeout=_not_a_float_" + ) + + class TestConnectionPoolUnixSocketURLParsing: def test_defaults(self): pool = redis.ConnectionPool.from_url("unix:///socket") diff --git a/tests/test_connection_pool.py b/tests/test_connection_pool.py index ef70a8ff35..d1e984ee9c 100644 --- a/tests/test_connection_pool.py +++ b/tests/test_connection_pool.py @@ -359,6 +359,31 @@ def test_invalid_scheme_raises_error_when_double_slash_missing(self): ) +class TestBlockingConnectionPoolURLParsing: + def test_extra_typed_querystring_options(self): + pool = redis.BlockingConnectionPool.from_url( + "redis://localhost/2?socket_timeout=20&socket_connect_timeout=10" + "&socket_keepalive=&retry_on_timeout=Yes&max_connections=10&timeout=42" + ) + + assert pool.connection_class == redis.Connection + assert pool.connection_kwargs == { + "host": "localhost", + "db": 2, + "socket_timeout": 20.0, + "socket_connect_timeout": 10.0, + "retry_on_timeout": True, + } + assert pool.max_connections == 10 + assert pool.timeout == 42.0 + + def test_invalid_extra_typed_querystring_options(self): + with pytest.raises(ValueError): + redis.BlockingConnectionPool.from_url( + "redis://localhost/2?timeout=_not_a_float_" + ) + + class TestConnectionPoolUnixSocketURLParsing: def test_defaults(self): pool = redis.ConnectionPool.from_url("unix:///socket")