-
Notifications
You must be signed in to change notification settings - Fork 194
/
test_pubsub_sentinel.py
204 lines (169 loc) · 7.75 KB
/
test_pubsub_sentinel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import asyncio
import random
import async_timeout
import pytest
from asgiref.sync import async_to_sync
from channels_redis.pubsub import RedisPubSubChannelLayer
SENTINEL_MASTER = "sentinel"
SENTINEL_KWARGS = {"password": "channels_redis"}
TEST_HOSTS = [
{
"sentinels": [("localhost", 26379)],
"master_name": SENTINEL_MASTER,
"sentinel_kwargs": SENTINEL_KWARGS,
}
]
@pytest.fixture()
async def channel_layer():
"""
Channel layer fixture that flushes automatically.
"""
channel_layer = RedisPubSubChannelLayer(hosts=TEST_HOSTS)
yield channel_layer
async with async_timeout.timeout(1):
await channel_layer.flush()
@pytest.mark.asyncio
async def test_send_receive(channel_layer):
"""
Makes sure we can send a message to a normal channel then receive it.
"""
channel = await channel_layer.new_channel()
await channel_layer.send(channel, {"type": "test.message", "text": "Ahoy-hoy!"})
message = await channel_layer.receive(channel)
assert message["type"] == "test.message"
assert message["text"] == "Ahoy-hoy!"
def test_send_receive_sync(channel_layer, event_loop):
_await = event_loop.run_until_complete
channel = _await(channel_layer.new_channel())
async_to_sync(channel_layer.send, force_new_loop=True)(
channel, {"type": "test.message", "text": "Ahoy-hoy!"}
)
message = _await(channel_layer.receive(channel))
assert message["type"] == "test.message"
assert message["text"] == "Ahoy-hoy!"
@pytest.mark.asyncio
async def test_multi_send_receive(channel_layer):
"""
Tests overlapping sends and receives, and ordering.
"""
channel = await channel_layer.new_channel()
await channel_layer.send(channel, {"type": "message.1"})
await channel_layer.send(channel, {"type": "message.2"})
await channel_layer.send(channel, {"type": "message.3"})
assert (await channel_layer.receive(channel))["type"] == "message.1"
assert (await channel_layer.receive(channel))["type"] == "message.2"
assert (await channel_layer.receive(channel))["type"] == "message.3"
def test_multi_send_receive_sync(channel_layer, event_loop):
_await = event_loop.run_until_complete
channel = _await(channel_layer.new_channel())
send = async_to_sync(channel_layer.send)
send(channel, {"type": "message.1"})
send(channel, {"type": "message.2"})
send(channel, {"type": "message.3"})
assert _await(channel_layer.receive(channel))["type"] == "message.1"
assert _await(channel_layer.receive(channel))["type"] == "message.2"
assert _await(channel_layer.receive(channel))["type"] == "message.3"
@pytest.mark.asyncio
async def test_groups_basic(channel_layer):
"""
Tests basic group operation.
"""
channel_name1 = await channel_layer.new_channel(prefix="test-gr-chan-1")
channel_name2 = await channel_layer.new_channel(prefix="test-gr-chan-2")
channel_name3 = await channel_layer.new_channel(prefix="test-gr-chan-3")
await channel_layer.group_add("test-group", channel_name1)
await channel_layer.group_add("test-group", channel_name2)
await channel_layer.group_add("test-group", channel_name3)
await channel_layer.group_discard("test-group", channel_name2)
await channel_layer.group_send("test-group", {"type": "message.1"})
# Make sure we get the message on the two channels that were in
async with async_timeout.timeout(1):
assert (await channel_layer.receive(channel_name1))["type"] == "message.1"
assert (await channel_layer.receive(channel_name3))["type"] == "message.1"
# Make sure the removed channel did not get the message
with pytest.raises(asyncio.TimeoutError):
async with async_timeout.timeout(1):
await channel_layer.receive(channel_name2)
@pytest.mark.asyncio
async def test_groups_same_prefix(channel_layer):
"""
Tests group_send with multiple channels with same channel prefix
"""
channel_name1 = await channel_layer.new_channel(prefix="test-gr-chan")
channel_name2 = await channel_layer.new_channel(prefix="test-gr-chan")
channel_name3 = await channel_layer.new_channel(prefix="test-gr-chan")
await channel_layer.group_add("test-group", channel_name1)
await channel_layer.group_add("test-group", channel_name2)
await channel_layer.group_add("test-group", channel_name3)
await channel_layer.group_send("test-group", {"type": "message.1"})
# Make sure we get the message on the channels that were in
async with async_timeout.timeout(1):
assert (await channel_layer.receive(channel_name1))["type"] == "message.1"
assert (await channel_layer.receive(channel_name2))["type"] == "message.1"
assert (await channel_layer.receive(channel_name3))["type"] == "message.1"
@pytest.mark.asyncio
async def test_random_reset__channel_name(channel_layer):
"""
Makes sure resetting random seed does not make us reuse channel names.
"""
random.seed(1)
channel_name_1 = await channel_layer.new_channel()
random.seed(1)
channel_name_2 = await channel_layer.new_channel()
assert channel_name_1 != channel_name_2
@pytest.mark.asyncio
async def test_loop_instance_channel_layer_reference(channel_layer):
redis_pub_sub_loop_layer = channel_layer._get_layer()
assert redis_pub_sub_loop_layer.channel_layer == channel_layer
def test_serialize(channel_layer):
"""
Test default serialization method
"""
message = {"a": True, "b": None, "c": {"d": []}}
serialized = channel_layer.serialize(message)
assert isinstance(serialized, bytes)
assert serialized == b"\x83\xa1a\xc3\xa1b\xc0\xa1c\x81\xa1d\x90"
def test_deserialize(channel_layer):
"""
Test default deserialization method
"""
message = b"\x83\xa1a\xc3\xa1b\xc0\xa1c\x81\xa1d\x90"
deserialized = channel_layer.deserialize(message)
assert isinstance(deserialized, dict)
assert deserialized == {"a": True, "b": None, "c": {"d": []}}
def test_multi_event_loop_garbage_collection(channel_layer):
"""
Test loop closure layer flushing and garbage collection
"""
assert len(channel_layer._layers.values()) == 0
async_to_sync(test_send_receive)(channel_layer)
assert len(channel_layer._layers.values()) == 0
@pytest.mark.asyncio
async def test_receive_hang(channel_layer):
channel_name = await channel_layer.new_channel(prefix="test-channel")
with pytest.raises(asyncio.TimeoutError):
await asyncio.wait_for(channel_layer.receive(channel_name), timeout=1)
@pytest.mark.asyncio
async def test_auto_reconnect(channel_layer):
"""
Tests redis-py reconnect and resubscribe
"""
channel_name1 = await channel_layer.new_channel(prefix="test-gr-chan-1")
channel_name2 = await channel_layer.new_channel(prefix="test-gr-chan-2")
channel_name3 = await channel_layer.new_channel(prefix="test-gr-chan-3")
await channel_layer.group_add("test-group", channel_name1)
await channel_layer.group_add("test-group", channel_name2)
await channel_layer._shards[0]._redis.close(close_connection_pool=True)
await channel_layer.group_add("test-group", channel_name3)
await channel_layer.group_discard("test-group", channel_name2)
await channel_layer._shards[0]._redis.close(close_connection_pool=True)
await asyncio.sleep(1)
await channel_layer.group_send("test-group", {"type": "message.1"})
# Make sure we get the message on the two channels that were in
async with async_timeout.timeout(5):
assert (await channel_layer.receive(channel_name1))["type"] == "message.1"
assert (await channel_layer.receive(channel_name3))["type"] == "message.1"
# Make sure the removed channel did not get the message
with pytest.raises(asyncio.TimeoutError):
async with async_timeout.timeout(1):
await channel_layer.receive(channel_name2)