Coverage for tests/brokers/redis/cluster/test_cluster_pubsub_more.py: 99%
115 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
1import asyncio
2from typing import Any
3from unittest.mock import MagicMock
5import pytest
7from faststream import Context
8from faststream.exceptions import StopConsume
9from tests.brokers.redis.basic import RedisClusterTestcaseConfig
11from .settings import SettingsCluster
14@pytest.mark.slow()
15@pytest.mark.connected()
16@pytest.mark.redis_cluster()
17class TestClusterPubSubMore(RedisClusterTestcaseConfig):
18 """Additional Pub/Sub tests for RedisClusterBroker."""
20 timeout: float = 10.0
22 @pytest.mark.asyncio()
23 async def test_pattern_subscribe(
24 self,
25 queue: str,
26 settings_cluster: SettingsCluster,
27 event: asyncio.Event,
28 ) -> None:
29 broker = self.get_broker(startup_nodes=settings_cluster.startup_nodes)
31 received = []
33 @broker.subscriber(channel=f"test.pattern.{queue}.*")
34 async def handler(msg: str) -> None:
35 received.append(msg)
36 if len(received) == 2:
37 event.set()
39 async with broker:
40 await broker.start()
41 await broker.publish("a", channel=f"test.pattern.{queue}.foo")
42 await broker.publish("b", channel=f"test.pattern.{queue}.bar")
43 await asyncio.wait(
44 (asyncio.create_task(event.wait()),),
45 timeout=self.timeout,
46 )
48 # The two channels hash to their own slots, so the publishes reach different
49 # nodes and propagate over the cluster bus independently. Which one arrives
50 # first is a race, and not one this test is about — sorted rather than a set,
51 # so a message delivered twice still fails.
52 assert sorted(received) == ["a", "b"]
54 @pytest.mark.asyncio()
55 async def test_multiple_subscribers_same_channel(
56 self,
57 queue: str,
58 settings_cluster: SettingsCluster,
59 event: asyncio.Event,
60 event2: asyncio.Event,
61 ) -> None:
62 broker = self.get_broker(startup_nodes=settings_cluster.startup_nodes)
64 received1 = []
65 received2 = []
67 @broker.subscriber(channel=f"multi-channel-{queue}")
68 async def handler1(msg: str) -> None:
69 received1.append(msg)
70 event.set()
72 @broker.subscriber(channel=f"multi-channel-{queue}")
73 async def handler2(msg: str) -> None:
74 received2.append(msg)
75 event2.set()
77 async with broker:
78 await broker.start()
79 await broker.publish("shared", channel=f"multi-channel-{queue}")
80 await asyncio.wait(
81 (
82 asyncio.create_task(event.wait()),
83 asyncio.create_task(event2.wait()),
84 ),
85 timeout=self.timeout,
86 )
88 assert received1 == ["shared"]
89 assert received2 == ["shared"]
91 @pytest.mark.asyncio()
92 async def test_get_one_from_channel(
93 self,
94 queue: str,
95 settings_cluster: SettingsCluster,
96 mock: MagicMock,
97 ) -> None:
98 broker = self.get_broker(startup_nodes=settings_cluster.startup_nodes)
100 sub = broker.subscriber(channel=f"get-one-channel-{queue}")
102 async with broker:
103 await broker.start()
105 async def consume() -> None:
106 msg = await sub.get_one(timeout=self.timeout)
107 if msg is not None: 107 ↛ exitline 107 didn't return from function 'consume' because the condition on line 107 was always true
108 mock(await msg.decode())
110 async def publish() -> None:
111 await asyncio.sleep(0.1)
112 await broker.publish("test_msg", channel=f"get-one-channel-{queue}")
114 await asyncio.wait(
115 (
116 asyncio.create_task(consume()),
117 asyncio.create_task(publish()),
118 ),
119 timeout=self.timeout,
120 )
122 mock.assert_called_once_with("test_msg")
124 @pytest.mark.asyncio()
125 async def test_get_one_timeout_channel(
126 self,
127 queue: str,
128 settings_cluster: SettingsCluster,
129 mock: MagicMock,
130 ) -> None:
131 broker = self.get_broker(startup_nodes=settings_cluster.startup_nodes)
133 sub = broker.subscriber(channel=f"empty-channel-{queue}")
135 async with broker:
136 await broker.start()
137 result = await sub.get_one(timeout=0.5)
138 mock(result)
140 mock.assert_called_once_with(None)
142 @pytest.mark.asyncio()
143 async def test_headers_propagation(
144 self,
145 queue: str,
146 settings_cluster: SettingsCluster,
147 mock: MagicMock,
148 event: asyncio.Event,
149 ) -> None:
150 broker = self.get_broker(
151 apply_types=True,
152 startup_nodes=settings_cluster.startup_nodes,
153 )
155 @broker.subscriber(channel=f"headers-channel-{queue}")
156 async def handler(
157 body: str,
158 msg: Any = Context("message"),
159 ) -> None:
160 mock(
161 body=body,
162 correlation_id=msg.correlation_id,
163 reply_to=msg.reply_to,
164 )
165 event.set()
167 async with broker:
168 await broker.start()
169 await broker.publish(
170 "data",
171 channel=f"headers-channel-{queue}",
172 correlation_id="my-cor-id",
173 reply_to="reply-chan",
174 )
175 await asyncio.wait(
176 (asyncio.create_task(event.wait()),),
177 timeout=self.timeout,
178 )
180 assert event.is_set()
181 assert mock.call_args[1]["body"] == "data"
182 assert mock.call_args[1]["correlation_id"] == "my-cor-id"
183 assert mock.call_args[1]["reply_to"] == "reply-chan"
185 @pytest.mark.asyncio()
186 async def test_stop_consume_channel(
187 self,
188 queue: str,
189 settings_cluster: SettingsCluster,
190 mock: MagicMock,
191 event: asyncio.Event,
192 ) -> None:
193 broker = self.get_broker(startup_nodes=settings_cluster.startup_nodes)
195 @broker.subscriber(channel=f"stop-channel-{queue}")
196 async def handler(msg: str) -> None:
197 mock(msg)
198 event.set()
199 raise StopConsume
201 async with broker:
202 await broker.start()
203 await broker.publish("hello", channel=f"stop-channel-{queue}")
204 await asyncio.wait(
205 (asyncio.create_task(event.wait()),),
206 timeout=self.timeout,
207 )
208 await asyncio.sleep(0.5)
209 await broker.publish("hello2", channel=f"stop-channel-{queue}")
210 await asyncio.sleep(0.5)
212 assert event.is_set()
213 mock.assert_called_once_with("hello")
215 @pytest.mark.asyncio()
216 async def test_unsubscribe_cleans_up(
217 self,
218 queue: str,
219 settings_cluster: SettingsCluster,
220 event: asyncio.Event,
221 ) -> None:
222 broker = self.get_broker(startup_nodes=settings_cluster.startup_nodes)
224 sub = broker.subscriber(channel=f"restart-channel-{queue}")
226 @sub
227 async def handler(msg: str) -> None:
228 event.set()
230 async with broker:
231 await broker.start()
233 # First round
234 await sub.start()
235 await broker.publish("msg1", channel=f"restart-channel-{queue}")
236 await asyncio.wait(
237 (asyncio.create_task(event.wait()),),
238 timeout=self.timeout,
239 )
240 assert event.is_set()
242 await sub.stop()
244 # Second round
245 event.clear()
246 await sub.start()
247 await broker.publish("msg2", channel=f"restart-channel-{queue}")
248 await asyncio.wait(
249 (asyncio.create_task(event.wait()),),
250 timeout=self.timeout,
251 )
252 assert event.is_set()
254 await sub.stop()