Files
Hermes_rocket.chat_Plugin/tests/test_f072_process_isolation.py

89 lines
4.4 KiB
Python

"""
F072 — Prozess-Isolation Plugin/Core.
Verifiziert den Proof-of-Concept (Stufe 1a) in plugin/isolation_worker.py:
1. Normale isolierte Ausführung liefert korrektes Ergebnis.
2. Timeout im Worker → IsolationError, Hauptprozess lebt weiter.
3. os._exit-Crash im Worker → Hauptprozess (Testprozess) überlebt UND der
Fehler wird als IsolationError abgefangen (Gateway verschont).
Der Proof-of-Concept beweist: Ein Worker-Subprozess kann hart crashen, ohne
den Core-/Gateway-Prozess mitzureißen.
@implements: [F-072]
"""
from __future__ import annotations
import os
from pathlib import Path
import sys
import pytest
_PLUGIN_DIR = str(Path(__file__).resolve().parent.parent / "plugin")
if _PLUGIN_DIR not in sys.path:
sys.path.insert(0, _PLUGIN_DIR)
import isolation_worker # noqa: E402
# ═══════════════════════════════════════════════════════════════════════════
# Test 1: Normale isolierte Ausführung (Roundtrip)
# ═══════════════════════════════════════════════════════════════════════════
def test_f072_isolated_keyword_extract_ok():
payload = {"texts": ["deployment status grün", "fehler firewall"]}
result = isolation_worker.submit_isolated("keyword_extract_batch", payload, timeout_s=30)
assert "doc1" in result
assert result["doc1"]["words"] == 3
# 'status' sollte unter top-3 sein (alle 3 Wörter eindeutig)
assert "status" in result["doc1"]["top"]
def test_f072_unknown_worker_raises_isolation_error():
with pytest.raises(isolation_worker.IsolationError) as excinfo:
isolation_worker.submit_isolated("nicht_existent", None)
assert "unbekannter Worker-Key" in str(excinfo.value.reason)
# ═══════════════════════════════════════════════════════════════════════════
# Test 2: Harte Timeout-Schranke → IsolationError, Hauptprozess lebt weiter
# ═══════════════════════════════════════════════════════════════════════════
def test_f072_timeout_isolated_does_not_kill_gateway():
# Nutzt den auf Modul-Ebene registrierten slow_sleeper-Worker, damit er
# im Subprozess verfügbar (picklbar) ist.
with pytest.raises(isolation_worker.IsolationError) as excinfo:
isolation_worker.submit_isolated("slow_sleeper", 60, timeout_s=1.0)
assert "Timeout" in excinfo.value.reason
# Wichtig: der Testprozess (stehen für den Gateway) lebt weiter:
assert os.getpid() > 0
# ═══════════════════════════════════════════════════════════════════════════
# Test 3: os._exit-Crash im Worker → Gateway überlebt, Fehler gefangen
# ═══════════════════════════════════════════════════════════════════════════
def test_f072_os_exit_crash_isolated_does_not_kill_gateway():
pid_before = os.getpid()
with pytest.raises(isolation_worker.IsolationError) as excinfo:
isolation_worker.submit_isolated("crash_via_exit", 3, timeout_s=15)
# Hauptprozess (Gateway-Stellvertreter) lebt weiter:
assert os.getpid() == pid_before
assert "SystemExit" in excinfo.value.reason or "Process" in str(excinfo.value)
# ═══════════════════════════════════════════════════════════════════════════
# Teardown
# ═══════════════════════════════════════════════════════════════════════════
@pytest.fixture(scope="module", autouse=True)
def _shutdown_executor():
yield
isolation_worker.shutdown()