mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Improve lingering timer checks (#99472)
This commit is contained in:
parent
1048f47a91
commit
1ab2e900f9
@ -294,6 +294,10 @@ class HomeAssistant:
|
|||||||
_hass.hass = hass
|
_hass.hass = hass
|
||||||
return hass
|
return hass
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
"""Return the representation."""
|
||||||
|
return f"<HomeAssistant {self.state}>"
|
||||||
|
|
||||||
def __init__(self, config_dir: str) -> None:
|
def __init__(self, config_dir: str) -> None:
|
||||||
"""Initialize new Home Assistant object."""
|
"""Initialize new Home Assistant object."""
|
||||||
self.loop = asyncio.get_running_loop()
|
self.loop = asyncio.get_running_loop()
|
||||||
|
@ -3,12 +3,13 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from collections.abc import AsyncGenerator, Callable, Coroutine, Generator
|
from collections.abc import AsyncGenerator, Callable, Coroutine, Generator
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager, contextmanager
|
||||||
import functools
|
import functools
|
||||||
import gc
|
import gc
|
||||||
import itertools
|
import itertools
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import reprlib
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import ssl
|
import ssl
|
||||||
import threading
|
import threading
|
||||||
@ -302,6 +303,21 @@ def skip_stop_scripts(
|
|||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def long_repr_strings() -> Generator[None, None, None]:
|
||||||
|
"""Increase reprlib maxstring and maxother to 300."""
|
||||||
|
arepr = reprlib.aRepr
|
||||||
|
original_maxstring = arepr.maxstring
|
||||||
|
original_maxother = arepr.maxother
|
||||||
|
arepr.maxstring = 300
|
||||||
|
arepr.maxother = 300
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
arepr.maxstring = original_maxstring
|
||||||
|
arepr.maxother = original_maxother
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def verify_cleanup(
|
def verify_cleanup(
|
||||||
event_loop: asyncio.AbstractEventLoop,
|
event_loop: asyncio.AbstractEventLoop,
|
||||||
@ -335,13 +351,16 @@ def verify_cleanup(
|
|||||||
|
|
||||||
for handle in event_loop._scheduled: # type: ignore[attr-defined]
|
for handle in event_loop._scheduled: # type: ignore[attr-defined]
|
||||||
if not handle.cancelled():
|
if not handle.cancelled():
|
||||||
if expected_lingering_timers:
|
with long_repr_strings():
|
||||||
_LOGGER.warning("Lingering timer after test %r", handle)
|
if expected_lingering_timers:
|
||||||
elif handle._args and isinstance(job := handle._args[0], HassJob):
|
_LOGGER.warning("Lingering timer after test %r", handle)
|
||||||
pytest.fail(f"Lingering timer after job {repr(job)}")
|
elif handle._args and isinstance(job := handle._args[-1], HassJob):
|
||||||
else:
|
if job.cancel_on_shutdown:
|
||||||
pytest.fail(f"Lingering timer after test {repr(handle)}")
|
continue
|
||||||
handle.cancel()
|
pytest.fail(f"Lingering timer after job {repr(job)}")
|
||||||
|
else:
|
||||||
|
pytest.fail(f"Lingering timer after test {repr(handle)}")
|
||||||
|
handle.cancel()
|
||||||
|
|
||||||
# Verify no threads where left behind.
|
# Verify no threads where left behind.
|
||||||
threads = frozenset(threading.enumerate()) - threads_before
|
threads = frozenset(threading.enumerate()) - threads_before
|
||||||
|
Loading…
x
Reference in New Issue
Block a user