Fix flakey bootstrap test (#118285)

This commit is contained in:
J. Nick Koston 2024-05-27 20:11:14 -10:00 committed by GitHub
parent 5d61743a5b
commit 3ba3e3135e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,6 +4,7 @@ import asyncio
from collections.abc import Generator, Iterable from collections.abc import Generator, Iterable
import contextlib import contextlib
import glob import glob
import logging
import os import os
import sys import sys
from typing import Any from typing import Any
@ -1101,14 +1102,14 @@ async def test_tasks_logged_that_block_stage_2(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None: ) -> None:
"""Test we log tasks that delay stage 2 startup.""" """Test we log tasks that delay stage 2 startup."""
done_future = hass.loop.create_future()
def gen_domain_setup(domain): def gen_domain_setup(domain):
async def async_setup(hass, config): async def async_setup(hass, config):
async def _not_marked_background_task(): async def _not_marked_background_task():
await asyncio.sleep(0.2) await done_future
hass.async_create_task(_not_marked_background_task()) hass.async_create_task(_not_marked_background_task())
await asyncio.sleep(0.1)
return True return True
return async_setup return async_setup
@ -1122,16 +1123,36 @@ async def test_tasks_logged_that_block_stage_2(
), ),
) )
wanted_messages = {
"Setup timed out for stage 2 waiting on",
"waiting on",
"_not_marked_background_task",
}
def on_message_logged(log_record: logging.LogRecord, *args):
for message in list(wanted_messages):
if message in log_record.message:
wanted_messages.remove(message)
if not done_future.done() and not wanted_messages:
done_future.set_result(None)
return
with ( with (
patch.object(bootstrap, "STAGE_2_TIMEOUT", 0), patch.object(bootstrap, "STAGE_2_TIMEOUT", 0),
patch.object(bootstrap, "COOLDOWN_TIME", 0), patch.object(bootstrap, "COOLDOWN_TIME", 0),
patch.object(
caplog.handler,
"emit",
wraps=caplog.handler.emit,
side_effect=on_message_logged,
),
): ):
await bootstrap._async_set_up_integrations(hass, {"normal_integration": {}}) await bootstrap._async_set_up_integrations(hass, {"normal_integration": {}})
async with asyncio.timeout(2):
await done_future
await hass.async_block_till_done() await hass.async_block_till_done()
assert "Setup timed out for stage 2 waiting on" in caplog.text assert not wanted_messages
assert "waiting on" in caplog.text
assert "_not_marked_background_task" in caplog.text
@pytest.mark.parametrize("load_registries", [False]) @pytest.mark.parametrize("load_registries", [False])