fix(tests): clear diagnostic_mode _bg_tasks between cases

test_fallback_task_retained_until_fire asserts len(_bg_tasks) == 1, but
the set carries pending tasks from earlier tests' fallback schedules, so
the assertion saw the accumulated count instead. Drop the references (no
.cancel() — the tasks belong to closed loops, cross-loop cancel raises
RuntimeError on the next test's setup).
This commit is contained in:
2026-05-28 15:26:11 +03:00
parent 6a8f374678
commit 9aada75381
+14 -2
View File
@@ -23,11 +23,23 @@ from fastapi.testclient import TestClient
def _reset_state() -> None:
"""Clear the module-level ``_active`` dict between tests so prior
activations don't bleed across cases."""
"""Clear the module-level ``_active`` dict and any pending fallback
tasks between tests so prior activations don't bleed across cases.
The ``_bg_tasks`` set retains tasks created via the asyncio-fallback
schedule path; without this, a test that schedules a 30-minute revert
leaves a pending task that inflates the ``len(_bg_tasks)`` invariant
checked by ``test_fallback_task_retained_until_fire``.
"""
from notify_bridge_server.services import diagnostic_mode as svc
svc._active.clear()
# ``_bg_tasks`` from a previous test belong to that test's now-closed
# event loop — calling ``.cancel()`` here crosses loops and raises
# ``RuntimeError: Event loop is closed``. Dropping the references is
# enough: the tasks can't fire on a dead loop, and CPython will GC
# them once the prior loop releases them.
svc._bg_tasks.clear()
@pytest.fixture(autouse=True)