[issue46568] non awaited coroutines on a IsolatedAsyncioTestCase results on a RuntimeWarning
bluecarrot
report at bugs.python.org
Sat Jan 29 03:45:57 EST 2022
New submission from bluecarrot <felix at kngnt.org>:
I am unittesting a tcp proxy module using coroutines. This is the coroutine I am using to do the forwarding, allowing the writer stream to drain while the rest of the coroutines are proceeding:
async def forward_stream(reader: StreamReader, writer: StreamWriter, event: asyncio.Event, source: str):
writer_drain = writer.drain()
while not event.is_set():
try:
data = await asyncio.wait_for(reader.read(1024), 1)
except asyncio.TimeoutError:
continue
if not data:
event.set()
break
# parse the data
if reading := parse(data):
# wait for the previous write to finish, and forward the data to the other end, process the data in between
await writer_drain
writer.write(data)
writer_drain = writer.drain()
# wait for any outstanding write buffer to be flushed
await writer_drain
logger.info("{} reader forwarder finished.".format(source))
In my unit tests, I have the following (EnergyAgentProxy is the wrapper calling the coroutine in the module that creates the proxy)
class TestConnections(IsolatedAsyncioTestCase):
async def asyncSetUp(self) -> None:
self.proxy = asyncio.create_task(EnergyAgentProxy(self.proxy_port, self.server_port, self.upstream_port))
The problem is: When running these tests, I am getting the following error:
/usr/lib/python3.10/unittest/async_case.py:159: RuntimeWarning: coroutine 'StreamWriter.drain' was never awaited
Coroutine created at (most recent call last)
File "/usr/lib/python3.10/unittest/case.py", line 650, in __call__
return self.run(*args, **kwds)
[...]
File "/home/frubio/Documents/powermonitor_raspberrypi/EnergyAgent.py", line 48, in forward_stream
writer_drain = writer.drain()
self._tearDownAsyncioLoop()
So... to me, it looks like when the tasks are being cancelled I am getting this warning because the last "await writer_drain" in forward stream is not executed, but I cannot ensure that. Am I doing something wrong? Is there any way I can just prevent this warning from showing up in my tests?
----------
components: Tests, asyncio
messages: 412060
nosy: asvetlov, bluecarrot, yselivanov
priority: normal
severity: normal
status: open
title: non awaited coroutines on a IsolatedAsyncioTestCase results on a RuntimeWarning
type: behavior
versions: Python 3.10
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46568>
_______________________________________
More information about the Python-bugs-list
mailing list