-
Hi, While trying to add unit tests to my project, I encountered an strange issue. Maybe a legit issue outside my code? No idea. On the server, async def test_handler(event_loop, aiohttp_client):
queue = asyncio.Queue(maxsize=3)
async def ws_handler(request):
ws = aiohttp.web.WebSocketResponse()
await ws.prepare(request)
i = 0
while True:
await asyncio.sleep(2)
await ws.send_json({"action": "wait"})
# await asyncio.sleep(2)
# await queue.put(("slept", i))
i += 1
app = aiohttp.web.Application()
app.router.add_get("/ws", ws_handler)
client = await aiohttp_client(app)
async with client.ws_connect("/ws") as ws:
assert False # 👈 Won't trigger!
rawmsg = await ws.receive()
assert (msg := ws_utils.tomsg(rawmsg))
await queue.put((">>>> ws", ws, msg))
while not queue.empty():
print(await queue.get())
assert False # To force the test to fail and get the logs When I run this test, the whole thing hangs. No logs, no errors (AFAICT). I was expecting that the assert marked with 👈 would happen but it does not! So, I added an
Why would the When I removed all Any suggestions?
In case you wonder about def tomsg(rawmsg: aiohttp.WSMessage) -> Optional[SimpleNamespace]:
logger.debug(
"rawmsg: %s\n%s",
rawmsg,
"\n".join(list(reversed(traceback.format_stack()))[0:2]),
)
if rawmsg.type != aiohttp.WSMsgType.TEXT:
return None
msg = json.loads(rawmsg.data, object_hook=lambda d: SimpleNamespace(**d))
logger.debug("msg: %s", msg)
return cast(SimpleNamespace, msg) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 16 replies
-
The assert is reached, but the test doesn't seem to exit while the handler is still active. No idea why that is happening though.
Not sure what you mean here. Your code awaits on receiving a message before printing the
No idea why the test doesn't complete as soon as the exception is hit, and continues to run the handler. |
Beta Was this translation helpful? Give feedback.
-
Here is the minimal test (hopefully clear enough) I added strings to each async def test_handler(event_loop, aiohttp_client):
async def ws_handler(request):
ws = aiohttp.web.WebSocketResponse()
await ws.prepare(request)
while True:
await asyncio.sleep(2)
await ws.send_json({"action": "wait"})
app = aiohttp.web.Application()
app.router.add_get("/ws", ws_handler)
client = await aiohttp_client(app)
try:
async with client.ws_connect("/ws") as ws:
assert False, "Before receive..."
rawmsg = await ws.receive()
assert (msg := ws_utils.tomsg(rawmsg))
except Exception as e:
print("############# '", e, "' ###########")
print(traceback.format_exc())
assert False, "At end"
And using Here's the output:
From the stack trace, it seems like the timeout is aborting a Any suggestion? |
Beta Was this translation helpful? Give feedback.
Here is the minimal test (hopefully clear enough)
I added strings to each
assert
to differentiate.