Fix race in influxdb test (#115514)

The patch was still too late in #115442

There is no good candidate to patch here since the late operation
is the error log that is being tested.

Patching the logger did not seem like a good idea so I went with
patching to wait for the error to be emitted since emit is the public
API of the log handler and was less likely to change
This commit is contained in:
J. Nick Koston 2024-04-12 21:13:01 -10:00 committed by GitHub
parent 76fefaafb0
commit bb9330135d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,9 +1,9 @@
"""The tests for the InfluxDB component.""" """The tests for the InfluxDB component."""
import asyncio
from dataclasses import dataclass from dataclasses import dataclass
import datetime import datetime
from http import HTTPStatus from http import HTTPStatus
import logging
from unittest.mock import ANY, MagicMock, Mock, call, patch from unittest.mock import ANY, MagicMock, Mock, call, patch
import pytest import pytest
@ -1573,21 +1573,25 @@ async def test_invalid_inputs_error(
await _setup(hass, mock_client, config_ext, get_write_api) await _setup(hass, mock_client, config_ext, get_write_api)
write_api = get_write_api(mock_client) write_api = get_write_api(mock_client)
write_api.side_effect = test_exception
write_api_done_event = asyncio.Event() log_emit_done = hass.loop.create_future()
def wait_for_write(*args, **kwargs): original_emit = caplog.handler.emit
hass.loop.call_soon_threadsafe(write_api_done_event.set)
raise test_exception
write_api.side_effect = wait_for_write def wait_for_emit(record: logging.LogRecord) -> None:
original_emit(record)
if record.levelname == "ERROR":
hass.loop.call_soon_threadsafe(log_emit_done.set_result, None)
with patch(f"{INFLUX_PATH}.time.sleep") as sleep: with (
write_api_done_event.clear() patch(f"{INFLUX_PATH}.time.sleep") as sleep,
patch.object(caplog.handler, "emit", wait_for_emit),
):
hass.states.async_set("fake.something", 1) hass.states.async_set("fake.something", 1)
await hass.async_block_till_done() await hass.async_block_till_done()
await async_wait_for_queue_to_process(hass) await async_wait_for_queue_to_process(hass)
await write_api_done_event.wait() await log_emit_done
await hass.async_block_till_done() await hass.async_block_till_done()
write_api.assert_called_once() write_api.assert_called_once()