Skip to content

Commit 6e9130d

Browse files
committed
Make sure all executors and task are closed once hass closes
1 parent 181ffc9 commit 6e9130d

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

tests/conftest.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import threading
1313
from typing import Any
1414
from unittest.mock import AsyncMock, MagicMock, Mock, patch
15+
import warnings
1516

1617
from aiohttp import client
1718
from aiohttp.pytest_plugin import AiohttpClient
@@ -187,12 +188,14 @@ async def guard_func(*args, **kwargs):
187188

188189

189190
@pytest.fixture(autouse=True)
190-
def verify_cleanup():
191+
def verify_cleanup(event_loop: asyncio.AbstractEventLoop):
191192
"""Verify that the test has cleaned up resources correctly."""
192193
threads_before = frozenset(threading.enumerate())
193-
194+
tasks_before = asyncio.all_tasks(event_loop)
194195
yield
195196

197+
event_loop.run_until_complete(event_loop.shutdown_default_executor())
198+
196199
if len(INSTANCES) >= 2:
197200
count = len(INSTANCES)
198201
for inst in INSTANCES:
@@ -203,6 +206,13 @@ def verify_cleanup():
203206
for thread in threads:
204207
assert isinstance(thread, threading._DummyThread)
205208

209+
tasks = asyncio.all_tasks(event_loop) - tasks_before
210+
for task in tasks:
211+
warnings.warn(f"Linger task after test {task}")
212+
task.cancel()
213+
if tasks:
214+
event_loop.run_until_complete(asyncio.wait(tasks))
215+
206216

207217
@pytest.fixture(autouse=True)
208218
def bcrypt_cost():
@@ -381,7 +391,7 @@ def exc_handle(loop, context):
381391

382392

383393
@pytest.fixture
384-
async def stop_hass():
394+
async def stop_hass(event_loop):
385395
"""Make sure all hass are stopped."""
386396
orig_hass = ha.HomeAssistant
387397

@@ -402,6 +412,7 @@ def mock_hass():
402412
with patch.object(hass_inst.loop, "stop"):
403413
await hass_inst.async_block_till_done()
404414
await hass_inst.async_stop(force=True)
415+
await event_loop.shutdown_default_executor()
405416

406417

407418
@pytest.fixture

0 commit comments

Comments
 (0)