Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-yin committed Jan 18, 2024
1 parent 093375e commit de8a100
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
16 changes: 16 additions & 0 deletions src/actioncable/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,19 @@ def cable_broadcast(group_name: str, message: Union[str, dict]):
"message": message,
},
)


async def async_cable_broadcast(group_name: str, message: Union[str, dict]):
"""
Rails: ActionCable.server.broadcast
"""
channel_layer = get_channel_layer()
await channel_layer.group_send(
group_name,
{
"type": "action_cable_message",
# should put group_name in the message, so the ActionCableConsumer can know which group this message sent to
"group": group_name,
"message": message,
},
)
25 changes: 3 additions & 22 deletions tests/test_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from channels.testing import WebsocketCommunicator
from channels.layers import get_channel_layer
from actioncable import cable_channel_register, ActionCableConsumer, CableChannel, compact_encode_json
from actioncable.utils import async_cable_broadcast


@cable_channel_register
Expand Down Expand Up @@ -45,17 +46,7 @@ async def test_subscribe():
assert response['type'] == 'confirm_subscription'

# Message
channel_layer = get_channel_layer()
await channel_layer.group_send(
group_name,
{
"type": "message",
"group": group_name,
"data": {
"message": 'html_snippet',
},
},
)
await async_cable_broadcast(group_name, 'html_snippet')

response = await communicator.receive_json_from(timeout=5)
assert response['message'] == 'html_snippet'
Expand All @@ -72,17 +63,7 @@ async def test_subscribe():
await communicator.send_to(text_data=compact_encode_json(subscribe_command))

# Message
channel_layer = get_channel_layer()
await channel_layer.group_send(
group_name,
{
"type": "message",
"group": group_name,
"data": {
"message": 'html_snippet',
},
},
)
await async_cable_broadcast(group_name, 'html_snippet')

assert await communicator.receive_nothing() is True

Expand Down

0 comments on commit de8a100

Please sign in to comment.