Coverage for tests/brokers/kafka/test_test_client.py: 99%
231 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.exceptions import SetupError
9from faststream.kafka import TopicPartition
10from faststream.kafka.annotations import KafkaMessage
11from faststream.kafka.message import FAKE_CONSUMER
12from faststream.kafka.testing import FakeProducer
13from tests.brokers.base.testclient import BrokerTestclientTestcase
14from tests.marks import require_aiopika
15from tests.tools import spy_decorator
17from .basic import KafkaMemoryTestcaseConfig
20@pytest.mark.kafka()
21@pytest.mark.asyncio()
22class TestTestclient(KafkaMemoryTestcaseConfig, BrokerTestclientTestcase):
23 async def test_publish_none_tombstone(
24 self,
25 queue: str,
26 mock: MagicMock,
27 ) -> None:
28 broker = self.get_broker(apply_types=True)
30 @broker.subscriber(queue)
31 async def handler(msg: Any = Context("message")) -> None:
32 mock(msg.raw_message.value)
34 async with self.patch_broker(broker) as br:
35 await br.publish(None, queue, key=b"tombstone-key")
37 mock.assert_called_once_with(None)
39 async def test_partition_match(
40 self,
41 queue: str,
42 ) -> None:
43 broker = self.get_broker()
45 @broker.subscriber(partitions=[TopicPartition(queue, 1)])
46 async def m(msg: Any) -> None:
47 pass
49 async with self.patch_broker(broker) as br:
50 await br.publish("hello", queue)
52 m.mock.assert_called_once_with("hello")
54 async def test_partition_match_exact(
55 self,
56 queue: str,
57 ) -> None:
58 broker = self.get_broker()
60 @broker.subscriber(partitions=[TopicPartition(queue, 1)])
61 async def m(msg: Any) -> None:
62 pass
64 async with self.patch_broker(broker) as br:
65 await br.publish("hello", queue, partition=1)
67 m.mock.assert_called_once_with("hello")
69 async def test_partition_mismatch(
70 self,
71 queue: str,
72 ) -> None:
73 broker = self.get_broker()
75 @broker.subscriber(partitions=[TopicPartition(queue, 1)])
76 async def m(msg: Any) -> None:
77 pass
79 @broker.subscriber(queue)
80 async def m2(msg: Any) -> None:
81 pass
83 async with self.patch_broker(broker) as br:
84 await br.publish("hello", queue, partition=2)
86 assert not m.mock.called
87 m2.mock.assert_called_once_with("hello")
89 async def test_message_nack_seek(
90 self,
91 queue: str,
92 ) -> None:
93 broker = self.get_broker(apply_types=True)
95 @broker.subscriber(queue, group_id=f"{queue}1", ack_policy=AckPolicy.MANUAL)
96 async def m(msg: KafkaMessage) -> None:
97 await msg.nack()
99 async with self.patch_broker(broker) as br:
100 with patch.object(
101 FAKE_CONSUMER,
102 "seek",
103 spy_decorator(FAKE_CONSUMER.seek),
104 ) as mocked:
105 await br.publish("hello", queue)
106 mocked.mock.assert_called_once()
108 async def test_publisher_autoflush_mock(
109 self,
110 queue: str,
111 ) -> None:
112 broker = self.get_broker()
114 publisher = broker.publisher(queue + "1", autoflush=True)
115 publisher.flush = AsyncMock() # type: ignore[method-assign]
117 @publisher
118 @broker.subscriber(queue)
119 async def m(msg: Any) -> Any:
120 return 1
122 async with self.patch_broker(broker) as br:
123 await br.publish("hello", queue)
125 m.mock.assert_called_once_with("hello")
126 publisher.mock.assert_called_once_with(1)
128 publisher.flush.assert_awaited_once()
130 async def test_batch_publisher_autoflush_mock(
131 self,
132 queue: str,
133 ) -> None:
134 broker = self.get_broker()
136 publisher = broker.publisher(queue + "1", batch=True, autoflush=True)
137 publisher.flush = AsyncMock() # type: ignore[method-assign]
139 @publisher
140 @broker.subscriber(queue)
141 async def m(msg: Any) -> Any:
142 return 1, 2, 3
144 async with self.patch_broker(broker) as br:
145 await br.publish("hello", queue)
147 m.mock.assert_called_once_with("hello")
148 publisher.mock.assert_called_once_with([1, 2, 3])
150 publisher.flush.assert_awaited_once()
152 @pytest.mark.connected()
153 async def test_with_real_testclient(self, queue: str, event: asyncio.Event) -> None:
154 broker = self.get_broker()
156 args, kwargs = self.get_subscriber_params(queue)
158 @broker.subscriber(*args, **kwargs) # type: ignore[untyped-decorator]
159 def subscriber(m: Any) -> None:
160 event.set()
162 async with self.patch_broker(broker, with_real=True) as br:
163 await asyncio.wait(
164 (
165 asyncio.create_task(br.publish("hello", queue)),
166 asyncio.create_task(event.wait()),
167 ),
168 timeout=3,
169 )
171 assert event.is_set()
173 async def test_batch_pub_by_default_pub(
174 self,
175 queue: str,
176 ) -> None:
177 broker = self.get_broker()
179 @broker.subscriber(queue, batch=True)
180 async def m(msg: Any) -> None:
181 pass
183 async with self.patch_broker(broker) as br:
184 await br.publish("hello", queue)
185 m.mock.assert_called_once_with(["hello"])
187 async def test_batch_pub_by_pub_batch(
188 self,
189 queue: str,
190 ) -> None:
191 broker = self.get_broker()
193 @broker.subscriber(queue, batch=True)
194 async def m(msg: Any) -> None:
195 pass
197 async with self.patch_broker(broker) as br:
198 await br.publish_batch("hello", topic=queue)
199 m.mock.assert_called_once_with(["hello"])
201 async def test_batch_assert_called_once_with(self, queue: str) -> None:
202 broker = self.get_broker()
204 @broker.subscriber(queue, batch=True)
205 async def m(msg: Any) -> None: ...
207 async with self.patch_broker(broker) as br:
208 await br.publish_batch({"n": 1}, {"n": 2}, topic=queue)
210 await m.assert_called_once_with([{"n": 1}, {"n": 2}])
212 # A batch has one header set per message, so there is no single answer
213 with pytest.raises(SetupError, match="received a batch"):
214 await m.assert_called_once_with(headers={"key": "value"})
216 with pytest.raises(SetupError, match=r"received a batch.*key, partition"):
217 await m.assert_called_once_with(key=b"k", partition=0)
219 async def test_assertions_take_the_kafka_fields(self, queue: str) -> None:
220 broker = self.get_broker()
222 @broker.subscriber(queue)
223 async def handle(msg: str) -> None: ...
225 async with self.patch_broker(broker) as br:
226 await br.publish("hello", queue, key=b"k", partition=1)
228 await handle.assert_called_once_with("hello", key=b"k", partition=1)
229 await handle.assert_called_with(key=b"k")
230 await handle.assert_any_call(partition=1)
232 with pytest.raises(AssertionError, match="key: expected b'other', got b'k'"):
233 await handle.assert_called_once_with("hello", key=b"other")
235 async def test_publisher_assertions_take_the_kafka_fields(self, queue: str) -> None:
236 broker = self.get_broker()
238 publisher = broker.publisher(queue)
240 async with self.patch_broker(broker):
241 await publisher.publish("response", key=b"k", partition=1)
243 await publisher.assert_called_once_with("response", key=b"k", partition=1)
244 await publisher.assert_called_with(key=b"k")
245 await publisher.assert_any_call(partition=1)
247 @require_aiopika
248 async def test_kafka_fields_refuse_another_brokers_message(self, queue: str) -> None:
249 from faststream.rabbit import RabbitBroker, TestRabbitBroker # ruff: ignore[import-outside-top-level]
251 broker = self.get_broker()
252 rabbit = RabbitBroker()
254 @broker.subscriber(queue)
255 async def handle(msg: str) -> None: ...
257 # The broker that wraps a handler first decides its wrapper class: Kafka's here
258 _ = rabbit.subscriber(queue)(handle)
260 async with self.patch_broker(broker), TestRabbitBroker(rabbit):
261 await rabbit.publish("hello", queue)
263 await handle.assert_called_once_with("hello")
265 with pytest.raises(SetupError, match="`key` is a Kafka field"):
266 await handle.assert_called_once_with("hello", key=b"k")
268 async def test_batch_publisher_mock(
269 self,
270 queue: str,
271 ) -> None:
272 broker = self.get_broker()
274 publisher = broker.publisher(queue + "1", batch=True)
276 @publisher
277 @broker.subscriber(queue)
278 async def m(msg: Any) -> Any:
279 return 1, 2, 3
281 async with self.patch_broker(broker) as br:
282 await br.publish("hello", queue)
283 m.mock.assert_called_once_with("hello")
284 publisher.mock.assert_called_once_with([1, 2, 3])
286 @pytest.mark.parametrize(
287 "returned",
288 (pytest.param(None, id="None"), pytest.param([], id="Empty Sequence")),
289 )
290 async def test_batch_publisher_empty_result_matches_default_publisher(
291 self,
292 queue: str,
293 returned: Any,
294 ) -> None:
295 """Fixes https://github.com/ag2ai/faststream/issues/3056.
297 An empty result publishes one empty message, exactly as a non-batch
298 publisher does.
299 """
300 broker = self.get_broker()
302 batch_publisher = broker.publisher(queue + "1", batch=True)
303 default_publisher = broker.publisher(queue + "2")
305 @batch_publisher
306 @broker.subscriber(queue)
307 async def batched(msg: Any) -> Any:
308 return returned
310 @default_publisher
311 @broker.subscriber(queue + "3")
312 async def single(msg: Any) -> None:
313 return None
315 async with self.patch_broker(broker) as br:
316 await br.publish("hello", queue)
317 await br.publish("hello", queue + "3")
319 default_publisher.mock.assert_called_once_with(b"")
320 batch_publisher.mock.assert_called_once_with([b""])
322 async def test_respect_middleware(self, queue: str) -> None:
323 routes: list[Any] = []
325 class Middleware(BaseMiddleware):
326 async def on_receive(self) -> None:
327 routes.append(None)
328 return await super().on_receive()
330 broker = self.get_broker(middlewares=(Middleware,))
332 @broker.subscriber(queue)
333 async def h1(msg: Any) -> None: ...
335 @broker.subscriber(queue + "1")
336 async def h2(msg: Any) -> None: ...
338 async with self.patch_broker(broker) as br:
339 await br.publish("", queue)
340 await br.publish("", queue + "1")
342 assert len(routes) == 2
344 @pytest.mark.connected()
345 async def test_real_respect_middleware(self, queue: str) -> None:
346 routes: list[Any] = []
348 class Middleware(BaseMiddleware):
349 async def on_receive(self) -> None:
350 routes.append(None)
351 return await super().on_receive()
353 broker = self.get_broker(middlewares=(Middleware,))
355 args, kwargs = self.get_subscriber_params(queue)
357 @broker.subscriber(*args, **kwargs) # type: ignore[untyped-decorator]
358 async def h1(msg: Any) -> None: ...
360 args2, kwargs2 = self.get_subscriber_params(queue + "1")
362 @broker.subscriber(*args2, **kwargs2) # type: ignore[untyped-decorator]
363 async def h2(msg: Any) -> None: ...
365 async with self.patch_broker(broker, with_real=True) as br:
366 await br.publish("", queue)
367 await br.publish("", queue + "1")
368 await h1.wait_call(3)
369 await h2.wait_call(3)
371 assert len(routes) == 2
373 async def test_multiple_subscribers_different_groups(
374 self,
375 queue: str,
376 ) -> None:
377 test_broker = self.get_broker()
379 @test_broker.subscriber(queue, group_id="group1")
380 async def subscriber1(msg: Any) -> None: ...
382 @test_broker.subscriber(queue, group_id="group2")
383 async def subscriber2(msg: Any) -> None: ...
385 async with self.patch_broker(test_broker) as br:
386 await br.start()
387 await br.publish("", queue)
389 assert subscriber1.mock.call_count == 1
390 assert subscriber2.mock.call_count == 1
392 async def test_multiple_subscribers_same_group(self, queue: str) -> None:
393 broker = self.get_broker()
395 @broker.subscriber(queue, group_id="group1")
396 async def subscriber1(msg: Any) -> None: ...
398 @broker.subscriber(queue, group_id="group1")
399 async def subscriber2(msg: Any) -> None: ...
401 async with self.patch_broker(broker) as br:
402 await br.start()
403 await br.publish("", queue)
405 # we can't guarantee the order of calls
406 assert {subscriber1.mock.call_count, subscriber2.mock.call_count} == {1, 0}
408 async def test_multiple_batch_subscriber_with_different_group(
409 self,
410 queue: str,
411 ) -> None:
412 broker = self.get_broker()
414 @broker.subscriber(queue, batch=True, group_id="group1")
415 async def subscriber1(msg: Any) -> None: ...
417 @broker.subscriber(queue, batch=True, group_id="group2")
418 async def subscriber2(msg: Any) -> None: ...
420 async with self.patch_broker(broker) as br:
421 await br.start()
422 await br.publish("", queue)
424 assert subscriber1.mock.call_count == 1
425 assert subscriber2.mock.call_count == 1
427 async def test_multiple_batch_subscriber_with_same_group(
428 self,
429 queue: str,
430 ) -> None:
431 broker = self.get_broker()
433 @broker.subscriber(queue, batch=True, group_id="group1")
434 async def subscriber1(msg: Any) -> None: ...
436 @broker.subscriber(queue, batch=True, group_id="group1")
437 async def subscriber2(msg: Any) -> None: ...
439 async with self.patch_broker(broker) as br:
440 await br.start()
441 await br.publish("", queue)
443 # we can't guarantee the order of calls
444 assert {subscriber1.mock.call_count, subscriber2.mock.call_count} == {1, 0}
446 @pytest.mark.connected()
447 async def test_broker_gets_patched_attrs_within_cm(self) -> None:
448 await super().test_broker_gets_patched_attrs_within_cm(FakeProducer)
450 @pytest.mark.connected()
451 async def test_broker_with_real_doesnt_get_patched(self) -> None:
452 await super().test_broker_with_real_doesnt_get_patched()
454 @pytest.mark.connected()
455 async def test_broker_with_real_patches_publishers_and_subscribers(
456 self,
457 queue: str,
458 ) -> None:
459 await super().test_broker_with_real_patches_publishers_and_subscribers(queue)
461 @pytest.mark.xfail(reason="https://github.com/ag2ai/faststream/issues/2513")
462 async def test_publisher_without_destination(self) -> None:
463 """Fixes https://github.com/ag2ai/faststream/issues/2513."""
464 broker = self.get_broker()
466 # use two publishers to check that we don't have conflicts
467 publisher = broker.publisher(topic="")
468 another_publisher = broker.publisher(topic="")
470 async with self.patch_broker(broker):
471 await publisher.publish(None, topic="new-key")
472 publisher.mock.assert_called_once()
474 await another_publisher.publish(None, topic="new-key")
475 another_publisher.mock.assert_called_once()