Skip to content

Commit

Permalink
Add tests for the __del__ handlers of async Redis and Connection objects
Browse files Browse the repository at this point in the history
  • Loading branch information
kristjanvalur committed Oct 13, 2023
1 parent 8a6a114 commit 2ad2364
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tests/test_asyncio/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,49 @@ async def mock_disconnect(_):

assert called == 0
await pool.disconnect()


async def test_client_garbage_collection(request):
"""
Test that a Redis client will call _close() on any
connection that it holds at time of destruction
"""

url: str = request.config.getoption("--redis-url")
pool = ConnectionPool.from_url(url)

# create a client with a connection from the pool
client = Redis(connection_pool=pool, single_connection_client=True)
await client.initialize()
with mock.patch.object(client, "connection") as a:
# we cannot, in unittests, or from asyncio, reliably trigger garbage collection
# so we must just invoke the handler
client.__del__()
assert a._close.called

await client.aclose()
await pool.aclose()


async def test_connection_garbage_collection(request):
"""
Test that a Connection object will call close() on the
stream that it holds.
"""

url: str = request.config.getoption("--redis-url")
pool = ConnectionPool.from_url(url)

# create a client with a connection from the pool
client = Redis(connection_pool=pool, single_connection_client=True)
await client.initialize()
conn = client.connection

with mock.patch.object(conn, "_writer") as a:
# we cannot, in unittests, or from asyncio, reliably trigger garbage collection
# so we must just invoke the handler
conn.__del__()
assert a.close.called

await client.aclose()
await pool.aclose()

0 comments on commit 2ad2364

Please sign in to comment.