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
pull/115518/head
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
1 changed files with 13 additions and 9 deletions

View File

@ -1,9 +1,9 @@
"""The tests for the InfluxDB component."""
import asyncio
from dataclasses import dataclass
import datetime
from http import HTTPStatus
import logging
from unittest.mock import ANY, MagicMock, Mock, call, patch
import pytest
@ -1573,21 +1573,25 @@ async def test_invalid_inputs_error(
await _setup(hass, mock_client, config_ext, get_write_api)
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):
hass.loop.call_soon_threadsafe(write_api_done_event.set)
raise test_exception
original_emit = caplog.handler.emit
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:
write_api_done_event.clear()
with (
patch(f"{INFLUX_PATH}.time.sleep") as sleep,
patch.object(caplog.handler, "emit", wait_for_emit),
):
hass.states.async_set("fake.something", 1)
await hass.async_block_till_done()
await async_wait_for_queue_to_process(hass)
await write_api_done_event.wait()
await log_emit_done
await hass.async_block_till_done()
write_api.assert_called_once()