Coverage for tests/brokers/mqtt/test_consume.py: 99%
68 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 unittest.mock import MagicMock
4import pytest
5import zmqtt
6from dirty_equals import IsPartialDataclass
8from faststream.mqtt import MQTTBroker
9from tests.brokers.base.consume import BrokerRealConsumeTestcase
11from .basic import MQTTTestcaseConfig
14class _FailingSubscription:
15 async def start(self) -> None:
16 pass
18 async def stop(self) -> None:
19 pass
21 def __aiter__(self) -> "_FailingSubscription":
22 return self
24 async def __anext__(self) -> zmqtt.Message:
25 msg = "Connection recovery failed"
26 raise zmqtt.MQTTDisconnectedError(msg)
29@pytest.mark.mqtt()
30@pytest.mark.asyncio()
31async def test_terminal_subscription_failure_is_not_restarted(
32 monkeypatch: pytest.MonkeyPatch,
33) -> None:
34 monkeypatch.setenv("FASTSTREAM_SUPERVISOR_DISABLED", "0")
35 broker = MQTTBroker()
36 subscriber = broker.subscriber("test")
38 @subscriber
39 async def handle() -> None:
40 pass
42 broker._setup_logger()
44 async def create_subscription() -> None:
45 subscriber._subscription = _FailingSubscription() # type: ignore[assignment]
47 monkeypatch.setattr(subscriber, "_create_subscription", create_subscription)
49 await subscriber.start()
50 await asyncio.gather(*subscriber.tasks, return_exceptions=True)
51 await asyncio.sleep(0)
53 assert len(subscriber.tasks) == 1
54 await subscriber.stop()
57@pytest.mark.connected()
58@pytest.mark.mqtt()
59@pytest.mark.asyncio()
60class TestConsume(MQTTTestcaseConfig, BrokerRealConsumeTestcase):
61 async def test_unsubscribe_result(self, queue: str) -> None:
62 broker = self.get_broker()
63 subscriber = broker.subscriber(queue)
65 async with self.patch_broker(broker) as br:
66 await br.start()
67 assert await subscriber.get_one(timeout=0.01) is None
68 assert subscriber.last_unsubscribe_result is None
70 await subscriber.stop()
71 result = subscriber.last_unsubscribe_result
72 assert result == IsPartialDataclass(
73 topic_filters=(queue,),
74 reason_codes=(0,) if self.version == "5.0" else (),
75 )
77 await subscriber.stop()
78 assert subscriber.last_unsubscribe_result is result
80 assert subscriber.last_unsubscribe_result is result
82 async def test_consume_with_filter(
83 self,
84 queue: str,
85 mock: MagicMock,
86 event: asyncio.Event,
87 event2: asyncio.Event,
88 ) -> None:
89 if self.version == "3.1.1":
90 pytest.skip("content_type filtering not supported in MQTT 3.1.1")
91 await super().test_consume_with_filter(queue, mock, event, event2)
93 @pytest.mark.asyncio()
94 async def test_iteration(
95 self,
96 queue: str,
97 mock: MagicMock,
98 ) -> None:
99 # Overridden since order is not guaranteed in MQTT.
100 expected_messages = {"test_message_1", "test_message_2"}
102 broker = self.get_broker(apply_types=True)
104 args, kwargs = self.get_subscriber_params(queue)
105 subscriber = broker.subscriber(*args, **kwargs)
107 async with self.patch_broker(broker) as br:
108 await br.start()
110 async def publish_test_message() -> None:
111 for msg in expected_messages:
112 await br.publish(msg, queue)
114 async def consume() -> None:
115 index_message = 0
116 async for msg in subscriber: 116 ↛ exitline 116 didn't return from function 'consume' because the loop on line 116 didn't complete
117 result_message = await msg.decode()
119 mock(result_message)
121 index_message += 1
122 if index_message >= len(expected_messages):
123 break
125 await asyncio.wait(
126 (
127 asyncio.create_task(consume()),
128 asyncio.create_task(publish_test_message()),
129 ),
130 timeout=self.timeout,
131 )
133 msgs = {call.args[0] for call in mock.mock_calls}
134 assert msgs == expected_messages