From aa65f21be7b32e279767ebaf2b2d00ffd6a32b69 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 26 Apr 2024 16:05:23 +0200 Subject: [PATCH] Fix flapping recorder tests (#116239) --- homeassistant/core.py | 5 +++-- tests/components/recorder/test_init.py | 10 +++------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/homeassistant/core.py b/homeassistant/core.py index a3150adc221..604840e542d 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -972,10 +972,11 @@ class HomeAssistant: target = cast(Callable[[*_Ts], Coroutine[Any, Any, _R] | _R], target) return self.async_run_hass_job(HassJob(target), *args) - def block_till_done(self) -> None: + def block_till_done(self, wait_background_tasks: bool = False) -> None: """Block until all pending work is done.""" asyncio.run_coroutine_threadsafe( - self.async_block_till_done(), self.loop + self.async_block_till_done(wait_background_tasks=wait_background_tasks), + self.loop, ).result() async def async_block_till_done(self, wait_background_tasks: bool = False) -> None: diff --git a/tests/components/recorder/test_init.py b/tests/components/recorder/test_init.py index e3fec10f86b..f0609f82229 100644 --- a/tests/components/recorder/test_init.py +++ b/tests/components/recorder/test_init.py @@ -981,11 +981,12 @@ async def test_defaults_set(hass: HomeAssistant) -> None: assert recorder_config["purge_keep_days"] == 10 -def run_tasks_at_time(hass, test_time): +def run_tasks_at_time(hass: HomeAssistant, test_time: datetime) -> None: """Advance the clock and wait for any callbacks to finish.""" fire_time_changed(hass, test_time) - hass.block_till_done() + hass.block_till_done(wait_background_tasks=True) get_instance(hass).block_till_done() + hass.block_till_done(wait_background_tasks=True) @pytest.mark.parametrize("enable_nightly_purge", [True]) @@ -1225,7 +1226,6 @@ def test_auto_statistics(hass_recorder: Callable[..., HomeAssistant], freezer) - test_time = datetime(now.year + 2, 1, 1, 4, 51, 0, tzinfo=tz) freezer.move_to(test_time.isoformat()) run_tasks_at_time(hass, test_time) - hass.block_till_done() hass.bus.listen( EVENT_RECORDER_5MIN_STATISTICS_GENERATED, async_5min_stats_updated_listener @@ -1245,7 +1245,6 @@ def test_auto_statistics(hass_recorder: Callable[..., HomeAssistant], freezer) - freezer.move_to(test_time.isoformat()) run_tasks_at_time(hass, test_time) assert len(compile_statistics.mock_calls) == 1 - hass.block_till_done() assert len(stats_5min) == 1 assert len(stats_hourly) == 0 @@ -1256,7 +1255,6 @@ def test_auto_statistics(hass_recorder: Callable[..., HomeAssistant], freezer) - freezer.move_to(test_time.isoformat()) run_tasks_at_time(hass, test_time) assert len(compile_statistics.mock_calls) == 1 - hass.block_till_done() assert len(stats_5min) == 2 assert len(stats_hourly) == 1 @@ -1267,7 +1265,6 @@ def test_auto_statistics(hass_recorder: Callable[..., HomeAssistant], freezer) - freezer.move_to(test_time.isoformat()) run_tasks_at_time(hass, test_time) assert len(compile_statistics.mock_calls) == 0 - hass.block_till_done() assert len(stats_5min) == 2 assert len(stats_hourly) == 1 @@ -1276,7 +1273,6 @@ def test_auto_statistics(hass_recorder: Callable[..., HomeAssistant], freezer) - freezer.move_to(test_time.isoformat()) run_tasks_at_time(hass, test_time) assert len(compile_statistics.mock_calls) == 1 - hass.block_till_done() assert len(stats_5min) == 3 assert len(stats_hourly) == 1