Skip to content

Commit df3b9e9

Browse files
Close various objects created during asyncio tests (#3005)
* Close various objects created during asyncio tests * Fix resource leake in test_cwe_404.py Need to wait for individual handler tasks when shutting down server.
1 parent 5391c5f commit df3b9e9

7 files changed

+34
-15
lines changed

tests/test_asyncio/test_commands.py

+2
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,12 @@ async def test_client_setinfo(self, r: redis.Redis):
370370
info = await r2.client_info()
371371
assert info["lib-name"] == "test2"
372372
assert info["lib-ver"] == "1234"
373+
await r2.aclose()
373374
r3 = redis.asyncio.Redis(lib_name=None, lib_version=None)
374375
info = await r3.client_info()
375376
assert info["lib-name"] == ""
376377
assert info["lib-ver"] == ""
378+
await r3.aclose()
377379

378380
@skip_if_server_version_lt("2.6.9")
379381
@pytest.mark.onlynoncluster

tests/test_asyncio/test_connect.py

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ async def _handler(reader, writer):
7373
try:
7474
return await _redis_request_handler(reader, writer, stop_event)
7575
finally:
76+
writer.close()
77+
await writer.wait_closed()
7678
finished.set()
7779

7880
if isinstance(server_address, str):

tests/test_asyncio/test_connection.py

+4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ async def get_conn(_):
8585

8686
assert init_call_count == 1
8787
assert command_call_count == 2
88+
r.connection = None # it was a Mock
89+
await r.aclose()
8890

8991

9092
@skip_if_server_version_lt("4.0.0")
@@ -143,6 +145,7 @@ async def mock_connect():
143145
conn._connect.side_effect = mock_connect
144146
await conn.connect()
145147
assert conn._connect.call_count == 3
148+
await conn.disconnect()
146149

147150

148151
async def test_connect_without_retry_on_os_error():
@@ -194,6 +197,7 @@ async def test_connection_parse_response_resume(r: redis.Redis):
194197
pytest.fail("didn't receive a response")
195198
assert response
196199
assert i > 0
200+
await conn.disconnect()
197201

198202

199203
@pytest.mark.onlynoncluster

tests/test_asyncio/test_cwe_404.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ def __init__(self, addr, redis_addr, delay: float = 0.0):
1515
self.send_event = asyncio.Event()
1616
self.server = None
1717
self.task = None
18+
self.cond = asyncio.Condition()
19+
self.running = 0
1820

1921
async def __aenter__(self):
2022
await self.start()
@@ -63,10 +65,10 @@ async def stop(self):
6365
except asyncio.CancelledError:
6466
pass
6567
await self.server.wait_closed()
66-
# do we need to close individual connections too?
67-
# prudently close all async generators
68-
loop = self.server.get_loop()
69-
await loop.shutdown_asyncgens()
68+
# Server does not wait for all spawned tasks. We must do that also to ensure
69+
# that all sockets are closed.
70+
async with self.cond:
71+
await self.cond.wait_for(lambda: self.running == 0)
7072

7173
async def pipe(
7274
self,
@@ -75,6 +77,7 @@ async def pipe(
7577
name="",
7678
event: asyncio.Event = None,
7779
):
80+
self.running += 1
7881
try:
7982
while True:
8083
data = await reader.read(1000)
@@ -94,6 +97,10 @@ async def pipe(
9497
# ignore errors on close pertaining to no event loop. Don't want
9598
# to clutter the test output with errors if being garbage collected
9699
pass
100+
async with self.cond:
101+
self.running -= 1
102+
if self.running == 0:
103+
self.cond.notify_all()
97104

98105

99106
@pytest.mark.onlynoncluster

tests/test_asyncio/test_retry.py

+3
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,8 @@ async def test_get_set_retry_object(self, request):
131131
assert r.get_retry()._retries == new_retry_policy._retries
132132
assert isinstance(r.get_retry()._backoff, ExponentialBackoff)
133133
assert exiting_conn.retry._retries == new_retry_policy._retries
134+
await r.connection_pool.release(exiting_conn)
134135
new_conn = await r.connection_pool.get_connection("_")
135136
assert new_conn.retry._retries == new_retry_policy._retries
137+
await r.connection_pool.release(new_conn)
138+
await r.aclose()

tests/test_asyncio/test_sentinel.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -183,30 +183,30 @@ async def test_discover_slaves(cluster, sentinel):
183183

184184
@pytest.mark.onlynoncluster
185185
async def test_master_for(cluster, sentinel, master_ip):
186-
master = sentinel.master_for("mymaster", db=9)
187-
assert await master.ping()
188-
assert master.connection_pool.master_address == (master_ip, 6379)
186+
async with sentinel.master_for("mymaster", db=9) as master:
187+
assert await master.ping()
188+
assert master.connection_pool.master_address == (master_ip, 6379)
189189

190190
# Use internal connection check
191-
master = sentinel.master_for("mymaster", db=9, check_connection=True)
192-
assert await master.ping()
191+
async with sentinel.master_for("mymaster", db=9, check_connection=True) as master:
192+
assert await master.ping()
193193

194194

195195
@pytest.mark.onlynoncluster
196196
async def test_slave_for(cluster, sentinel):
197197
cluster.slaves = [
198198
{"ip": "127.0.0.1", "port": 6379, "is_odown": False, "is_sdown": False}
199199
]
200-
slave = sentinel.slave_for("mymaster", db=9)
201-
assert await slave.ping()
200+
async with sentinel.slave_for("mymaster", db=9) as slave:
201+
assert await slave.ping()
202202

203203

204204
@pytest.mark.onlynoncluster
205205
async def test_slave_for_slave_not_found_error(cluster, sentinel):
206206
cluster.master["is_odown"] = True
207-
slave = sentinel.slave_for("mymaster", db=9)
208-
with pytest.raises(SlaveNotFoundError):
209-
await slave.ping()
207+
async with sentinel.slave_for("mymaster", db=9) as slave:
208+
with pytest.raises(SlaveNotFoundError):
209+
await slave.ping()
210210

211211

212212
@pytest.mark.onlynoncluster
@@ -260,7 +260,7 @@ async def mock_disconnect():
260260
calls += 1
261261

262262
with mock.patch.object(pool, "disconnect", mock_disconnect):
263-
await client.close()
263+
await client.aclose()
264264

265265
assert calls == 1
266266
await pool.disconnect()

tests/test_asyncio/test_sentinel_managed_connection.py

+1
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ async def mock_connect():
3434
conn._connect.side_effect = mock_connect
3535
await conn.connect()
3636
assert conn._connect.call_count == 3
37+
await conn.disconnect()

0 commit comments

Comments
 (0)