8
8
from redis .asyncio .sentinel import (
9
9
MasterNotFoundError ,
10
10
Sentinel ,
11
+ SentinelBlockingConnectionPool ,
11
12
SentinelConnectionPool ,
12
13
SlaveNotFoundError ,
13
14
)
@@ -182,40 +183,77 @@ async def test_discover_slaves(cluster, sentinel):
182
183
183
184
184
185
@pytest .mark .onlynoncluster
185
- async def test_master_for (cluster , sentinel , master_ip ):
186
- async with sentinel .master_for ("mymaster" , db = 9 ) as master :
186
+ @pytest .mark .parametrize (
187
+ "connection_pool_class" ,
188
+ [SentinelConnectionPool , SentinelBlockingConnectionPool ],
189
+ )
190
+ async def test_master_for (cluster , sentinel , master_ip , connection_pool_class ):
191
+ async with sentinel .master_for (
192
+ "mymaster" ,
193
+ db = 9 ,
194
+ connection_pool_class = connection_pool_class ,
195
+ ) as master :
187
196
assert await master .ping ()
188
197
assert master .connection_pool .master_address == (master_ip , 6379 )
189
198
190
199
# Use internal connection check
191
- async with sentinel .master_for ("mymaster" , db = 9 , check_connection = True ) as master :
200
+ async with sentinel .master_for (
201
+ "mymaster" ,
202
+ db = 9 ,
203
+ check_connection = True ,
204
+ connection_pool_class = connection_pool_class ,
205
+ ) as master :
192
206
assert await master .ping ()
193
207
194
208
195
209
@pytest .mark .onlynoncluster
196
- async def test_slave_for (cluster , sentinel ):
210
+ @pytest .mark .parametrize (
211
+ "connection_pool_class" ,
212
+ [SentinelConnectionPool , SentinelBlockingConnectionPool ],
213
+ )
214
+ async def test_slave_for (cluster , sentinel , connection_pool_class ):
197
215
cluster .slaves = [
198
216
{"ip" : "127.0.0.1" , "port" : 6379 , "is_odown" : False , "is_sdown" : False }
199
217
]
200
- async with sentinel .slave_for ("mymaster" , db = 9 ) as slave :
218
+ async with sentinel .slave_for (
219
+ "mymaster" ,
220
+ db = 9 ,
221
+ connection_pool_class = connection_pool_class ,
222
+ ) as slave :
201
223
assert await slave .ping ()
202
224
203
225
204
226
@pytest .mark .onlynoncluster
205
- async def test_slave_for_slave_not_found_error (cluster , sentinel ):
227
+ @pytest .mark .parametrize (
228
+ "connection_pool_class" ,
229
+ [SentinelConnectionPool , SentinelBlockingConnectionPool ],
230
+ )
231
+ async def test_slave_for_slave_not_found_error (
232
+ cluster ,
233
+ sentinel ,
234
+ connection_pool_class ,
235
+ ):
206
236
cluster .master ["is_odown" ] = True
207
- async with sentinel .slave_for ("mymaster" , db = 9 ) as slave :
237
+ async with sentinel .slave_for (
238
+ "mymaster" ,
239
+ db = 9 ,
240
+ connection_pool_class = connection_pool_class ,
241
+ ) as slave :
208
242
with pytest .raises (SlaveNotFoundError ):
209
243
await slave .ping ()
210
244
211
245
212
246
@pytest .mark .onlynoncluster
213
- async def test_slave_round_robin (cluster , sentinel , master_ip ):
247
+ @pytest .mark .parametrize (
248
+ "connection_pool_class" ,
249
+ [SentinelConnectionPool , SentinelBlockingConnectionPool ],
250
+ )
251
+ async def test_slave_round_robin (cluster , sentinel , master_ip , connection_pool_class ):
214
252
cluster .slaves = [
215
253
{"ip" : "slave0" , "port" : 6379 , "is_odown" : False , "is_sdown" : False },
216
254
{"ip" : "slave1" , "port" : 6379 , "is_odown" : False , "is_sdown" : False },
217
255
]
218
- pool = SentinelConnectionPool ("mymaster" , sentinel )
256
+ pool = connection_pool_class ("mymaster" , sentinel )
219
257
rotator = pool .rotate_slaves ()
220
258
assert await rotator .__anext__ () in (("slave0" , 6379 ), ("slave1" , 6379 ))
221
259
assert await rotator .__anext__ () in (("slave0" , 6379 ), ("slave1" , 6379 ))
@@ -242,15 +280,39 @@ async def test_reset(cluster, sentinel):
242
280
243
281
244
282
@pytest .mark .onlynoncluster
245
- @pytest .mark .parametrize ("method_name" , ["master_for" , "slave_for" ])
246
- async def test_auto_close_pool (cluster , sentinel , method_name ):
283
+ @pytest .mark .parametrize (
284
+ "method_name,connection_pool_class" ,
285
+ [
286
+ pytest .param (
287
+ "master_for" ,
288
+ SentinelConnectionPool ,
289
+ id = "master_for__SentinelConnectionPool" ,
290
+ ),
291
+ pytest .param (
292
+ "slave_for" ,
293
+ SentinelConnectionPool ,
294
+ id = "slave_for__SentinelConnectionPool" ,
295
+ ),
296
+ pytest .param (
297
+ "master_for" ,
298
+ SentinelBlockingConnectionPool ,
299
+ id = "master_for__SentinelBlockingConnectionPool" ,
300
+ ),
301
+ pytest .param (
302
+ "slave_for" ,
303
+ SentinelBlockingConnectionPool ,
304
+ id = "slave_for__SentinelBlockingConnectionPool" ,
305
+ ),
306
+ ]
307
+ )
308
+ async def test_auto_close_pool (cluster , sentinel , method_name , connection_pool_class ):
247
309
"""
248
310
Check that the connection pool created by the sentinel client is
249
311
automatically closed
250
312
"""
251
313
252
314
method = getattr (sentinel , method_name )
253
- client = method ("mymaster" , db = 9 )
315
+ client = method ("mymaster" , db = 9 , connection_pool_class = connection_pool_class )
254
316
pool = client .connection_pool
255
317
assert client .auto_close_connection_pool is True
256
318
calls = 0
0 commit comments