From 5c223912879bf333742cb6fb3020328ad275e77c Mon Sep 17 00:00:00 2001 From: "Bernd (Hermes)" Date: Thu, 30 Jul 2026 09:10:07 +0200 Subject: [PATCH] =?UTF-8?q?P1c:=20hasattr-Schutz=20in=20connect()=20?= =?UTF-8?q?=E2=80=94=20getattr-Pattern=20statt=20direktem=20Attributzugrif?= =?UTF-8?q?f?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). --- plugin/adapter.py | 5 +-- tests/test_adapter.py | 76 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/plugin/adapter.py b/plugin/adapter.py index 62c1492..da73d03 100644 --- a/plugin/adapter.py +++ b/plugin/adapter.py @@ -1933,8 +1933,9 @@ class RocketChatAdapter(BasePlatformAdapter): self._listen_task = asyncio.create_task(self._listen_loop()) # P5 V9.x W2: OutboundGate Sweep-Loop (asyncio.create_task, nicht threading.Timer) # ── P1c: Sweep-Task-Check vor Re-create ── - if self._gate_sweep_task is not None and not self._gate_sweep_task.done(): - self._gate_sweep_task.cancel() + task = getattr(self, '_gate_sweep_task', None) + if task is not None and not task.done(): + task.cancel() self._gate_sweep_task = asyncio.create_task(self._gate_sweep_loop()) self._last_activity = time.time() logger.info("[rocketchat] Listen loop started, _running=%s", self._running) diff --git a/tests/test_adapter.py b/tests/test_adapter.py index 4a1afff..e0520f5 100644 --- a/tests/test_adapter.py +++ b/tests/test_adapter.py @@ -803,3 +803,79 @@ class TestPriority0MismatchTelemetry: f"routing_counter 'priority0_token_mismatch_skip' should increment on " 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"