Skip to content

Commit 2ad2364

Browse files
committed
Add tests for the __del__ handlers of async Redis and Connection objects
1 parent 8a6a114 commit 2ad2364

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

tests/test_asyncio/test_connection.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,3 +436,49 @@ async def mock_disconnect(_):
436436

437437
assert called == 0
438438
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

Comments
 (0)