@@ -436,3 +436,49 @@ async def mock_disconnect(_):
436
436
437
437
assert called == 0
438
438
await pool .disconnect ()
439
+
440
+
441
+ async def test_client_garbage_collection (request ):
442
+ """
443
+ Test that a Redis client will call _close() on any
444
+ connection that it holds at time of destruction
445
+ """
446
+
447
+ url : str = request .config .getoption ("--redis-url" )
448
+ pool = ConnectionPool .from_url (url )
449
+
450
+ # create a client with a connection from the pool
451
+ client = Redis (connection_pool = pool , single_connection_client = True )
452
+ await client .initialize ()
453
+ with mock .patch .object (client , "connection" ) as a :
454
+ # we cannot, in unittests, or from asyncio, reliably trigger garbage collection
455
+ # so we must just invoke the handler
456
+ client .__del__ ()
457
+ assert a ._close .called
458
+
459
+ await client .aclose ()
460
+ await pool .aclose ()
461
+
462
+
463
+ async def test_connection_garbage_collection (request ):
464
+ """
465
+ Test that a Connection object will call close() on the
466
+ stream that it holds.
467
+ """
468
+
469
+ url : str = request .config .getoption ("--redis-url" )
470
+ pool = ConnectionPool .from_url (url )
471
+
472
+ # create a client with a connection from the pool
473
+ client = Redis (connection_pool = pool , single_connection_client = True )
474
+ await client .initialize ()
475
+ conn = client .connection
476
+
477
+ with mock .patch .object (conn , "_writer" ) as a :
478
+ # we cannot, in unittests, or from asyncio, reliably trigger garbage collection
479
+ # so we must just invoke the handler
480
+ conn .__del__ ()
481
+ assert a .close .called
482
+
483
+ await client .aclose ()
484
+ await pool .aclose ()
0 commit comments