|
| 1 | +"""The frontend-connect wait must never hang forever (issue #281). |
| 2 | +
|
| 3 | +Pure-python tests: the helper takes its clock, sleeper, and environment as |
| 4 | +parameters, so no kernel, websocket, or notebook is involved. |
| 5 | +""" |
| 6 | +import pytest |
| 7 | + |
| 8 | +from vpython._frontend_wait import wait_for_frontend, DEFAULT_TIMEOUT |
| 9 | + |
| 10 | + |
| 11 | +class FakeClock: |
| 12 | + def __init__(self): |
| 13 | + self.now = 0.0 |
| 14 | + |
| 15 | + def time(self): |
| 16 | + return self.now |
| 17 | + |
| 18 | + def sleep(self, dt): |
| 19 | + self.now += dt |
| 20 | + |
| 21 | + |
| 22 | +def test_returns_as_soon_as_the_frontend_connects(): |
| 23 | + clock = FakeClock() |
| 24 | + flips_at = 1.0 |
| 25 | + wait_for_frontend(lambda: clock.now >= flips_at, |
| 26 | + environ={}, _time=clock.time, _sleep=clock.sleep) |
| 27 | + assert clock.now < flips_at + 1.0 # returned promptly, no full-timeout burn |
| 28 | + |
| 29 | + |
| 30 | +def test_raises_instead_of_hanging_when_nothing_ever_connects(): |
| 31 | + clock = FakeClock() |
| 32 | + with pytest.raises(RuntimeError) as exc: |
| 33 | + wait_for_frontend(lambda: False, |
| 34 | + environ={}, _time=clock.time, _sleep=clock.sleep) |
| 35 | + msg = str(exc.value) |
| 36 | + assert 'no VPython frontend connected' in msg |
| 37 | + assert 'vpython-jupyter/issues/281' in msg |
| 38 | + assert 'Jupyter' in msg # points at an environment that works |
| 39 | + |
| 40 | + |
| 41 | +def test_message_names_vscode_when_running_under_vscode(): |
| 42 | + clock = FakeClock() |
| 43 | + with pytest.raises(RuntimeError) as exc: |
| 44 | + wait_for_frontend(lambda: False, |
| 45 | + environ={'VSCODE_PID': '123'}, |
| 46 | + _time=clock.time, _sleep=clock.sleep) |
| 47 | + assert 'VS Code' in str(exc.value) |
| 48 | + |
| 49 | + |
| 50 | +def test_message_names_colab_when_running_under_colab(): |
| 51 | + clock = FakeClock() |
| 52 | + with pytest.raises(RuntimeError) as exc: |
| 53 | + wait_for_frontend(lambda: False, |
| 54 | + environ={'COLAB_RELEASE_TAG': 'x'}, |
| 55 | + _time=clock.time, _sleep=clock.sleep) |
| 56 | + assert 'Colab' in str(exc.value) |
| 57 | + |
| 58 | + |
| 59 | +def test_timeout_is_overridable_via_environment(): |
| 60 | + clock = FakeClock() |
| 61 | + with pytest.raises(RuntimeError): |
| 62 | + wait_for_frontend(lambda: False, |
| 63 | + environ={'VPYTHON_CONNECT_TIMEOUT': '2'}, |
| 64 | + _time=clock.time, _sleep=clock.sleep) |
| 65 | + assert clock.now < DEFAULT_TIMEOUT # honored the shorter override |
| 66 | + |
| 67 | + |
| 68 | +def test_bogus_override_falls_back_to_default(): |
| 69 | + clock = FakeClock() |
| 70 | + with pytest.raises(RuntimeError): |
| 71 | + wait_for_frontend(lambda: False, |
| 72 | + environ={'VPYTHON_CONNECT_TIMEOUT': 'soon'}, |
| 73 | + _time=clock.time, _sleep=clock.sleep) |
| 74 | + assert clock.now >= DEFAULT_TIMEOUT |
0 commit comments