P1c: hasattr-Schutz in connect() — getattr-Pattern statt direktem Attributzugriff
connect() Z.1936: self._gate_sweep_task durch getattr(self, '_gate_sweep_task', None) ersetzt. Beim ersten connect()-Aufruf nach Adapter-Instanziierung existiert _gate_sweep_task noch nicht als Attribut — direkter Zugriff warf AttributeError und connect() brach ab. disconnect() Z.6519 hatte den hasattr-Check bereits. Konsistenz mit disconnect(): gleiches getattr-Pattern (Option 1 nach Rainer). Test: test_connect_fresh_adapter_no_attribute_error - Marker: @pytest.mark.outbound_gate - Verifiziert: connect() auf frischem Adapter (ohne _gate_sweep_task) wirft kein AttributeError und erzeugt _gate_sweep_task korrekt. - Coverage-Lücke geschlossen (Rainer-Review).
This commit is contained in:
+3
-2
@@ -1933,8 +1933,9 @@ class RocketChatAdapter(BasePlatformAdapter):
|
|||||||
self._listen_task = asyncio.create_task(self._listen_loop())
|
self._listen_task = asyncio.create_task(self._listen_loop())
|
||||||
# P5 V9.x W2: OutboundGate Sweep-Loop (asyncio.create_task, nicht threading.Timer)
|
# P5 V9.x W2: OutboundGate Sweep-Loop (asyncio.create_task, nicht threading.Timer)
|
||||||
# ── P1c: Sweep-Task-Check vor Re-create ──
|
# ── P1c: Sweep-Task-Check vor Re-create ──
|
||||||
if self._gate_sweep_task is not None and not self._gate_sweep_task.done():
|
task = getattr(self, '_gate_sweep_task', None)
|
||||||
self._gate_sweep_task.cancel()
|
if task is not None and not task.done():
|
||||||
|
task.cancel()
|
||||||
self._gate_sweep_task = asyncio.create_task(self._gate_sweep_loop())
|
self._gate_sweep_task = asyncio.create_task(self._gate_sweep_loop())
|
||||||
self._last_activity = time.time()
|
self._last_activity = time.time()
|
||||||
logger.info("[rocketchat] Listen loop started, _running=%s", self._running)
|
logger.info("[rocketchat] Listen loop started, _running=%s", self._running)
|
||||||
|
|||||||
@@ -803,3 +803,79 @@ class TestPriority0MismatchTelemetry:
|
|||||||
f"routing_counter 'priority0_token_mismatch_skip' should increment on "
|
f"routing_counter 'priority0_token_mismatch_skip' should increment on "
|
||||||
f"mismatch. Before: {counter_before}, After: {counter_after}"
|
f"mismatch. Before: {counter_before}, After: {counter_after}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# ═══════════════════════════════════════════════════════════════════════════
|
||||||
|
# P1c: connect() auf frischem Adapter wirft kein AttributeError
|
||||||
|
# ═══════════════════════════════════════════════════════════════════════════
|
||||||
|
|
||||||
|
@pytest.mark.outbound_gate
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_connect_fresh_adapter_no_attribute_error():
|
||||||
|
"""P1c: connect() auf frischem Adapter (ohne _gate_sweep_task) wirft kein AttributeError.
|
||||||
|
|
||||||
|
Beim ersten connect()-Aufruf nach Adapter-Instanziierung existiert
|
||||||
|
_gate_sweep_task noch nicht als Attribut. Der getattr-Schutz in connect()
|
||||||
|
muss das abfangen (Konsistenz mit disconnect() Z.6519).
|
||||||
|
|
||||||
|
Regression: Vor P1c-Fix griff connect() direkt auf self._gate_sweep_task
|
||||||
|
zu (Z.1936), was beim ersten Aufruf AttributeError warf.
|
||||||
|
"""
|
||||||
|
import asyncio
|
||||||
|
import threading
|
||||||
|
from unittest.mock import AsyncMock, MagicMock, patch
|
||||||
|
|
||||||
|
from plugin.adapter import RocketChatAdapter
|
||||||
|
|
||||||
|
# Adapter ohne __init__ (Pattern wie andere Tests in dieser Datei)
|
||||||
|
adapter = RocketChatAdapter.__new__(RocketChatAdapter)
|
||||||
|
|
||||||
|
# Minimal-Mocks für connect()-Pfad
|
||||||
|
adapter.base_url = "https://rc.example.com"
|
||||||
|
adapter.user = "testuser"
|
||||||
|
adapter.password = "testpass"
|
||||||
|
adapter.rest = MagicMock()
|
||||||
|
adapter.rest.login = MagicMock(return_value=True)
|
||||||
|
adapter.rest.auth_token = "mock-token"
|
||||||
|
adapter.rest.user_id = "mock-user-id"
|
||||||
|
adapter.rest.get_rooms = MagicMock(return_value=[])
|
||||||
|
adapter._rooms = []
|
||||||
|
adapter._subscribed_rooms = set()
|
||||||
|
adapter._room_info = {}
|
||||||
|
adapter._user_rooms = {}
|
||||||
|
adapter._excluded_room_ids = set()
|
||||||
|
adapter._shutdown_event = threading.Event()
|
||||||
|
adapter._running = False
|
||||||
|
adapter.archivar = None
|
||||||
|
adapter._outbound_gate = MagicMock()
|
||||||
|
|
||||||
|
# Mock _mark_connected (BasePlatformAdapter-Methode)
|
||||||
|
adapter._mark_connected = MagicMock()
|
||||||
|
# Mock _listen_loop und _gate_sweep_loop (AsyncMock für create_task)
|
||||||
|
adapter._listen_loop = AsyncMock()
|
||||||
|
adapter._gate_sweep_loop = AsyncMock()
|
||||||
|
# Mock _sync_room_map_with_membership
|
||||||
|
adapter._sync_room_map_with_membership = MagicMock()
|
||||||
|
|
||||||
|
# Precondition: _gate_sweep_task existiert NICHT (das ist der Bug-Fall)
|
||||||
|
assert not hasattr(adapter, '_gate_sweep_task'), \
|
||||||
|
"Precondition: _gate_sweep_task must not exist on fresh adapter"
|
||||||
|
|
||||||
|
mock_ddp = MagicMock()
|
||||||
|
mock_ddp.connect_and_login = MagicMock(return_value=True)
|
||||||
|
mock_ddp.connected = True
|
||||||
|
mock_ddp.get_logged_in_username = MagicMock(return_value="testuser")
|
||||||
|
mock_ddp.subscribe_room_events = MagicMock(return_value="sub-1")
|
||||||
|
mock_ddp.subscribe_room = MagicMock(return_value="sub-room-1")
|
||||||
|
mock_ddp.bind_loop = MagicMock()
|
||||||
|
|
||||||
|
with patch("plugin.adapter.DDPClient", return_value=mock_ddp), \
|
||||||
|
patch("archivar.onboarding.cleanup_stale_locks", return_value=0), \
|
||||||
|
patch("plugin.adapter._MULTIROOM", return_value=False):
|
||||||
|
result = await adapter.connect(is_reconnect=False)
|
||||||
|
|
||||||
|
assert result is True, "connect() should return True"
|
||||||
|
assert hasattr(adapter, '_gate_sweep_task'), \
|
||||||
|
"connect() should create _gate_sweep_task"
|
||||||
|
assert adapter._gate_sweep_task is not None, \
|
||||||
|
"_gate_sweep_task should be a valid asyncio.Task"
|
||||||
|
|||||||
Reference in New Issue
Block a user