Skip to content
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

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

Open
Milutinke92 opened this issue Apr 19, 2023 · 2 comments

Comments

@Milutinke92
Copy link

Milutinke92 commented Apr 19, 2023

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.

  • Your OS and runtime environment, and browser if applicable:
    OS: MacOS and Linux

  • pip freeze:

Django==4.2
channels==4.0.0

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")
  • What you expected to happen vs. what actually happened:
    I want to access object from the database after calling .connect() method on WebsocketCommunicator., but instead, running is blocked on the line mentioned bellow.
    Code example:
@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
Copy link
Member

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

@Milutinke92
Copy link
Author

Milutinke92 commented Apr 19, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants