fix(P1): F6 Nachbesserung — 4 fehlende _room_context Stellen + Inkonsistenz + Tests
P0: 4 ungepatchte _CURRENT_ORIGIN_ROOM.set() Stellen mit _room_context gefüllt: - Z.2924: topic_scoped_sticky match → target_room_id - Z.2969: topic_scoped_sticky (zweiter Branch) → target_room_id - Z.3141: Auto-Route (V8.1 P1 path) → target_room_id - Z.3196: Default-Origin (stay_in_room) → room_id P1: send_clarify() Reihenfolge getauscht: - _room_context VOR _THEMATIC_TARGET_ROOM (exakt wie in send()) P1: Helper-Funktion _set_room_context_origin() eingeführt: - Atomare setdefault + Index-Zuweisung unter _room_context_lock - An allen 10 Stellen verwendet (9 _CURRENT_ORIGIN_ROOM.set() + 1 initial set) P1: 3 neue Tests: - Test 8: send_clarify mit leerer ContextVar → _room_context Fallback - Test 9: Concurrency — 10 parallele Tasks, 5 set + 5 read - Test 10: Stale-Entry — _room_context gewinnt gegen _user_rooms
This commit is contained in:
@@ -483,6 +483,151 @@ class TestRoomContextFallback(unittest.TestCase):
|
||||
f"nicht _user_rooms[-1] ({wrong_room}). Bekam: {real_room_id}"
|
||||
)
|
||||
|
||||
# ── Test 8: send_clarify mit leerer ContextVar → _room_context Fallback ──
|
||||
|
||||
def test_send_clarify_with_empty_contextvar_falls_back_to_room_context(self):
|
||||
"""send_clarify() mit leerer ContextVar → _room_context Fallback.
|
||||
|
||||
Deckt die Inkonsistenz ab: send_clarify() hatte _THEMATIC_TARGET_ROOM
|
||||
VOR _room_context geprüft (falsch). Nach dem Fix prüft send_clarify()
|
||||
_CURRENT_ORIGIN_ROOM → _room_context → _THEMATIC_TARGET_ROOM,
|
||||
exakt wie send().
|
||||
"""
|
||||
chat_id = "user:alice"
|
||||
correct_room = "room_clarify_123"
|
||||
thematic_room = "room_thematic_456"
|
||||
|
||||
# ContextVars sind LEER (simuliert Stream-Retry in neuem Task)
|
||||
self._CURRENT_ORIGIN_ROOM.set("")
|
||||
self._THEMATIC_TARGET_ROOM.set(thematic_room) # Archivar hat thematischen Zielraum
|
||||
|
||||
# _room_context hat den korrekten Raum (wurde in _process_incoming gesetzt)
|
||||
with self._room_context_lock:
|
||||
self._room_context[chat_id] = {"origin_room": correct_room}
|
||||
|
||||
# _user_rooms ist LEER
|
||||
self.adapter._user_rooms = {}
|
||||
self.adapter._find_fallback_room_for_user = MagicMock(return_value=None)
|
||||
|
||||
result = asyncio.run(
|
||||
self.adapter.send_clarify(
|
||||
chat_id=chat_id,
|
||||
question="Test question?",
|
||||
choices=["Yes", "No"],
|
||||
clarify_id="clarify_001",
|
||||
session_key="session_001",
|
||||
)
|
||||
)
|
||||
|
||||
# Verify: send_clarify() hat _post_message_with_retry mit dem
|
||||
# _room_context Fallback aufgerufen, NICHT mit _THEMATIC_TARGET_ROOM.
|
||||
self.adapter._post_message_with_retry.assert_called_once()
|
||||
call_args = self.adapter._post_message_with_retry.call_args
|
||||
real_room_id = call_args[0][0]
|
||||
self.assertEqual(
|
||||
real_room_id, correct_room,
|
||||
f"send_clarify() sollte _room_context ({correct_room}) priorisieren, "
|
||||
f"nicht _THEMATIC_TARGET_ROOM ({thematic_room}). Bekam: {real_room_id}"
|
||||
)
|
||||
|
||||
# ── Test 9: Concurrency — 10 parallele Tasks, konsistente Werte ─────
|
||||
|
||||
def test_concurrency_parallel_set_and_read(self):
|
||||
"""10 parallele Tasks: 5 set + 5 read, alle sehen konsistenten Wert.
|
||||
|
||||
Stresstest für _set_room_context_origin() und _room_context_lock.
|
||||
Keine Race-Condition: alle Reader sehen den zuletzt geschriebenen Wert.
|
||||
"""
|
||||
chat_id = "user:concurrent"
|
||||
base_room = "room_concurrent"
|
||||
|
||||
async def _run():
|
||||
results = []
|
||||
|
||||
async def setter(room_suffix: str):
|
||||
"""Setze _room_context auf einen bestimmten Raum."""
|
||||
room = f"{base_room}_{room_suffix}"
|
||||
from plugin.adapter import _set_room_context_origin
|
||||
_set_room_context_origin(chat_id, room)
|
||||
results.append(("set", room_suffix, room))
|
||||
|
||||
async def reader(expected_suffix: str):
|
||||
"""Lies _room_context und prüfe Konsistenz."""
|
||||
from plugin.adapter import _room_context, _room_context_lock
|
||||
with _room_context_lock:
|
||||
ctx = _room_context.get(chat_id, {})
|
||||
origin = ctx.get("origin_room", "")
|
||||
results.append(("read", expected_suffix, origin))
|
||||
|
||||
# 5 Setter + 5 Reader parallel starten
|
||||
tasks = []
|
||||
for i in range(5):
|
||||
tasks.append(asyncio.create_task(setter(f"s{i}")))
|
||||
for i in range(5):
|
||||
tasks.append(asyncio.create_task(reader(f"s{i}")))
|
||||
|
||||
await asyncio.gather(*tasks)
|
||||
return results
|
||||
|
||||
results = asyncio.run(_run())
|
||||
|
||||
# Alle Setter haben ihren Wert gesetzt
|
||||
setters = [r for r in results if r[0] == "set"]
|
||||
self.assertEqual(len(setters), 5, "Alle 5 Setter sollten durchlaufen")
|
||||
|
||||
# Alle Reader haben einen nicht-leeren Wert gesehen
|
||||
readers = [r for r in results if r[0] == "read"]
|
||||
self.assertEqual(len(readers), 5, "Alle 5 Reader sollten durchlaufen")
|
||||
for _, _, origin in readers:
|
||||
self.assertTrue(
|
||||
origin.startswith(base_room),
|
||||
f"Reader sollte einen Raum mit Präfix {base_room} sehen, "
|
||||
f"bekam: {origin}"
|
||||
)
|
||||
|
||||
# ── Test 10: Stale-Entry — _room_context gewinnt gegen _user_rooms ──
|
||||
|
||||
def test_stale_entry_room_context_wins_over_user_rooms(self):
|
||||
"""_room_context hat Raum X, _user_rooms hat Raum Y, ContextVar leer
|
||||
→ _room_context gewinnt (Doku-Test).
|
||||
|
||||
Szenario: User war zuletzt in Raum Y (_user_rooms[-1]), aber die
|
||||
aktuelle Frage kam aus Raum X (_room_context). ContextVar ist leer
|
||||
(Stream-Retry). send() muss _room_context priorisieren.
|
||||
"""
|
||||
chat_id = "user:stale"
|
||||
room_context_room = "room_X_correct"
|
||||
user_rooms_room = "room_Y_wrong"
|
||||
|
||||
# ContextVars sind LEER
|
||||
self._CURRENT_ORIGIN_ROOM.set("")
|
||||
self._THEMATIC_TARGET_ROOM.set("")
|
||||
|
||||
# _room_context hat Raum X (korrekt)
|
||||
with self._room_context_lock:
|
||||
self._room_context[chat_id] = {"origin_room": room_context_room}
|
||||
|
||||
# _user_rooms hat Raum Y (falsch, veraltet)
|
||||
self.adapter._user_rooms = {
|
||||
chat_id: [user_rooms_room]
|
||||
}
|
||||
|
||||
self.adapter._find_fallback_room_for_user = MagicMock(return_value=None)
|
||||
|
||||
result = asyncio.run(
|
||||
self.adapter.send(chat_id=chat_id, content="Test response")
|
||||
)
|
||||
|
||||
# Verify: send() hat _room_context (Raum X) verwendet, nicht _user_rooms (Raum Y)
|
||||
self.adapter._post_message_with_retry.assert_called_once()
|
||||
call_args = self.adapter._post_message_with_retry.call_args
|
||||
real_room_id = call_args[0][0]
|
||||
self.assertEqual(
|
||||
real_room_id, room_context_room,
|
||||
f"send() sollte _room_context ({room_context_room}) priorisieren, "
|
||||
f"nicht _user_rooms[-1] ({user_rooms_room}). Bekam: {real_room_id}"
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user