From 15c0422837d52783533fa6afe569b85ef3538e69 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 19 Mar 2024 09:17:12 -1000 Subject: [PATCH] Fix flakey profiler object growth tests (#113825) --- tests/components/profiler/test_init.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/tests/components/profiler/test_init.py b/tests/components/profiler/test_init.py index 2574f71a57b..8847e779b9e 100644 --- a/tests/components/profiler/test_init.py +++ b/tests/components/profiler/test_init.py @@ -9,6 +9,7 @@ from unittest.mock import patch from freezegun.api import FrozenDateTimeFactory from lru import LRU +import objgraph import pytest from homeassistant.components.profiler import ( @@ -115,30 +116,31 @@ async def test_object_growth_logging( assert hass.services.has_service(DOMAIN, SERVICE_START_LOG_OBJECTS) assert hass.services.has_service(DOMAIN, SERVICE_STOP_LOG_OBJECTS) - with patch("objgraph.growth"): + with patch.object(objgraph, "growth"): await hass.services.async_call( - DOMAIN, SERVICE_START_LOG_OBJECTS, {CONF_SCAN_INTERVAL: 10}, blocking=True + DOMAIN, SERVICE_START_LOG_OBJECTS, {CONF_SCAN_INTERVAL: 1}, blocking=True ) with pytest.raises(HomeAssistantError, match="Object logging already started"): await hass.services.async_call( DOMAIN, SERVICE_START_LOG_OBJECTS, - {CONF_SCAN_INTERVAL: 10}, + {CONF_SCAN_INTERVAL: 1}, blocking=True, ) assert "Growth" in caplog.text + await hass.async_block_till_done(wait_background_tasks=True) caplog.clear() - async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=11)) - await hass.async_block_till_done() + async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=2)) + await hass.async_block_till_done(wait_background_tasks=True) assert "Growth" in caplog.text await hass.services.async_call(DOMAIN, SERVICE_STOP_LOG_OBJECTS, {}, blocking=True) caplog.clear() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=21)) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) assert "Growth" not in caplog.text with pytest.raises(HomeAssistantError, match="Object logging not running"): @@ -146,17 +148,18 @@ async def test_object_growth_logging( DOMAIN, SERVICE_STOP_LOG_OBJECTS, {}, blocking=True ) - with patch("objgraph.growth"): + with patch.object(objgraph, "growth"): await hass.services.async_call( DOMAIN, SERVICE_START_LOG_OBJECTS, {CONF_SCAN_INTERVAL: 10}, blocking=True ) + await hass.async_block_till_done(wait_background_tasks=True) caplog.clear() assert await hass.config_entries.async_unload(entry.entry_id) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=31)) - await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) assert "Growth" not in caplog.text