20aae149c8
test_05 (ContextVar-Isolation): - 10s startup grace before posting (rate-limit window reset) - 5s delay between long/short posts (was: immediate) - Timeout 60s→120s, initial_interval 5s→8s test_06 (Concurrent Isolation): - 10s startup grace before posting - /reset wait 5s→8s, inter-post delay 3s→8s - Timeout 60s→120s, initial_interval 5s→8s test_04 (Multi-Room): - Restructured from keywords_100 auto-route to keywords_context Butler-mode - Uses only keywords_context words (plugin, deployment, archivar, hermes, gateway, sync, button) — no keywords_100 matches - Added /reset step, 10s startup grace, 8s initial_interval - Verifies Butler Move-Request button detection - Copy-to-project-room check uses 'plugin' instead of 'rocketchat'
251 lines
9.4 KiB
Python
251 lines
9.4 KiB
Python
"""
|
|
Test 05 — ContextVar-Isolation unter paralleler Last.
|
|
|
|
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 in test_room, die eine lange Antwort triggert
|
|
(>10s LLM-Stream, z.B. Architektur-Frage ohne 100%-Keywords)
|
|
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
|
|
|
|
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.
|
|
|
|
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
|
|
def test_05_contextvar_isolation_parallel_load(
|
|
rc_client,
|
|
test_room_id: str,
|
|
bot_username: str,
|
|
bot_user_id: str,
|
|
rate_limiter,
|
|
cleanup_messages,
|
|
bot_status_check,
|
|
):
|
|
"""ContextVar-Isolation: Parallele Streams in verschiedenen Räumen.
|
|
|
|
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
|
|
"""
|
|
target_room = _get_env_room("RC_LIVE_TARGET_ROOM")
|
|
|
|
posted_ids_test: list = []
|
|
posted_ids_target: list = []
|
|
|
|
# ── Startup grace: 10s Pause für Bot-Startup und Rate-Limit-Reset ──
|
|
# Vermeidet 429-Kaskaden durch zu schnelles Posten nach Test-Start.
|
|
logger.info("Test 05: 10s startup grace — letting rate-limit window reset")
|
|
time.sleep(10.0)
|
|
|
|
# ── 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 ──────────────
|
|
# Die Frage enthält KEIN 100%-Keyword (kein 'rocketchat', 'ddp', 'websocket',
|
|
# 'plugin', 'gateway', 'hermes', etc.) — sie triggert eine echte LLM-Antwort
|
|
# statt Auto-Route. Thematisch passend für ContextVar-Isolation-Test.
|
|
long_text = (
|
|
"Erkläre ausführlich die Architektur eines Chatbot-Backends mit "
|
|
"Multi-Room-Routing, ContextVar-basierter Task-Isolation und einem "
|
|
"Push-basierten Eingangskanal."
|
|
)
|
|
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],
|
|
)
|
|
|
|
# ── 5s Delay: RC-Rate-Limit atmen lassen vor zweitem Post ──────────
|
|
time.sleep(5.0)
|
|
|
|
# ── Step 3: 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 (initial_interval=8s, timeout=120s)
|
|
try:
|
|
short_answer = wait_for_bot_message(
|
|
client=rc_client,
|
|
room_id=target_room,
|
|
bot_username=bot_username,
|
|
cursor=short_cursor,
|
|
timeout=120.0,
|
|
rate_limiter=rate_limiter,
|
|
initial_interval=8.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 120s. 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 120s 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=120.0,
|
|
rate_limiter=rate_limiter,
|
|
initial_interval=8.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 120s. "
|
|
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")
|