Skip to content

Commit 57e898e

Browse files
committed
test(connection): tighten the Retry conversion tests
Use a non-default retry count so a converter that ignored the caller's count would fail, drop two assertions the remaining test already covers, and correct the integration test's docstring: the immediate ping after CLIENT KILL fails on main on redis-py 6.3 as well as 8.1, so the test exercises the policy on every supported version.
1 parent 6aa6057 commit 57e898e

2 files changed

Lines changed: 11 additions & 20 deletions

File tree

‎tests/integration/test_connection.py‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,11 @@ async def test_validate_async_redis_custom_lib_name(async_client):
181181
async def test_converted_client_recovers_after_client_kill(client, redis_url):
182182
"""The converted client's retry policy must run on the async connection.
183183
184-
After CLIENT KILL the pooled connection's socket is closed. On redis-py
185-
8.1+ the pool hands it out as is, so the next command fails once and only
186-
the async Retry's reconnect-and-retry makes it succeed; a sync Retry would
187-
raise ConnectionError. Earlier pools reconnect a dead connection at
188-
checkout, so there this test does not exercise the policy; the unit tests
189-
pin the conversion on every version.
184+
After CLIENT KILL the pooled connection's socket is closed. When the ping
185+
follows straight away, the event loop has not yet seen the server close
186+
it, so the pool hands the connection out and the first attempt fails. Only
187+
the async Retry's reconnect-and-retry makes the ping succeed; with a sync
188+
Retry it raises ConnectionError.
190189
"""
191190
sync_client = Redis.from_url(redis_url, retry=Retry(NoBackoff(), 1))
192191
converted = RedisConnectionFactory.sync_to_async_redis(sync_client)

‎tests/unit/test_sync_to_async_redis.py‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
)
1414
from redis.asyncio.retry import Retry as AsyncRetry
1515
from redis.backoff import ExponentialBackoff
16-
from redis.connection import Connection, ConnectionPool, SSLConnection
16+
from redis.connection import ConnectionPool, SSLConnection
1717
from redis.exceptions import ConnectionError
1818
from redis.retry import Retry
1919
from redis.sentinel import Sentinel
@@ -123,31 +123,23 @@ def test_explicitly_enabled_maintenance_notifications_are_not_dropped():
123123

124124
def test_sync_retry_is_rebuilt_as_the_async_retry():
125125
# The async connection awaits call_with_retry, so a sync Retry would hand
126-
# it an unawaited coroutine and the policy would never run.
126+
# it an unawaited coroutine and the policy would never run. A plain
127+
# Redis() carries a sync Retry too (redis-py 6+), so this is the default
128+
# path, not an opt-in one. 5 is not redis-py's default retry count.
127129
client = Redis(
128130
host="h",
129-
retry=Retry(ExponentialBackoff(cap=2, base=0.1), 3, (ConnectionError, OSError)),
131+
retry=Retry(ExponentialBackoff(cap=2, base=0.1), 5, (ConnectionError, OSError)),
130132
)
131133
pool = RedisConnectionFactory.sync_to_async_redis(client).connection_pool
132134

133135
retry = pool.make_connection().retry
134136
assert type(retry) is AsyncRetry
135137
assert retry == AsyncRetry(
136-
ExponentialBackoff(cap=2, base=0.1), 3, (ConnectionError, OSError)
138+
ExponentialBackoff(cap=2, base=0.1), 5, (ConnectionError, OSError)
137139
)
138140

139141

140-
def test_redis_py_default_retry_is_converted_too():
141-
# Since redis-py 6 every Redis() carries a sync Retry unless told otherwise.
142-
pool = RedisConnectionFactory.sync_to_async_redis(Redis(host="h")).connection_pool
143-
assert type(pool.make_connection().retry) is AsyncRetry
144-
145-
146142
def test_missing_retry_is_left_alone():
147143
client = Redis(host="h", retry=None)
148144
pool = RedisConnectionFactory.sync_to_async_redis(client).connection_pool
149145
assert pool.connection_kwargs["retry"] is None
150-
151-
assert _async_connection_kwargs({"host": "h"}, AsyncConnection, Connection) == {
152-
"host": "h"
153-
}

0 commit comments

Comments
 (0)