Add MQTT publish ACK timeout test (#67062)

This commit is contained in:
Jan Bouwhuis 2022-02-23 12:30:13 +01:00 committed by GitHub
parent 845bf80e72
commit 8dbb184ed5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1242,6 +1242,48 @@ async def test_setup_mqtt_client_protocol(hass):
assert mock_client.call_args[1]["protocol"] == 3
@patch("homeassistant.components.mqtt.TIMEOUT_ACK", 0.2)
async def test_handle_mqtt_timeout_on_callback(hass, caplog):
"""Test publish without receiving an ACK callback."""
mid = 0
class FakeInfo:
"""Returns a simulated client publish response."""
mid = 100
rc = 0
with patch("paho.mqtt.client.Client") as mock_client:
def _mock_ack(topic, qos=0):
# Handle ACK for subscribe normally
nonlocal mid
mid += 1
mock_client.on_subscribe(0, 0, mid)
return (0, mid)
# We want to simulate the publish behaviour MQTT client
mock_client = mock_client.return_value
mock_client.publish.return_value = FakeInfo()
mock_client.subscribe.side_effect = _mock_ack
mock_client.connect.return_value = 0
entry = MockConfigEntry(
domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"}
)
# Set up the integration
assert await mqtt.async_setup_entry(hass, entry)
# Make sure we are connected correctly
mock_client.on_connect(mock_client, None, None, 0)
# Now call we publish without simulating and ACK callback
await mqtt.async_publish(hass, "no_callback/test-topic", "test-payload")
await hass.async_block_till_done()
# The is no ACK so we should see a timeout in the log after publishing
assert len(mock_client.publish.mock_calls) == 1
assert "No ACK from MQTT server" in caplog.text
async def test_setup_raises_ConfigEntryNotReady_if_no_connect_broker(hass, caplog):
"""Test for setup failure if connection to broker is missing."""
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"})