fix(B1): Future-Cleanup bei Disconnect — _pending_results werden mit DDPNotConnected aufgelöst
- _cancel_pending_results() löst alle pending Futures mit DDPNotConnected auf - disconnect() ruft _cancel_pending_results() nach Socket-Close - _reader_loop Exit-Pfad ruft _cancel_pending_results() nach self.connected=False - PulseController._pulse_tick_async fängt Exception (inkl. DDPNotConnected) und fällt auf REST zurück - 2 neue Tests: test_disconnect_clears_pending_futures, test_reader_loop_exit_clears_pending_futures
This commit is contained in:
@@ -481,3 +481,61 @@ class TestLegacyTimerSetStatusRemainsSync:
|
||||
adapter.ddp.set_status("away")
|
||||
|
||||
mock_ddp.set_status.assert_called_with("away")
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
# Test 11: B1 — disconnect clears pending Futures with DDPNotConnected
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
class TestDisconnectClearsPendingFutures:
|
||||
"""B1: disconnect() löst alle pending Futures mit DDPNotConnected auf."""
|
||||
|
||||
def test_disconnect_clears_pending_futures(self):
|
||||
"""Engine erstellt, set_status_async aufrufen, disconnect() → DDPNotConnected."""
|
||||
client = _make_ddp_client(connected=True)
|
||||
client._send_json = MagicMock()
|
||||
|
||||
async def _run():
|
||||
# Starte set_status_async — registriert einen Future
|
||||
task = asyncio.create_task(client.set_status_async("online", timeout=5.0))
|
||||
await asyncio.sleep(0.05)
|
||||
|
||||
# Verifiziere: Future ist registriert
|
||||
with client._result_lock:
|
||||
assert len(client._pending_results) == 1
|
||||
|
||||
# Disconnect — muss den Future mit DDPNotConnected auflösen
|
||||
client.disconnect()
|
||||
|
||||
# Der Task sollte jetzt DDPNotConnected werfen
|
||||
with pytest.raises(DDPNotConnected) as exc_info:
|
||||
await task
|
||||
assert "connection closed" in str(exc_info.value).lower()
|
||||
|
||||
# _pending_results muss leer sein
|
||||
with client._result_lock:
|
||||
assert len(client._pending_results) == 0
|
||||
|
||||
asyncio.run(_run())
|
||||
|
||||
def test_reader_loop_exit_clears_pending_futures(self):
|
||||
"""Simuliere reader-loop exit: _cancel_pending_results wird aufgerufen."""
|
||||
client = _make_ddp_client(connected=True)
|
||||
client._send_json = MagicMock()
|
||||
|
||||
# Registriere einen pending Future manuell
|
||||
future = concurrent.futures.Future()
|
||||
with client._result_lock:
|
||||
client._pending_results["st9999999999_1"] = future
|
||||
|
||||
# Simuliere reader-loop exit (wie in _reader_loop nach while-Schleife)
|
||||
client._cancel_pending_results()
|
||||
|
||||
# Future muss DDPNotConnected haben
|
||||
assert future.done()
|
||||
with pytest.raises(DDPNotConnected):
|
||||
future.result()
|
||||
|
||||
# Store muss leer sein
|
||||
with client._result_lock:
|
||||
assert len(client._pending_results) == 0
|
||||
|
||||
Reference in New Issue
Block a user