Skip to content

Fix resource warnings in unit tests #2899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/test_asyncio/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ async def test_tcp_ssl_connect(tcp_address):
socket_timeout=10,
)
await _assert_connect(conn, tcp_address, certfile=certfile, keyfile=keyfile)
await conn.disconnect()


async def _assert_connect(conn, server_address, certfile=None, keyfile=None):
Expand Down
5 changes: 2 additions & 3 deletions tests/test_asyncio/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,8 @@ async def do_close():
async def do_read():
return await conn.read_response()

reader = mock.AsyncMock()
writer = mock.AsyncMock()
writer.transport = mock.Mock()
reader = mock.Mock(spec=asyncio.StreamReader)
writer = mock.Mock(spec=asyncio.StreamWriter)
writer.transport.get_extra_info.side_effect = None

# for HiredisParser
Expand Down
47 changes: 28 additions & 19 deletions tests/test_asyncio/test_cwe_404.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ def set_delay(self, delay: float = 0.0):
async def handle(self, reader, writer):
# establish connection to redis
redis_reader, redis_writer = await asyncio.open_connection(*self.redis_addr)
try:
pipe1 = asyncio.create_task(
self.pipe(reader, redis_writer, "to redis:", self.send_event)
)
pipe2 = asyncio.create_task(self.pipe(redis_reader, writer, "from redis:"))
await asyncio.gather(pipe1, pipe2)
finally:
redis_writer.close()
pipe1 = asyncio.create_task(
self.pipe(reader, redis_writer, "to redis:", self.send_event)
)
pipe2 = asyncio.create_task(self.pipe(redis_reader, writer, "from redis:"))
await asyncio.gather(pipe1, pipe2)

async def stop(self):
# clean up enough so that we can reuse the looper
# shutdown the server
self.task.cancel()
try:
await self.task
except asyncio.CancelledError:
pass
await self.server.wait_closed()
# do we need to close individual connections too?
# prudently close all async generators
loop = self.server.get_loop()
await loop.shutdown_asyncgens()

Expand All @@ -75,16 +75,25 @@ async def pipe(
name="",
event: asyncio.Event = None,
):
while True:
data = await reader.read(1000)
if not data:
break
# print(f"{name} read {len(data)} delay {self.delay}")
if event:
event.set()
await asyncio.sleep(self.delay)
writer.write(data)
await writer.drain()
try:
while True:
data = await reader.read(1000)
if not data:
break
# print(f"{name} read {len(data)} delay {self.delay}")
if event:
event.set()
await asyncio.sleep(self.delay)
writer.write(data)
await writer.drain()
finally:
try:
writer.close()
await writer.wait_closed()
except RuntimeError:
# ignore errors on close pertaining to no event loop. Don't want
# to clutter the test output with errors if being garbage collected
pass


@pytest.mark.onlynoncluster
Expand Down