10869697de
Rewrite des test_06_concurrent (vorher @pytest.mark.skip Skeleton):
- Umbenannt zu 'ContextVar-Isolation zwischen parallelen Routings'
- Szenario: Zwei Nachrichten in zwei Räumen gleichzeitig mit
verschiedenen Keywords ('lan subnetz maskieren' + 'rocketchat ddp bug')
- Verifiziert unabhängige Routings ohne Cross-Contamination
- Assertions: A) Je 1 Bot-Antwort pro Raum, B) Antwort_A referenziert
LAN-Routing, C) Antwort_B referenziert Rocketchat-Routing,
D) KEIN Cross-Talk, E) Message-Ownership, F) Beide innerhalb 60s
- Docstring korrigiert: 'Themenring' → 'ContextVar-Isolation'
(der Themenring als Ring-Datenstruktur ist ein anderes Konzept)
262 lines
9.3 KiB
Python
262 lines
9.3 KiB
Python
"""
|
|
Test 06 — ContextVar-Isolation zwischen parallelen Routings.
|
|
|
|
Verifiziert dass zwei gleichzeitige Nachrichten in verschiedenen Räumen
|
|
mit unterschiedlichen Keywords unabhängig geroutet werden, ohne dass
|
|
die Streams sich kontaminieren (F6 R24: _CURRENT_TASK_TOKEN).
|
|
|
|
Szenario:
|
|
1. /reset in test_room und test-rc-source
|
|
2. Cursor für beide Räume holen
|
|
3. Beide Nachrichten INNERHALB 1s posten:
|
|
- test_room: "lan subnetz maskieren" (keyword "lan" → #LAN-Dokumentation)
|
|
- test-rc-source: "rocketchat ddp bug" (keyword "rocketchat" → #Rocketchat-Plugin)
|
|
4. Auf Bot-Antworten in beiden Räumen warten
|
|
5. Verifiziere: Antwort_A referenziert LAN-Routing,
|
|
Antwort_B referenziert Rocketchat-Routing
|
|
6. Cross-Contamination: Antwort_A nicht "rocketchat",
|
|
Antwort_B nicht "lan"/"subnetz"
|
|
|
|
Dies ist KEIN "Themenring"-Test (der Themenring als Ring-Datenstruktur
|
|
ist ein anderes Konzept). Der Test verifiziert ContextVar-Isolation
|
|
zwischen parallelen Routings — defense-in-depth für F6 R24.
|
|
|
|
ENV-Vars:
|
|
- RC_LIVE_SOURCE_ROOM (test-rc-source)
|
|
- 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_06_concurrent_isolation(
|
|
rc_client,
|
|
test_room_id: str,
|
|
bot_username: str,
|
|
bot_user_id: str,
|
|
rate_limiter,
|
|
cleanup_messages,
|
|
bot_status_check,
|
|
):
|
|
"""ContextVar-Isolation: Parallele Routings in verschiedenen Räumen.
|
|
|
|
Verifiziert dass zwei gleichzeitige Nachrichten mit unterschiedlichen
|
|
Keywords unabhängig geroutet werden — keine Cross-Contamination.
|
|
|
|
Steps:
|
|
1. /reset in beiden Räumen
|
|
2. Cursor holen
|
|
3. Beide Nachrichten innerhalb 1s posten
|
|
4. Auf Bot-Antworten warten
|
|
5. Cross-Contamination-Check
|
|
"""
|
|
source_room = _get_env_room("RC_LIVE_SOURCE_ROOM")
|
|
|
|
posted_ids_test: list = []
|
|
posted_ids_source: list = []
|
|
|
|
# ── Step 1: /reset in beiden Räumen ────────────────────────────────
|
|
# Clear sticky topics from previous tests.
|
|
_reset_test_id, _ = post_test_message(
|
|
rc_client, test_room_id, "/reset",
|
|
posted_ids_test, rate_limiter,
|
|
)
|
|
_reset_source_id, _ = post_test_message(
|
|
rc_client, source_room, "/reset",
|
|
posted_ids_source, rate_limiter,
|
|
)
|
|
logger.info(
|
|
"Test 06: sent /reset to test_room (msg %s) and source_room (msg %s)",
|
|
_reset_test_id[:12], _reset_source_id[:12],
|
|
)
|
|
# Wait for /reset responses
|
|
time.sleep(5.0)
|
|
|
|
# ── Step 2: Cursor für beide Räume holen ───────────────────────────
|
|
cursor_test = get_latest_timestamp(
|
|
rc_client, test_room_id, rate_limiter=rate_limiter
|
|
)
|
|
cursor_source = get_latest_timestamp(
|
|
rc_client, source_room, rate_limiter=rate_limiter
|
|
)
|
|
logger.info(
|
|
"Test 06: cursors — test_room=%s, source=%s",
|
|
cursor_test[:19], cursor_source[:19],
|
|
)
|
|
|
|
# ── Step 3: Beide Nachrichten INNERHALB 1s posten ──────────────────
|
|
# Nachricht A: test_room — "lan subnetz maskieren" (keyword "lan" → #LAN-Dokumentation)
|
|
text_a = "lan subnetz maskieren"
|
|
msg_a_id, cursor_a = post_test_message(
|
|
rc_client, test_room_id, text_a,
|
|
posted_ids_test, rate_limiter,
|
|
)
|
|
|
|
# Nachricht B: test-rc-source — "rocketchat ddp bug" (keyword "rocketchat" → #Rocketchat-Plugin)
|
|
text_b = "rocketchat ddp bug"
|
|
msg_b_id, cursor_b = post_test_message(
|
|
rc_client, source_room, text_b,
|
|
posted_ids_source, rate_limiter,
|
|
)
|
|
|
|
logger.info(
|
|
"Test 06: posted A='%s' (msg=%s) in test_room, B='%s' (msg=%s) in source_room",
|
|
text_a, msg_a_id[:12], text_b, msg_b_id[:12],
|
|
)
|
|
|
|
# ── Step 4: Auf Bot-Antworten warten (sequenziell) ─────────────────
|
|
start_time = time.monotonic()
|
|
|
|
# 4a: Antwort in test_room (LAN-Routing)
|
|
try:
|
|
answer_a = wait_for_bot_message(
|
|
client=rc_client,
|
|
room_id=test_room_id,
|
|
bot_username=bot_username,
|
|
cursor=cursor_a,
|
|
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 test_room within 60s. Error: {e}"
|
|
)
|
|
|
|
answer_a_text = answer_a.get("msg", "")
|
|
answer_a_ts = answer_a.get("ts", "")
|
|
logger.info(
|
|
"Test 06: answer A in test_room (ts=%s): '%s'",
|
|
answer_a_ts[:19], answer_a_text[:200],
|
|
)
|
|
posted_ids_test.append(answer_a.get("_id", ""))
|
|
|
|
# 4b: Antwort in test-rc-source (Rocketchat-Routing)
|
|
try:
|
|
answer_b = wait_for_bot_message(
|
|
client=rc_client,
|
|
room_id=source_room,
|
|
bot_username=bot_username,
|
|
cursor=cursor_b,
|
|
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 source_room within 60s. Error: {e}"
|
|
)
|
|
|
|
answer_b_text = answer_b.get("msg", "")
|
|
answer_b_ts = answer_b.get("ts", "")
|
|
logger.info(
|
|
"Test 06: answer B in source_room (ts=%s): '%s'",
|
|
answer_b_ts[:19], answer_b_text[:200],
|
|
)
|
|
posted_ids_source.append(answer_b.get("_id", ""))
|
|
|
|
elapsed = time.monotonic() - start_time
|
|
|
|
# ── Assertion A: Je 1 Bot-Antwort in jedem Raum ────────────────────
|
|
# (wait_for_bot_message returns exactly 1 — verified by the calls)
|
|
|
|
# ── Assertion B: Antwort_A bezieht sich auf LAN-Routing ────────────
|
|
answer_a_lower = answer_a_text.lower()
|
|
lan_indicators = ["lan", "dokumentation", "subnetz", "netzwerk", "homelab"]
|
|
has_lan_reference = any(ind in answer_a_lower for ind in lan_indicators)
|
|
assert has_lan_reference, (
|
|
f"Answer A does not reference LAN routing. "
|
|
f"Expected one of {lan_indicators}. "
|
|
f"Got: '{answer_a_text[:200]}'"
|
|
)
|
|
|
|
# ── Assertion C: Antwort_B bezieht sich auf Rocketchat-Routing ─────
|
|
answer_b_lower = answer_b_text.lower()
|
|
rc_indicators = ["rocketchat", "plugin", "ddp", "adapter", "gateway"]
|
|
has_rc_reference = any(ind in answer_b_lower for ind in rc_indicators)
|
|
assert has_rc_reference, (
|
|
f"Answer B does not reference Rocketchat routing. "
|
|
f"Expected one of {rc_indicators}. "
|
|
f"Got: '{answer_b_text[:200]}'"
|
|
)
|
|
|
|
# ── Assertion D: KEIN Cross-Talk ───────────────────────────────────
|
|
# Antwort_A (LAN) darf NICHT "rocketchat" enthalten
|
|
assert "rocketchat" not in answer_a_lower, (
|
|
f"Cross-contamination: Answer A (LAN) contains 'rocketchat'. "
|
|
f"Answer: '{answer_a_text[:200]}'"
|
|
)
|
|
|
|
# Antwort_B (Rocketchat) darf NICHT "lan" oder "subnetz" enthalten
|
|
for forbidden in ["lan", "subnetz"]:
|
|
assert forbidden not in answer_b_lower, (
|
|
f"Cross-contamination: Answer B (Rocketchat) contains '{forbidden}'. "
|
|
f"Answer: '{answer_b_text[:200]}'"
|
|
)
|
|
|
|
# ── Assertion E: Beide von bot_username ────────────────────────────
|
|
assert_bot_message(
|
|
answer_a,
|
|
expected_username=bot_username,
|
|
context="test_06_isolation_test_room",
|
|
)
|
|
assert_bot_message(
|
|
answer_b,
|
|
expected_username=bot_username,
|
|
context="test_06_isolation_source_room",
|
|
)
|
|
|
|
# ── Assertion F: Beide innerhalb 60s ───────────────────────────────
|
|
assert elapsed < 60.0, (
|
|
f"Both answers took {elapsed:.1f}s — expected <60s. "
|
|
f"Concurrency may be broken (deadlock?)."
|
|
)
|
|
|
|
# ── 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_source:
|
|
try:
|
|
rc_client.delete_message(source_room, mid)
|
|
except Exception as e:
|
|
logger.warning("Cleanup source: failed to delete %s: %s", mid[:12], e)
|
|
|
|
logger.info("Test 06 PASSED: ContextVar-Isolation zwischen parallelen Routings OK")
|