Coverage for tests/brokers/confluent/test_test_client.py: 99%
208 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 AsyncMock, MagicMock, patch
5import pytest
7from faststream import AckPolicy, BaseMiddleware, Context
8from faststream.confluent.annotations import KafkaMessage
9from faststream.confluent.message import FAKE_CONSUMER
10from faststream.confluent.testing import FakeProducer
11from faststream.exceptions import SetupError
12from tests.brokers.base.testclient import BrokerTestclientTestcase
13from tests.marks import require_aiopika
14from tests.tools import spy_decorator
16from .basic import ConfluentMemoryTestcaseConfig
19@pytest.mark.confluent()
20@pytest.mark.asyncio()
21class TestTestclient(ConfluentMemoryTestcaseConfig, BrokerTestclientTestcase):
22 async def test_publish_none_tombstone(
23 self,
24 queue: str,
25 mock: MagicMock,
26 ) -> None:
27 broker = self.get_broker(apply_types=True)
29 @broker.subscriber(queue)
30 async def handler(msg: Any = Context("message")) -> None:
31 mock(msg.raw_message.value())
33 async with self.patch_broker(broker) as br:
34 await br.publish(None, queue, key=b"tombstone-key")
36 mock.assert_called_once_with(None)
38 async def test_message_nack_seek(self, queue: str) -> None:
39 broker = self.get_broker(apply_types=True)
41 with pytest.warns(UserWarning, match="REJECT_ON_ERROR has the same effect"):
43 @broker.subscriber(
44 queue,
45 group_id=f"{queue}-consume",
46 auto_offset_reset="earliest",
47 ack_policy=AckPolicy.REJECT_ON_ERROR,
48 )
49 async def m(msg: KafkaMessage) -> None:
50 await msg.nack()
52 async with self.patch_broker(broker) as br:
53 with patch.object(
54 FAKE_CONSUMER,
55 "seek",
56 spy_decorator(FAKE_CONSUMER.seek),
57 ) as mocked:
58 await br.publish("hello", queue)
59 m.mock.assert_called_once_with("hello")
60 mocked.mock.assert_called_once()
62 @pytest.mark.connected()
63 async def test_with_real_testclient(self, queue: str, event: asyncio.Event) -> None:
64 broker = self.get_broker()
66 args, kwargs = self.get_subscriber_params(queue)
68 @broker.subscriber(*args, **kwargs) # type: ignore[untyped-decorator]
69 def subscriber(m: Any) -> None:
70 event.set()
72 async with self.patch_broker(broker, with_real=True) as br:
73 await asyncio.wait(
74 (
75 asyncio.create_task(br.publish("hello", queue)),
76 asyncio.create_task(event.wait()),
77 ),
78 timeout=10,
79 )
81 assert event.is_set()
83 async def test_publisher_autoflush_mock(self, queue: str) -> None:
84 broker = self.get_broker()
86 publisher = broker.publisher(queue + "1", autoflush=True)
87 publisher.flush = AsyncMock() # type: ignore[method-assign]
89 @publisher
90 @broker.subscriber(queue)
91 async def m(msg: Any) -> None:
92 pass
94 async with self.patch_broker(broker) as br:
95 await br.publish("hello", queue)
97 m.mock.assert_called_once_with("hello")
98 publisher.mock.assert_called_once()
100 publisher.flush.assert_awaited_once()
102 async def test_batch_publisher_autoflush_mock(self, queue: str) -> None:
103 broker = self.get_broker()
105 publisher = broker.publisher(queue + "1", batch=True, autoflush=True)
106 publisher.flush = AsyncMock() # type: ignore[method-assign]
108 @publisher
109 @broker.subscriber(queue)
110 async def m(msg: Any) -> Any:
111 return 1, 2, 3
113 async with self.patch_broker(broker) as br:
114 await br.publish("hello", queue)
116 m.mock.assert_called_once_with("hello")
117 publisher.mock.assert_called_once_with([1, 2, 3])
119 publisher.flush.assert_awaited_once()
121 async def test_batch_pub_by_default_pub(
122 self,
123 queue: str,
124 ) -> None:
125 broker = self.get_broker()
127 @broker.subscriber(queue, batch=True)
128 async def m(msg: Any) -> None:
129 pass
131 async with self.patch_broker(broker) as br:
132 await br.publish("hello", queue)
133 m.mock.assert_called_once_with(["hello"])
135 async def test_batch_pub_by_pub_batch(
136 self,
137 queue: str,
138 ) -> None:
139 broker = self.get_broker()
141 @broker.subscriber(queue, batch=True)
142 async def m(msg: Any) -> None:
143 pass
145 async with self.patch_broker(broker) as br:
146 await br.publish_batch("hello", topic=queue)
147 m.mock.assert_called_once_with(["hello"])
149 async def test_batch_assert_called_once_with(self, queue: str) -> None:
150 broker = self.get_broker()
152 @broker.subscriber(queue, batch=True)
153 async def m(msg: Any) -> None: ...
155 async with self.patch_broker(broker) as br:
156 await br.publish_batch({"n": 1}, {"n": 2}, topic=queue)
158 await m.assert_called_once_with([{"n": 1}, {"n": 2}])
160 # A batch has one header set per message, so there is no single answer
161 with pytest.raises(SetupError, match="received a batch"):
162 await m.assert_called_once_with(headers={"key": "value"})
164 with pytest.raises(SetupError, match=r"received a batch.*key, partition"):
165 await m.assert_called_once_with(key=b"k", partition=0)
167 async def test_assertions_take_the_kafka_fields(self, queue: str) -> None:
168 broker = self.get_broker()
170 @broker.subscriber(queue)
171 async def handle(msg: str) -> None: ...
173 async with self.patch_broker(broker) as br:
174 await br.publish("hello", queue, key=b"k", partition=1)
176 await handle.assert_called_once_with("hello", key=b"k", partition=1)
177 await handle.assert_called_with(key=b"k")
178 await handle.assert_any_call(partition=1)
180 with pytest.raises(AssertionError, match="key: expected b'other', got b'k'"):
181 await handle.assert_called_once_with("hello", key=b"other")
183 async def test_publisher_assertions_take_the_kafka_fields(self, queue: str) -> None:
184 broker = self.get_broker()
186 publisher = broker.publisher(queue)
188 async with self.patch_broker(broker):
189 await publisher.publish("response", key=b"k", partition=1)
191 await publisher.assert_called_once_with("response", key=b"k", partition=1)
192 await publisher.assert_called_with(key=b"k")
193 await publisher.assert_any_call(partition=1)
195 @require_aiopika
196 async def test_kafka_fields_refuse_another_brokers_message(self, queue: str) -> None:
197 from faststream.rabbit import RabbitBroker, TestRabbitBroker # ruff: ignore[import-outside-top-level]
199 broker = self.get_broker()
200 rabbit = RabbitBroker()
202 @broker.subscriber(queue)
203 async def handle(msg: str) -> None: ...
205 # The broker that wraps a handler first decides its wrapper class: Kafka's here
206 _ = rabbit.subscriber(queue)(handle)
208 async with self.patch_broker(broker), TestRabbitBroker(rabbit):
209 await rabbit.publish("hello", queue)
211 await handle.assert_called_once_with("hello")
213 with pytest.raises(SetupError, match="`key` is a Kafka field"):
214 await handle.assert_called_once_with("hello", key=b"k")
216 async def test_batch_publisher_mock(self, queue: str) -> None:
217 broker = self.get_broker()
219 publisher = broker.publisher(queue + "1", batch=True)
221 @publisher
222 @broker.subscriber(queue)
223 async def m(msg: Any) -> Any:
224 return 1, 2, 3
226 async with self.patch_broker(broker) as br:
227 await br.publish("hello", queue)
228 m.mock.assert_called_once_with("hello")
229 publisher.mock.assert_called_once_with([1, 2, 3])
231 @pytest.mark.parametrize(
232 "returned",
233 (pytest.param(None, id="None"), pytest.param([], id="Empty Sequence")),
234 )
235 async def test_batch_publisher_empty_result_matches_default_publisher(
236 self,
237 queue: str,
238 returned: Any,
239 ) -> None:
240 """Fixes https://github.com/ag2ai/faststream/issues/3056.
242 An empty result publishes one empty message, exactly as a non-batch
243 publisher does.
244 """
245 broker = self.get_broker()
247 batch_publisher = broker.publisher(queue + "1", batch=True)
248 default_publisher = broker.publisher(queue + "2")
250 @batch_publisher
251 @broker.subscriber(queue)
252 async def batched(msg: Any) -> Any:
253 return returned
255 @default_publisher
256 @broker.subscriber(queue + "3")
257 async def single(msg: Any) -> None:
258 return None
260 async with self.patch_broker(broker) as br:
261 await br.publish("hello", queue)
262 await br.publish("hello", queue + "3")
264 default_publisher.mock.assert_called_once_with(b"")
265 batch_publisher.mock.assert_called_once_with([b""])
267 async def test_respect_middleware(self, queue: str) -> None:
268 routes: list[Any] = []
270 class Middleware(BaseMiddleware):
271 async def on_receive(self) -> None:
272 routes.append(None)
273 return await super().on_receive()
275 broker = self.get_broker(middlewares=(Middleware,))
277 @broker.subscriber(queue)
278 async def h1(msg: Any) -> None: ...
280 @broker.subscriber(queue + "1")
281 async def h2(msg: Any) -> None: ...
283 async with self.patch_broker(broker) as br:
284 await br.publish("", queue)
285 await br.publish("", queue + "1")
287 assert len(routes) == 2
289 @pytest.mark.connected()
290 async def test_real_respect_middleware(self, queue: str) -> None:
291 routes: list[Any] = []
293 class Middleware(BaseMiddleware):
294 async def on_receive(self) -> None:
295 routes.append(None)
296 return await super().on_receive()
298 broker = self.get_broker(middlewares=(Middleware,))
300 args, kwargs = self.get_subscriber_params(queue)
302 @broker.subscriber(*args, **kwargs) # type: ignore[untyped-decorator]
303 async def h1(msg: Any) -> None: ...
305 args2, kwargs2 = self.get_subscriber_params(queue + "1")
307 @broker.subscriber(*args2, **kwargs2) # type: ignore[untyped-decorator]
308 async def h2(msg: Any) -> None: ...
310 async with self.patch_broker(broker, with_real=True) as br:
311 await br.publish("", queue)
312 await br.publish("", queue + "1")
313 await h1.wait_call(10)
314 await h2.wait_call(10)
316 assert len(routes) == 2
318 async def test_multiple_subscribers_different_groups(self, queue: str) -> None:
319 broker = self.get_broker()
321 @broker.subscriber(queue, group_id="group1")
322 async def subscriber1(msg: Any) -> None: ...
324 @broker.subscriber(queue, group_id="group2")
325 async def subscriber2(msg: Any) -> None: ...
327 async with self.patch_broker(broker) as br:
328 await br.start()
329 await br.publish("", queue)
331 assert subscriber1.mock.call_count == 1
332 assert subscriber2.mock.call_count == 1
334 async def test_multiple_subscribers_same_group(self, queue: str) -> None:
335 broker = self.get_broker()
337 @broker.subscriber(queue, group_id="group1")
338 async def subscriber1(msg: Any) -> None: ...
340 @broker.subscriber(queue, group_id="group1")
341 async def subscriber2(msg: Any) -> None: ...
343 async with self.patch_broker(broker) as br:
344 await br.start()
345 await br.publish("", queue)
347 # we can't guarantee the order of calls
348 assert {subscriber1.mock.call_count, subscriber2.mock.call_count} == {1, 0}
350 async def test_multiple_batch_subscriber_with_different_group(
351 self, queue: str
352 ) -> None:
353 broker = self.get_broker()
355 @broker.subscriber(queue, batch=True, group_id="group1")
356 async def subscriber1(msg: Any) -> None: ...
358 @broker.subscriber(queue, batch=True, group_id="group2")
359 async def subscriber2(msg: Any) -> None: ...
361 async with self.patch_broker(broker) as br:
362 await br.start()
363 await br.publish("", queue)
365 assert subscriber1.mock.call_count == 1
366 assert subscriber2.mock.call_count == 1
368 async def test_multiple_batch_subscriber_with_same_group(self, queue: str) -> None:
369 broker = self.get_broker()
371 @broker.subscriber(queue, batch=True, group_id="group1")
372 async def subscriber1(msg: Any) -> None: ...
374 @broker.subscriber(queue, batch=True, group_id="group1")
375 async def subscriber2(msg: Any) -> None: ...
377 async with self.patch_broker(broker) as br:
378 await br.start()
379 await br.publish("", queue)
381 # we can't guarantee the order of calls
382 assert {subscriber1.mock.call_count, subscriber2.mock.call_count} == {1, 0}
384 @pytest.mark.connected()
385 async def test_broker_gets_patched_attrs_within_cm(self) -> None:
386 await super().test_broker_gets_patched_attrs_within_cm(FakeProducer)
388 @pytest.mark.connected()
389 async def test_broker_with_real_doesnt_get_patched(self) -> None:
390 await super().test_broker_with_real_doesnt_get_patched()
392 @pytest.mark.connected()
393 async def test_broker_with_real_patches_publishers_and_subscribers(
394 self, queue: str
395 ) -> None:
396 await super().test_broker_with_real_patches_publishers_and_subscribers(queue)
398 @pytest.mark.xfail(reason="https://github.com/ag2ai/faststream/issues/2513")
399 async def test_publisher_without_destination(self) -> None:
400 """Fixes https://github.com/ag2ai/faststream/issues/2513."""
401 broker = self.get_broker()
403 # use two publishers to check that we don't have conflicts
404 publisher = broker.publisher(topic="")
405 another_publisher = broker.publisher(topic="")
407 async with self.patch_broker(broker):
408 await publisher.publish(None, topic="new-key")
409 publisher.mock.assert_called_once()
411 await another_publisher.publish(None, topic="new-key")
412 another_publisher.mock.assert_called_once()