From 2306eac3910216a65bed40bc214e020a4ce9ae75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iva=CC=81n=20Raskovsky?= Date: Thu, 6 Jul 2023 13:11:20 -0300 Subject: [PATCH] add test for issue #2025 --- tests/test_inmemorychannel.py | 55 ++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/tests/test_inmemorychannel.py b/tests/test_inmemorychannel.py index 3f05ed7e..d5a568c6 100644 --- a/tests/test_inmemorychannel.py +++ b/tests/test_inmemorychannel.py @@ -3,8 +3,11 @@ import async_timeout import pytest +from django.test import override_settings from channels.exceptions import ChannelFull -from channels.layers import InMemoryChannelLayer +from channels.generic.websocket import AsyncWebsocketConsumer +from channels.layers import InMemoryChannelLayer, get_channel_layer +from channels.testing import WebsocketCommunicator @pytest.fixture() @@ -92,6 +95,56 @@ async def test_groups_basic(channel_layer): await channel_layer.receive("test-gr-chan-2") +@pytest.mark.asyncio +async def test_async_group_self(): + """ + Tests that AsyncWebsocketConsumer receives messages from a group while processing a previous message + """ + results = {} + + class TestConsumer(AsyncWebsocketConsumer): + groups = ["chat"] + + async def receive(self, text_data=None, bytes_data=None): + results["received"] = (text_data, bytes_data) + assert text_data == "hello" + await self.channel_layer.group_send("chat", {"type": "chat.message", "message": "message1"}) + await self.channel_layer.group_send("chat", {"type": "chat.message", "message": "message2"}) + await asyncio.sleep(0.1) + await self.send(text_data="message3") + + async def chat_message(self, event): + await self.send(text_data=event["message"]) + + app = TestConsumer() + + channel_layers_setting = { + "default": {"BACKEND": "channels.layers.InMemoryChannelLayer"} + } + with override_settings(CHANNEL_LAYERS=channel_layers_setting): + communicator = WebsocketCommunicator(app, "/testws/") + await communicator.connect() + + channel_layer = get_channel_layer() + + message = {"type": "websocket.receive", "text": "hello"} + await channel_layer.group_send("chat", message) + + responses = [] + response = await communicator.receive_from() + responses.append(response) + + response = await communicator.receive_from() + responses.append(response) + + response = await communicator.receive_from() + responses.append(response) + + await communicator.disconnect() + + assert responses == ["message1", "message2", "message3"] + + @pytest.mark.asyncio async def test_groups_channel_full(channel_layer): """