Skip to content

Commit

Permalink
Fix BlockingConnectionPool.from_url parsing of timeout in query args #…
Browse files Browse the repository at this point in the history
…2983 (#2984)

Co-authored-by: Romain Fliedel <romain@oqee.tv>
  • Loading branch information
r0ro and Romain Fliedel committed Nov 13, 2023
1 parent d3a3ada commit c2d5596
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions redis/asyncio/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,7 @@ def to_bool(value) -> Optional[bool]:
"max_connections": int,
"health_check_interval": int,
"ssl_check_hostname": to_bool,
"timeout": float,
}
)

Expand Down
1 change: 1 addition & 0 deletions redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,7 @@ def to_bool(value):
"max_connections": int,
"health_check_interval": int,
"ssl_check_hostname": to_bool,
"timeout": float,
}


Expand Down
25 changes: 25 additions & 0 deletions tests/test_asyncio/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
25 changes: 25 additions & 0 deletions tests/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit c2d5596

Please sign in to comment.