11
11
_AsyncRESP3Parser ,
12
12
_AsyncRESPBase ,
13
13
)
14
- from redis .asyncio import Redis
14
+ from redis .asyncio import ConnectionPool , Redis
15
15
from redis .asyncio .connection import Connection , UnixDomainSocketConnection , parse_url
16
16
from redis .asyncio .retry import Retry
17
17
from redis .backoff import NoBackoff
@@ -289,7 +289,7 @@ def test_create_single_connection_client_from_url():
289
289
assert client .single_connection_client is True
290
290
291
291
292
- @pytest .mark .parametrize ("from_url" , (True , False ))
292
+ @pytest .mark .parametrize ("from_url" , (True , False ), ids = ( "from_url" , "from_args" ) )
293
293
async def test_pool_auto_close (request , from_url ):
294
294
"""Verify that basic Redis instances have auto_close_connection_pool set to True"""
295
295
@@ -306,20 +306,81 @@ async def get_redis_connection():
306
306
await r1 .close ()
307
307
308
308
309
- @pytest .mark .parametrize ("from_url" , (True , False ))
310
- async def test_pool_auto_close_disable (request , from_url ):
309
+ async def test_pool_auto_close_disable (request ):
311
310
"""Verify that auto_close_connection_pool can be disabled"""
312
311
313
312
url : str = request .config .getoption ("--redis-url" )
314
313
url_args = parse_url (url )
315
314
316
315
async def get_redis_connection ():
317
- if from_url :
318
- return Redis .from_url (url , auto_close_connection_pool = False )
319
316
url_args ["auto_close_connection_pool" ] = False
320
317
return Redis (** url_args )
321
318
322
319
r1 = await get_redis_connection ()
323
320
assert r1 .auto_close_connection_pool is False
324
321
await r1 .connection_pool .disconnect ()
325
322
await r1 .close ()
323
+
324
+
325
+ @pytest .mark .parametrize ("from_url" , (True , False ), ids = ("from_url" , "from_args" ))
326
+ async def test_redis_connection_pool (request , from_url ):
327
+ """Verify that basic Redis instances using `connection_pool`
328
+ have auto_close_connection_pool set to False"""
329
+
330
+ url : str = request .config .getoption ("--redis-url" )
331
+ url_args = parse_url (url )
332
+
333
+ pool = None
334
+
335
+ async def get_redis_connection ():
336
+ nonlocal pool
337
+ if from_url :
338
+ pool = ConnectionPool .from_url (url )
339
+ else :
340
+ pool = ConnectionPool (** url_args )
341
+ return Redis (connection_pool = pool )
342
+
343
+ called = 0
344
+
345
+ async def mock_disconnect (_ ):
346
+ nonlocal called
347
+ called += 1
348
+
349
+ with patch .object (ConnectionPool , "disconnect" , mock_disconnect ):
350
+ async with await get_redis_connection () as r1 :
351
+ assert r1 .auto_close_connection_pool is False
352
+
353
+ assert called == 0
354
+ await pool .disconnect ()
355
+
356
+
357
+ @pytest .mark .parametrize ("from_url" , (True , False ), ids = ("from_url" , "from_args" ))
358
+ async def test_redis_from_pool (request , from_url ):
359
+ """Verify that basic Redis instances using `from_pool`
360
+ have auto_close_connection_pool set to True"""
361
+
362
+ url : str = request .config .getoption ("--redis-url" )
363
+ url_args = parse_url (url )
364
+
365
+ pool = None
366
+
367
+ async def get_redis_connection ():
368
+ nonlocal pool
369
+ if from_url :
370
+ pool = ConnectionPool .from_url (url )
371
+ else :
372
+ pool = ConnectionPool (** url_args )
373
+ return Redis (from_pool = pool )
374
+
375
+ called = 0
376
+
377
+ async def mock_disconnect (_ ):
378
+ nonlocal called
379
+ called += 1
380
+
381
+ with patch .object (ConnectionPool , "disconnect" , mock_disconnect ):
382
+ async with await get_redis_connection () as r1 :
383
+ assert r1 .auto_close_connection_pool is True
384
+
385
+ assert called == 1
386
+ await pool .disconnect ()
0 commit comments