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