12
12
import threading
13
13
from typing import Any
14
14
from unittest .mock import AsyncMock , MagicMock , Mock , patch
15
+ import warnings
15
16
16
17
from aiohttp import client
17
18
from aiohttp .pytest_plugin import AiohttpClient
@@ -187,12 +188,14 @@ async def guard_func(*args, **kwargs):
187
188
188
189
189
190
@pytest .fixture (autouse = True )
190
- def verify_cleanup ():
191
+ def verify_cleanup (event_loop : asyncio . AbstractEventLoop ):
191
192
"""Verify that the test has cleaned up resources correctly."""
192
193
threads_before = frozenset (threading .enumerate ())
193
-
194
+ tasks_before = asyncio . all_tasks ( event_loop )
194
195
yield
195
196
197
+ event_loop .run_until_complete (event_loop .shutdown_default_executor ())
198
+
196
199
if len (INSTANCES ) >= 2 :
197
200
count = len (INSTANCES )
198
201
for inst in INSTANCES :
@@ -203,6 +206,13 @@ def verify_cleanup():
203
206
for thread in threads :
204
207
assert isinstance (thread , threading ._DummyThread )
205
208
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
+
206
216
207
217
@pytest .fixture (autouse = True )
208
218
def bcrypt_cost ():
@@ -381,7 +391,7 @@ def exc_handle(loop, context):
381
391
382
392
383
393
@pytest .fixture
384
- async def stop_hass ():
394
+ async def stop_hass (event_loop ):
385
395
"""Make sure all hass are stopped."""
386
396
orig_hass = ha .HomeAssistant
387
397
@@ -402,6 +412,7 @@ def mock_hass():
402
412
with patch .object (hass_inst .loop , "stop" ):
403
413
await hass_inst .async_block_till_done ()
404
414
await hass_inst .async_stop (force = True )
415
+ await event_loop .shutdown_default_executor ()
405
416
406
417
407
418
@pytest .fixture
0 commit comments