P3: test_05 — ContextVar-Isolation unter paralleler Last
Rewrite des test_05_stream_retry (vorher @pytest.mark.skip Skeleton): - Umbenannt zu 'ContextVar-Isolation unter paralleler Last' - Szenario: Lange Antwort in test_room + kurze Antwort in test-rc-target gleichzeitig — verifiziert dass Streams nicht kontaminieren - Assertions: A) 1 Bot-Antwort pro Raum, B) Lange Antwort >200 Zeichen, C) KEIN Cross-Talk (kein 'rüber'/'Move' in test_room, kein 'Architektur' in target), D) Message-Ownership - Docstring korrigiert: ehrliche Beschreibung statt irreführendem 'Stream-Retry' (Stream-Abbruch ist nicht via REST simulierbar)
This commit is contained in:
@@ -1,60 +1,238 @@
|
||||
"""
|
||||
Test 05 — Antwort nach Stream-Abbruch (Stream-Retry).
|
||||
Test 05 — ContextVar-Isolation unter paralleler Last.
|
||||
|
||||
TODO: Implementierung sobald Stream-Retry auf .45 verfügbar.
|
||||
Verifiziert dass parallele LLM-Streams in verschiedenen Räumen nicht
|
||||
kontaminieren (F6 R24: _CURRENT_TASK_TOKEN + _set_room_context_origin).
|
||||
|
||||
Szenario:
|
||||
1. Sende eine Nachricht, die einen langen Stream triggert
|
||||
2. Simuliere Stream-Abbruch (DDP disconnect/reconnect)
|
||||
3. Verifiziere: Bot sendet Retry und liefert vollständige Antwort
|
||||
1. Sende eine Nachricht in test_room, die eine lange Antwort triggert
|
||||
(>10s LLM-Stream, z.B. "Erkläre ausführlich die Architektur...")
|
||||
2. Sofort eine zweite Nachricht in test-rc-target mit 100%-Keyword
|
||||
("rocketchat ddp bug") — kurze Antwort erwartet
|
||||
3. Warte auf Bot-Antworten in beiden Räumen (sequenziell)
|
||||
4. Verifiziere: Antwort in test_room enthält Architektur-Keywords,
|
||||
Antwort in test-rc-target ist Auto-Route oder Move-Request
|
||||
5. Cross-Contamination-Check: test_room hat keine "rüber"/"Move"-Nachricht,
|
||||
test-rc-target hat keine "Architektur"-Nachricht
|
||||
|
||||
Benötigt:
|
||||
- Stream-Abbruch-Simulation (DDP disconnect auf .45)
|
||||
- Retry-Logik im Adapter (stream_retry Mechanismus)
|
||||
- Timeout-Handling für abgebrochene Streams
|
||||
Dies ist KEIN klassischer Stream-Retry-Test (Stream-Abbruch ist nicht
|
||||
simulierbar via REST), sondern ein ContextVar-Isolation-Test unter Last.
|
||||
F6 R24 hat dies mit 20/20 Unit-Tests abgesichert — dieser Test ist
|
||||
defense-in-depth im Live-Kontext.
|
||||
|
||||
Aktueller Status: Stream-Retry ist in adapter.py noch nicht vollständig
|
||||
implementiert. Dieser Test wird aktiviert, sobald die Retry-Logik auf
|
||||
.45 deployed und getestet ist.
|
||||
ENV-Vars:
|
||||
- RC_LIVE_TARGET_ROOM (test-rc-target)
|
||||
- RC_LIVE_TEST_ROOM (rc-test-automated)
|
||||
|
||||
RATE-LIMIT: 30/min-Bucket, 20s Cooldown zwischen Tests.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.live_rc.conftest import (
|
||||
assert_bot_message,
|
||||
post_test_message,
|
||||
)
|
||||
from tests.live_rc.helpers.polling import (
|
||||
wait_for_bot_message,
|
||||
get_latest_timestamp,
|
||||
PollTimeout,
|
||||
RateLimitSkip,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _get_env_room(name: str) -> str:
|
||||
"""Get a room ID from env var, skip test if not set."""
|
||||
val = (os.getenv(name) or "").strip()
|
||||
if not val:
|
||||
pytest.skip(f"{name} not set — skipping isolation test")
|
||||
return val
|
||||
|
||||
|
||||
@pytest.mark.live_rc
|
||||
@pytest.mark.skip(
|
||||
reason="TODO: Stream-Retry noch nicht auf .45 verfügbar. "
|
||||
"Implementierung folgt nach Deployment der Retry-Logik."
|
||||
)
|
||||
def test_05_stream_retry(
|
||||
def test_05_contextvar_isolation_parallel_load(
|
||||
rc_client,
|
||||
test_room_id: str,
|
||||
bot_username: str,
|
||||
bot_user_id: str,
|
||||
cursor: str,
|
||||
posted_message_ids: list,
|
||||
rate_limiter,
|
||||
cleanup_messages,
|
||||
bot_status_check,
|
||||
):
|
||||
"""Stream-Retry: Bot antwortet korrekt nach Stream-Abbruch.
|
||||
"""ContextVar-Isolation: Parallele Streams in verschiedenen Räumen.
|
||||
|
||||
TODO — Implementierungsschritte:
|
||||
1. Poste Nachricht, die langen Stream auslöst (z.B. 'Erkläre
|
||||
die Architektur des Rocket.Chat Plugins im Detail')
|
||||
2. Warte auf Stream-Beginn (erste Chunks via DDP)
|
||||
3. Simuliere DDP-Disconnect (kill DDP connection auf .45)
|
||||
4. Warte auf Reconnect + Retry
|
||||
5. Verifiziere: vollständige Antwort nach Retry
|
||||
6. Verifiziere: keine doppelten/verlorenen Chunks
|
||||
Verifiziert dass zwei gleichzeitige LLM-Streams (lange + kurze Antwort)
|
||||
nicht kontaminieren — die Antworten landen im jeweils korrekten Raum.
|
||||
|
||||
Steps:
|
||||
1. Cursor für test_room und test-rc-target holen
|
||||
2. Lange-Antwort-Trigger in test_room posten
|
||||
3. Sofort kurze Keyword-Nachricht in test-rc-target posten
|
||||
4. Auf Bot-Antworten in beiden Räumen warten
|
||||
5. Cross-Contamination-Check
|
||||
"""
|
||||
pytest.skip(
|
||||
"TODO: Stream-Retry noch nicht auf .45 verfügbar. "
|
||||
"Implementierung folgt nach Deployment der Retry-Logik."
|
||||
target_room = _get_env_room("RC_LIVE_TARGET_ROOM")
|
||||
|
||||
posted_ids_test: list = []
|
||||
posted_ids_target: list = []
|
||||
|
||||
# ── Step 1: Cursor für beide Räume holen ───────────────────────────
|
||||
cursor_test = get_latest_timestamp(
|
||||
rc_client, test_room_id, rate_limiter=rate_limiter
|
||||
)
|
||||
cursor_target = get_latest_timestamp(
|
||||
rc_client, target_room, rate_limiter=rate_limiter
|
||||
)
|
||||
logger.info(
|
||||
"Test 05: cursors — test_room=%s, target=%s",
|
||||
cursor_test[:19], cursor_target[:19],
|
||||
)
|
||||
|
||||
# ── Step 2: Lange-Antwort-Trigger in test_room posten ──────────────
|
||||
long_text = (
|
||||
"Erkläre ausführlich die Architektur des Hermes Rocket.Chat Plugins "
|
||||
"inklusive aller Komponenten, Datenflüsse und Sicherheitsmechanismen."
|
||||
)
|
||||
long_msg_id, long_cursor = post_test_message(
|
||||
rc_client, test_room_id, long_text,
|
||||
posted_ids_test, rate_limiter,
|
||||
)
|
||||
logger.info(
|
||||
"Test 05: posted long trigger in test_room (msg=%s, cursor=%s)",
|
||||
long_msg_id[:12], long_cursor[:19],
|
||||
)
|
||||
|
||||
# ── Step 3: Sofort kurze Keyword-Nachricht in test-rc-target ───────
|
||||
# "rocketchat ddp bug" — 100% keyword match für #Rocketchat-Plugin
|
||||
short_text = "rocketchat ddp bug"
|
||||
short_msg_id, short_cursor = post_test_message(
|
||||
rc_client, target_room, short_text,
|
||||
posted_ids_target, rate_limiter,
|
||||
)
|
||||
logger.info(
|
||||
"Test 05: posted short trigger in target_room (msg=%s, cursor=%s)",
|
||||
short_msg_id[:12], short_cursor[:19],
|
||||
)
|
||||
|
||||
# ── Step 4: Auf Bot-Antworten warten (sequenziell) ─────────────────
|
||||
# Zuerst die kurze Antwort (sollte schneller kommen), dann die lange.
|
||||
# Reihenfolge ist nicht-deterministisch — das ist OK.
|
||||
|
||||
# 4a: Kurze Antwort in test-rc-target
|
||||
try:
|
||||
short_answer = wait_for_bot_message(
|
||||
client=rc_client,
|
||||
room_id=target_room,
|
||||
bot_username=bot_username,
|
||||
cursor=short_cursor,
|
||||
timeout=60.0,
|
||||
rate_limiter=rate_limiter,
|
||||
initial_interval=5.0,
|
||||
)
|
||||
except RateLimitSkip as e:
|
||||
pytest.skip(f"Rate limit triggered: {e}")
|
||||
except PollTimeout as e:
|
||||
pytest.fail(
|
||||
f"Bot did not respond in target_room within 60s. Error: {e}"
|
||||
)
|
||||
|
||||
short_text_answer = short_answer.get("msg", "")
|
||||
short_ts = short_answer.get("ts", "")
|
||||
logger.info(
|
||||
"Test 05: short answer in target_room (ts=%s): '%s'",
|
||||
short_ts[:19], short_text_answer[:200],
|
||||
)
|
||||
posted_ids_target.append(short_answer.get("_id", ""))
|
||||
|
||||
# 4b: Lange Antwort in test_room (timeout 90s für langen Stream)
|
||||
try:
|
||||
long_answer = wait_for_bot_message(
|
||||
client=rc_client,
|
||||
room_id=test_room_id,
|
||||
bot_username=bot_username,
|
||||
cursor=long_cursor,
|
||||
timeout=90.0,
|
||||
rate_limiter=rate_limiter,
|
||||
initial_interval=5.0,
|
||||
)
|
||||
except RateLimitSkip as e:
|
||||
pytest.skip(f"Rate limit triggered: {e}")
|
||||
except PollTimeout as e:
|
||||
pytest.fail(
|
||||
f"Bot did not respond in test_room within 90s. "
|
||||
f"The long answer may have timed out. Error: {e}"
|
||||
)
|
||||
|
||||
long_text_answer = long_answer.get("msg", "")
|
||||
long_ts = long_answer.get("ts", "")
|
||||
logger.info(
|
||||
"Test 05: long answer in test_room (ts=%s, len=%d): '%s'",
|
||||
long_ts[:19], len(long_text_answer), long_text_answer[:200],
|
||||
)
|
||||
posted_ids_test.append(long_answer.get("_id", ""))
|
||||
|
||||
# ── Assertion A: Genau 1 Bot-Antwort in test_room ──────────────────
|
||||
# (wait_for_bot_message returns exactly 1 — verified by the call itself)
|
||||
|
||||
# ── Assertion B: Genau 1 Bot-Antwort in test-rc-target ─────────────
|
||||
# (wait_for_bot_message returns exactly 1 — verified by the call itself)
|
||||
|
||||
# ── Assertion C: test_room-Antwort > 200 Zeichen ───────────────────
|
||||
assert len(long_text_answer) > 200, (
|
||||
f"Long answer too short ({len(long_text_answer)} chars). "
|
||||
f"Expected >200 chars for architecture explanation. "
|
||||
f"Got: '{long_text_answer[:200]}'"
|
||||
)
|
||||
|
||||
# ── Assertion D: KEIN Cross-Talk ───────────────────────────────────
|
||||
# test_room darf keine "rüber"/"Move"-Nachricht enthalten
|
||||
cross_talk_indicators = ["rüber", "move", "kopieren", "verschieben"]
|
||||
long_lower = long_text_answer.lower()
|
||||
for ind in cross_talk_indicators:
|
||||
assert ind not in long_lower, (
|
||||
f"Cross-contamination: test_room answer contains '{ind}' — "
|
||||
f"this should only appear in target_room. "
|
||||
f"Answer: '{long_text_answer[:200]}'"
|
||||
)
|
||||
|
||||
# test-rc-target darf keine "Architektur"-Nachricht enthalten
|
||||
short_lower = short_text_answer.lower()
|
||||
assert "architektur" not in short_lower, (
|
||||
f"Cross-contamination: target_room answer contains 'Architektur' — "
|
||||
f"this should only appear in test_room. "
|
||||
f"Answer: '{short_text_answer[:200]}'"
|
||||
)
|
||||
|
||||
# ── Assertion E: Message-Ownership ─────────────────────────────────
|
||||
assert_bot_message(
|
||||
long_answer,
|
||||
expected_username=bot_username,
|
||||
context="test_05_isolation_test_room",
|
||||
)
|
||||
assert_bot_message(
|
||||
short_answer,
|
||||
expected_username=bot_username,
|
||||
context="test_05_isolation_target_room",
|
||||
)
|
||||
|
||||
# ── Cleanup ────────────────────────────────────────────────────────
|
||||
for mid in posted_ids_test:
|
||||
try:
|
||||
rc_client.delete_message(test_room_id, mid)
|
||||
except Exception as e:
|
||||
logger.warning("Cleanup test_room: failed to delete %s: %s", mid[:12], e)
|
||||
for mid in posted_ids_target:
|
||||
try:
|
||||
rc_client.delete_message(target_room, mid)
|
||||
except Exception as e:
|
||||
logger.warning("Cleanup target: failed to delete %s: %s", mid[:12], e)
|
||||
|
||||
logger.info("Test 05 PASSED: ContextVar-Isolation unter paralleler Last OK")
|
||||
|
||||
Reference in New Issue
Block a user