django / channels

Developer-friendly asynchrony for Django
https://channels.readthedocs.io
BSD 3-Clause "New" or "Revised" License
6.02k stars 793 forks source link

Accessing Database while testing Consumer with WebsocketCommunicator blocks running of test suite on Ubuntu (works on MacOS) #2007

Open Milutinke92 opened 1 year ago

Milutinke92 commented 1 year ago

Sorry, I know this is not a place for "debuging" and asking questions, but there isn't too big community on Stack Overflow for this.

Consumer connect

class EditResourceConsumer(JsonWebsocketConsumer):
    def connect(self):
        self.accept()

        draft_id = self.scope["url_route"]["kwargs"]["id"]

        draft.edited_by = self.scope["user"]
        draft.save()
        self.send_message("success")
@freeze_time("2023-03-07 13:06:00")
class ResourceWSTestCase(TransactionTestCase):
    @aioresponses()
    async def test_connect_with_correct_token_edit_resource_draft(
        self, mocked_io_request
    ):
        communicator = WebsocketCommunicator(
            application, f"/ws/draft/1/?token={self.token_john}"
        )
        connected, subprotocol = await communicator.connect()
        self.assertTrue(connected)

        event = await communicator.receive_output()
        draft = (
            await ResourceDraft.objects.filter(pk=self.draft.pk)
            .select_related("edited_by")
            .afirst()
        )
        self.assertEqual(draft.edited_by, self.user_john)
        self.assertEqual(event["type"], "websocket.send")
        self.assertDictEqual(
            json.loads(event["text"]), {"code": "success", "message": "Success"}
        )

Code gets stuck on:

        draft = await ResourceDraft.objects.filter(pk=self.draft.pk).select_related("edited_by").afirst()

There is no logs.

This code and tests runs successfully on MacOS but it fails on Ubuntu. We are running our app using docker image python:3.11

Am I doing something wrong?

carltongibson commented 1 year ago

Grrr. There's too much going on there to be able to say anything. Can you reduce it at all?

Milutinke92 commented 1 year ago

@carltongibson Try it now please. I want to test if draft.edited_by is set properly after .connect().