Remove duplicate mid handling in MQTT (#116531)

This commit is contained in:
J. Nick Koston
2024-05-01 15:18:44 -05:00
committed by GitHub
parent e68901235b
commit 041456759f
2 changed files with 33 additions and 15 deletions

View File

@ -2074,16 +2074,34 @@ async def test_handle_mqtt_on_callback(
) -> None:
"""Test receiving an ACK callback before waiting for it."""
await mqtt_mock_entry()
# Simulate an ACK for mid == 1, this will call mqtt_mock._mqtt_handle_mid(mid)
mqtt_client_mock.on_publish(mqtt_client_mock, None, 1)
await hass.async_block_till_done()
# Make sure the ACK has been received
await hass.async_block_till_done()
# Now call publish without call back, this will call _wait_for_mid(msg_info.mid)
await mqtt.async_publish(hass, "no_callback/test-topic", "test-payload")
# Since the mid event was already set, we should not see any timeout warning in the log
with patch.object(mqtt_client_mock, "get_mid", return_value=100):
# Simulate an ACK for mid == 100, this will call mqtt_mock._async_get_mid_future(mid)
mqtt_client_mock.on_publish(mqtt_client_mock, None, 100)
await hass.async_block_till_done()
# Make sure the ACK has been received
await hass.async_block_till_done()
# Now call publish without call back, this will call _async_async_wait_for_mid(msg_info.mid)
await mqtt.async_publish(hass, "no_callback/test-topic", "test-payload")
# Since the mid event was already set, we should not see any timeout warning in the log
await hass.async_block_till_done()
assert "No ACK from MQTT server" not in caplog.text
async def test_handle_mqtt_on_callback_after_timeout(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
mqtt_mock_entry: MqttMockHAClientGenerator,
mqtt_client_mock: MqttMockPahoClient,
) -> None:
"""Test receiving an ACK after a timeout."""
mqtt_mock = await mqtt_mock_entry()
# Simulate the mid future getting a timeout
mqtt_mock()._async_get_mid_future(100).set_exception(asyncio.TimeoutError)
# Simulate an ACK for mid == 100, being received after the timeout
mqtt_client_mock.on_publish(mqtt_client_mock, None, 100)
await hass.async_block_till_done()
assert "No ACK from MQTT server" not in caplog.text
assert "InvalidStateError" not in caplog.text
async def test_publish_error(