|
| 1 | +import asyncio |
| 2 | +import random |
| 3 | + |
| 4 | +import async_timeout |
| 5 | +import pytest |
| 6 | +from async_generator import async_generator, yield_ |
| 7 | + |
| 8 | +from channels_redis.pubsub import RedisPubSubChannelLayer |
| 9 | + |
| 10 | +SENTINEL_MASTER = "sentinel" |
| 11 | +TEST_HOSTS = [{"sentinels": [("localhost", 26379)], "master_name": SENTINEL_MASTER}] |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture() |
| 15 | +@async_generator |
| 16 | +async def channel_layer(): |
| 17 | + """ |
| 18 | + Channel layer fixture that flushes automatically. |
| 19 | + """ |
| 20 | + channel_layer = RedisPubSubChannelLayer(hosts=TEST_HOSTS) |
| 21 | + await yield_(channel_layer) |
| 22 | + await channel_layer.flush() |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.asyncio |
| 26 | +async def test_send_receive(channel_layer): |
| 27 | + """ |
| 28 | + Makes sure we can send a message to a normal channel then receive it. |
| 29 | + """ |
| 30 | + channel = await channel_layer.new_channel() |
| 31 | + await channel_layer.send(channel, {"type": "test.message", "text": "Ahoy-hoy!"}) |
| 32 | + message = await channel_layer.receive(channel) |
| 33 | + assert message["type"] == "test.message" |
| 34 | + assert message["text"] == "Ahoy-hoy!" |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.asyncio |
| 38 | +async def test_multi_send_receive(channel_layer): |
| 39 | + """ |
| 40 | + Tests overlapping sends and receives, and ordering. |
| 41 | + """ |
| 42 | + channel = await channel_layer.new_channel() |
| 43 | + await channel_layer.send(channel, {"type": "message.1"}) |
| 44 | + await channel_layer.send(channel, {"type": "message.2"}) |
| 45 | + await channel_layer.send(channel, {"type": "message.3"}) |
| 46 | + assert (await channel_layer.receive(channel))["type"] == "message.1" |
| 47 | + assert (await channel_layer.receive(channel))["type"] == "message.2" |
| 48 | + assert (await channel_layer.receive(channel))["type"] == "message.3" |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.asyncio |
| 52 | +async def test_groups_basic(channel_layer): |
| 53 | + """ |
| 54 | + Tests basic group operation. |
| 55 | + """ |
| 56 | + channel_name1 = await channel_layer.new_channel(prefix="test-gr-chan-1") |
| 57 | + channel_name2 = await channel_layer.new_channel(prefix="test-gr-chan-2") |
| 58 | + channel_name3 = await channel_layer.new_channel(prefix="test-gr-chan-3") |
| 59 | + await channel_layer.group_add("test-group", channel_name1) |
| 60 | + await channel_layer.group_add("test-group", channel_name2) |
| 61 | + await channel_layer.group_add("test-group", channel_name3) |
| 62 | + await channel_layer.group_discard("test-group", channel_name2) |
| 63 | + await channel_layer.group_send("test-group", {"type": "message.1"}) |
| 64 | + # Make sure we get the message on the two channels that were in |
| 65 | + async with async_timeout.timeout(1): |
| 66 | + assert (await channel_layer.receive(channel_name1))["type"] == "message.1" |
| 67 | + assert (await channel_layer.receive(channel_name3))["type"] == "message.1" |
| 68 | + # Make sure the removed channel did not get the message |
| 69 | + with pytest.raises(asyncio.TimeoutError): |
| 70 | + async with async_timeout.timeout(1): |
| 71 | + await channel_layer.receive(channel_name2) |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.asyncio |
| 75 | +async def test_groups_same_prefix(channel_layer): |
| 76 | + """ |
| 77 | + Tests group_send with multiple channels with same channel prefix |
| 78 | + """ |
| 79 | + channel_name1 = await channel_layer.new_channel(prefix="test-gr-chan") |
| 80 | + channel_name2 = await channel_layer.new_channel(prefix="test-gr-chan") |
| 81 | + channel_name3 = await channel_layer.new_channel(prefix="test-gr-chan") |
| 82 | + await channel_layer.group_add("test-group", channel_name1) |
| 83 | + await channel_layer.group_add("test-group", channel_name2) |
| 84 | + await channel_layer.group_add("test-group", channel_name3) |
| 85 | + await channel_layer.group_send("test-group", {"type": "message.1"}) |
| 86 | + |
| 87 | + # Make sure we get the message on the channels that were in |
| 88 | + async with async_timeout.timeout(1): |
| 89 | + assert (await channel_layer.receive(channel_name1))["type"] == "message.1" |
| 90 | + assert (await channel_layer.receive(channel_name2))["type"] == "message.1" |
| 91 | + assert (await channel_layer.receive(channel_name3))["type"] == "message.1" |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.asyncio |
| 95 | +async def test_random_reset__channel_name(channel_layer): |
| 96 | + """ |
| 97 | + Makes sure resetting random seed does not make us reuse channel names. |
| 98 | + """ |
| 99 | + random.seed(1) |
| 100 | + channel_name_1 = await channel_layer.new_channel() |
| 101 | + random.seed(1) |
| 102 | + channel_name_2 = await channel_layer.new_channel() |
| 103 | + |
| 104 | + assert channel_name_1 != channel_name_2 |
0 commit comments