1
- import socket
2
1
from typing import Tuple
3
2
from unittest import mock
4
3
5
4
import pytest
6
- from redis .asyncio .retry import Retry
7
- from redis .asyncio .sentinel import (
5
+ from redis .sentinel import (
8
6
Sentinel ,
9
7
SentinelConnectionPool ,
10
8
SentinelManagedConnection ,
11
9
)
12
- from redis .backoff import NoBackoff
13
-
14
- pytestmark = pytest .mark .asyncio
15
-
16
-
17
- def test_connect_retry_on_timeout_error (connect_args ):
18
- """Test that the _connect function is retried in case of a timeout"""
19
- connection_pool = mock .AsyncMock ()
20
- connection_pool .get_master_address = mock .AsyncMock (
21
- return_value = (connect_args ["host" ], connect_args ["port" ])
22
- )
23
- conn = SentinelManagedConnection (
24
- retry_on_timeout = True ,
25
- retry = Retry (NoBackoff (), 3 ),
26
- connection_pool = connection_pool ,
27
- )
28
- origin_connect = conn ._connect
29
- conn ._connect = mock .AsyncMock ()
30
-
31
- async def mock_connect ():
32
- # connect only on the last retry
33
- if conn ._connect .call_count <= 2 :
34
- raise socket .timeout
35
- else :
36
- return await origin_connect ()
37
-
38
- conn ._connect .side_effect = mock_connect
39
- conn .connect ()
40
- assert conn ._connect .call_count == 3
41
- conn .disconnect ()
42
-
43
10
44
11
class SentinelManagedConnectionMock (SentinelManagedConnection ):
45
12
def connect_to_same_address (self ) -> None :
@@ -125,7 +92,7 @@ def test_connects_to_same_address_if_same_id_replica(
125
92
"""
126
93
Assert that the connection address is the same if the ``_iter_req_id`` is the same
127
94
when we are in replica mode using a
128
- :py:class:`~redis.asyncio. sentinel.SentinelConnectionPool`.
95
+ :py:class:`~redis.sentinel.SentinelConnectionPool`.
129
96
"""
130
97
connection_for_req_1 = connection_pool_replica_mock .get_connection (
131
98
"ANY" , _iter_req_id = 1
@@ -142,7 +109,7 @@ def test_connects_to_same_conn_object_if_same_id_and_conn_released_replica(
142
109
"""
143
110
Assert that the connection object is the same if the ``_iter_req_id`` is the same
144
111
when we are in replica mode using a
145
- :py:class:`~redis.asyncio. sentinel.SentinelConnectionPool`
112
+ :py:class:`~redis.sentinel.SentinelConnectionPool`
146
113
and if we release the connection back to the connection pool before
147
114
trying to connect again.
148
115
"""
@@ -185,7 +152,7 @@ def test_connects_to_same_address_if_same_iter_req_id_master(
185
152
"""
186
153
Assert that the connection address is the same if the ``_iter_req_id`` is the same
187
154
when we are in master mode using a
188
- :py:class:`~redis.asyncio. sentinel.SentinelConnectionPool`.
155
+ :py:class:`~redis.sentinel.SentinelConnectionPool`.
189
156
"""
190
157
connection_for_req_1 = connection_pool_master_mock .get_connection (
191
158
"ANY" , _iter_req_id = 1
@@ -202,7 +169,7 @@ def test_connects_to_same_conn_object_if_same_iter_req_id_and_released_master(
202
169
"""
203
170
Assert that the connection address is the same if the ``_iter_req_id`` is the same
204
171
when we are in master mode using a
205
- :py:class:`~redis.asyncio. sentinel.SentinelConnectionPool`
172
+ :py:class:`~redis.sentinel.SentinelConnectionPool`
206
173
and if we release the connection back to the connection pool before
207
174
trying to connect again.
208
175
"""
@@ -222,7 +189,7 @@ def test_connects_to_same_address_if_no_iter_req_id_master(
222
189
Assert that connection address is always the same regardless if
223
190
there's an ``iter_req_id`` or not
224
191
when we are in master mode using a
225
- :py:class:`~redis.asyncio. sentinel.SentinelConnectionPool`.
192
+ :py:class:`~redis.sentinel.SentinelConnectionPool`.
226
193
"""
227
194
connection_for_req_1 = connection_pool_master_mock .get_connection (
228
195
"ANY" , _iter_req_id = 1
@@ -244,7 +211,7 @@ def test_scan_iter_family_cleans_up(
244
211
connection_pool_replica_mock : SentinelConnectionPool ,
245
212
):
246
213
"""Test that connection pool is correctly cleaned up"""
247
- from redis . asyncio import Redis
214
+ from redis import Redis
248
215
249
216
r = Redis (connection_pool = connection_pool_replica_mock )
250
217
0 commit comments