fix(B4): Pytest fixture engine.shutdown() — Test-Flakiness durch leaked Honeymoon-Timer behoben

This commit is contained in:
Bernd (Hermes)
2026-07-24 17:02:21 +02:00
parent 56baaaf038
commit fac0a79644
@@ -20,6 +20,47 @@ import pytest
# Ensure the plugin package is importable # Ensure the plugin package is importable
sys.path.insert(0, str(Path(__file__).resolve().parents[2])) sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
# ═══════════════════════════════════════════════════════════════════════════
# B4: Autouse fixture — engine.shutdown() nach jedem Test
# Verhindert dass Honeymoon-/Coalesce-/Pulse-/Detection-Timer in
# nachfolgende Tests leaken (20-30% Failure-Rate).
# ═══════════════════════════════════════════════════════════════════════════
_ENGINE_REGISTRY: list = []
@pytest.fixture(autouse=True)
def _shutdown_presence_engines():
"""Track all PresenceEngine instances and call shutdown() after each test.
Monkey-patches PresenceEngine.__init__ to register every instance.
After the test (in finally), calls engine.shutdown() on all registered
instances — stops Honeymoon, Coalesce, PulseController, DetectionTimer.
"""
from plugin.archivar.presence_engine import PresenceEngine
_original_init = PresenceEngine.__init__
def _tracking_init(self, *args, **kwargs):
_original_init(self, *args, **kwargs)
_ENGINE_REGISTRY.append(self)
PresenceEngine.__init__ = _tracking_init
_ENGINE_REGISTRY.clear()
try:
yield
finally:
PresenceEngine.__init__ = _original_init
for engine in _ENGINE_REGISTRY:
try:
engine.shutdown()
except Exception:
pass
_ENGINE_REGISTRY.clear()
# ═══════════════════════════════════════════════════════════════════════════ # ═══════════════════════════════════════════════════════════════════════════
# Phase 1: Basis-PresenceEngine # Phase 1: Basis-PresenceEngine
# ═══════════════════════════════════════════════════════════════════════════ # ═══════════════════════════════════════════════════════════════════════════