fix(server/debug): robust debugpy attach with bounded wait and tests update
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import sys
|
||||
import threading
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
@@ -27,27 +28,46 @@ def test_debug_endpoint_rejects_invalid_port(monkeypatch, port):
|
||||
_debug_server._debug_endpoint()
|
||||
|
||||
|
||||
def test_connect_debugger_retries_until_adapter_is_ready(monkeypatch):
|
||||
class FakeDebugpy:
|
||||
def __init__(self):
|
||||
self.connect_calls = 0
|
||||
self.wait_calls = 0
|
||||
class FakeDebugpy:
|
||||
def __init__(self, refuse=False, block_wait=False):
|
||||
self.refuse = refuse
|
||||
self.block_wait = block_wait
|
||||
self.connect_calls = 0
|
||||
self.wait_calls = 0
|
||||
|
||||
def connect(self, endpoint):
|
||||
assert endpoint == ("127.0.0.1", 5678)
|
||||
self.connect_calls += 1
|
||||
if self.connect_calls < 3:
|
||||
raise ConnectionRefusedError("listener is starting")
|
||||
def connect(self, endpoint):
|
||||
assert endpoint == ("127.0.0.1", 5678)
|
||||
self.connect_calls += 1
|
||||
if self.refuse:
|
||||
raise ConnectionRefusedError("no listener")
|
||||
|
||||
def wait_for_client(self):
|
||||
self.wait_calls += 1
|
||||
def wait_for_client(self):
|
||||
self.wait_calls += 1
|
||||
if self.block_wait:
|
||||
threading.Event().wait()
|
||||
|
||||
|
||||
def test_connect_debugger_connects_once_and_waits_for_client():
|
||||
fake_debugpy = FakeDebugpy()
|
||||
monkeypatch.setattr(_debug_server.time, "sleep", lambda _seconds: None)
|
||||
|
||||
_debug_server._connect_debugger(
|
||||
fake_debugpy, "127.0.0.1", 5678, timeout=1.0
|
||||
)
|
||||
_debug_server._connect_debugger(fake_debugpy, "127.0.0.1", 5678, timeout=1.0)
|
||||
|
||||
assert fake_debugpy.connect_calls == 3
|
||||
assert fake_debugpy.connect_calls == 1
|
||||
assert fake_debugpy.wait_calls == 1
|
||||
|
||||
|
||||
def test_connect_debugger_does_not_retry_refused_connection():
|
||||
# debugpy.connect() cannot be called a second time after a refused connection.
|
||||
fake_debugpy = FakeDebugpy(refuse=True)
|
||||
|
||||
with pytest.raises(RuntimeError, match="No debugpy listener"):
|
||||
_debug_server._connect_debugger(fake_debugpy, "127.0.0.1", 5678, timeout=1.0)
|
||||
|
||||
assert fake_debugpy.connect_calls == 1
|
||||
|
||||
|
||||
def test_connect_debugger_times_out_on_stale_adapter():
|
||||
fake_debugpy = FakeDebugpy(block_wait=True)
|
||||
|
||||
with pytest.raises(RuntimeError, match="stale debugpy adapter"):
|
||||
_debug_server._connect_debugger(fake_debugpy, "127.0.0.1", 5678, timeout=0.1)
|
||||
|
||||
Reference in New Issue
Block a user