Closed maribedran closed 10 months ago
@ipmb figured out how to solve it.
If this is expected behavior, maybe the docs should reflect it. I don't know async Python enough to understand why the disconnect has to be moved to the fixture.
@pytest_asyncio.fixture
async def communicator_1():
communicator = WebsocketCommunicator(EditReportConsumer.as_asgi(), "/ws/reports/")
connected, _ = await communicator.connect()
assert connected
yield communicator
assert await communicator.receive_nothing()
await communicator.disconnect()
@pytest_asyncio.fixture
async def communicator_2():
communicator = WebsocketCommunicator(EditReportConsumer.as_asgi(), "/ws/reports/")
connected, _ = await communicator.connect()
assert connected
yield communicator
assert await communicator.receive_nothing()
await communicator.disconnect()
@pytest.mark.asyncio
async def test_failing(communicator_1, communicator_2):
await communicator_1.send_json_to({"report": 1, "action": "join"})
comm_1_msg = await communicator_1.receive_json_from()
assert comm_1_msg == {"others": 0, "locked": False, "editing": False}
await communicator_2.send_json_to({"report": 2, "action": "join"})
comm_2_msg = await communicator_2.receive_json_from()
# ERROR here:
assert comm_2_msg == {"others": 0, "locked": False, "editing": True}
@pytest.mark.asyncio
async def test_passing(communicator_1, communicator_2):
await communicator_1.send_json_to({"report": 1, "action": "join"})
comm_1_msg = await communicator_1.receive_json_from()
assert comm_1_msg == {"others": 0, "locked": False, "editing": False}
await communicator_2.send_json_to({"report": 2, "action": "join"})
comm_2_msg = await communicator_2.receive_json_from()
assert comm_2_msg == {"others": 0, "locked": False, "editing": False}
OK... so... I guess the issue is about event loops (and how pytest is creating/managing those) TBH I'd have to do some research into that… but the error must be leading to a loop shutdown. 🤔
It's coming up in your search because channels_redis
was hitting this issue in 4.0, due to the new redis-py
requirement to shut async connections before shutting down the event loop — which happens on each call to async_to_sync()
. (Channels Redis 4.1 does that automatically for you.)
If we can get a phrasing right, I'd happily take an admonition to the Channels testing docs.
This is really outside of my skill set to even begin investigating, but if you think this is an issue with how pytest-asyncio
handles event loops on test failures I'd be happy to create an issue over there.
@maribedran I suspect that it's expected behaviour... I'd have to go and read their docs.
OK, I don't think this is addressable here. Happy to reopen/take a PR if there's concrete thoughts to the contrary.
I have multiple tests for a websocket consumer where one failing test causes other tests that pass when run on their own to fail.
I initially commented on it here
The issue only happens on tests that use more than one communicator, so they're rather long. Sorry about that.
The first test fails as expected:
The second one passes when run alone and raises a
RuntimeError
when run together with the first one: