Skip to content

Commit bb93301

Browse files
authored
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
1 parent 76fefaa commit bb93301

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

tests/components/influxdb/test_init.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""The tests for the InfluxDB component."""
22

3-
import asyncio
43
from dataclasses import dataclass
54
import datetime
65
from http import HTTPStatus
6+
import logging
77
from unittest.mock import ANY, MagicMock, Mock, call, patch
88

99
import pytest
@@ -1573,21 +1573,25 @@ async def test_invalid_inputs_error(
15731573
await _setup(hass, mock_client, config_ext, get_write_api)
15741574

15751575
write_api = get_write_api(mock_client)
1576+
write_api.side_effect = test_exception
15761577

1577-
write_api_done_event = asyncio.Event()
1578+
log_emit_done = hass.loop.create_future()
15781579

1579-
def wait_for_write(*args, **kwargs):
1580-
hass.loop.call_soon_threadsafe(write_api_done_event.set)
1581-
raise test_exception
1580+
original_emit = caplog.handler.emit
15821581

1583-
write_api.side_effect = wait_for_write
1582+
def wait_for_emit(record: logging.LogRecord) -> None:
1583+
original_emit(record)
1584+
if record.levelname == "ERROR":
1585+
hass.loop.call_soon_threadsafe(log_emit_done.set_result, None)
15841586

1585-
with patch(f"{INFLUX_PATH}.time.sleep") as sleep:
1586-
write_api_done_event.clear()
1587+
with (
1588+
patch(f"{INFLUX_PATH}.time.sleep") as sleep,
1589+
patch.object(caplog.handler, "emit", wait_for_emit),
1590+
):
15871591
hass.states.async_set("fake.something", 1)
15881592
await hass.async_block_till_done()
15891593
await async_wait_for_queue_to_process(hass)
1590-
await write_api_done_event.wait()
1594+
await log_emit_done
15911595
await hass.async_block_till_done()
15921596

15931597
write_api.assert_called_once()

0 commit comments

Comments
 (0)