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

Test case for issue #2025 #2027

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion tests/test_inmemorychannel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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):
"""
Expand Down