Skip to content

Commit 2ab79d3

Browse files
Jiaqi LvJiaqi Lv
authored andcommitted
improve test coverage
1 parent fd00f12 commit 2ab79d3

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

tests/test_app_bokeh.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import re
1010
import time
1111
from pathlib import Path
12+
from types import SimpleNamespace
1213
from typing import TYPE_CHECKING
1314

1415
import bokeh.models as bkmodels
@@ -25,7 +26,7 @@
2526
from scipy.ndimage import label
2627

2728
from tiatoolbox.data import _fetch_remote_sample
28-
from tiatoolbox.visualization.bokeh_app import main
29+
from tiatoolbox.visualization.bokeh_app import app_hooks, main
2930
from tiatoolbox.visualization.tileserver import TileServer
3031
from tiatoolbox.visualization.ui_utils import get_level_by_extent
3132

@@ -41,6 +42,13 @@
4142
GRIDLINES = 2
4243

4344

45+
class _DummySessionContext:
46+
"""Simple shim matching the subset of Bokeh's SessionContext we use."""
47+
48+
def __init__(self: _DummySessionContext, user: str) -> None:
49+
self.request = SimpleNamespace(arguments={"user": user})
50+
51+
4452
# helper functions and fixtures
4553
def get_tile(layer: str, x: float, y: float, z: float, *, show: bool) -> np.ndarray:
4654
"""Get a tile from the server.
@@ -831,3 +839,47 @@ def test_clearing_doc(doc: Document) -> None:
831839
"""Test that the doc can be cleared."""
832840
doc.clear()
833841
assert len(doc.roots) == 0
842+
843+
844+
def test_app_hooks_session_destroyed(monkeypatch: pytest.MonkeyPatch) -> None:
845+
"""Hook should call reset endpoint and exit."""
846+
recorded: dict[str, object] = {}
847+
848+
def fake_get(url: str, *, timeout: int) -> None:
849+
recorded["url"] = url
850+
recorded["timeout"] = timeout
851+
852+
monkeypatch.setattr(app_hooks, "PORT", "6150")
853+
monkeypatch.setattr(app_hooks.requests, "get", fake_get)
854+
exited = False
855+
856+
def fake_exit() -> None:
857+
nonlocal exited
858+
exited = True
859+
860+
monkeypatch.setattr(app_hooks, "sys", SimpleNamespace(exit=fake_exit))
861+
app_hooks.on_session_destroyed(_DummySessionContext("user-1"))
862+
assert recorded["url"] == "http://127.0.0.1:6150/tileserver/reset/user-1"
863+
assert recorded["timeout"] == 5
864+
assert exited
865+
866+
867+
def test_app_hooks_session_destroyed_suppresses_timeout(
868+
monkeypatch: pytest.MonkeyPatch,
869+
) -> None:
870+
"""ReadTimeout should be suppressed and exit still called."""
871+
872+
def fake_get(*_: object, **__: object) -> None:
873+
raise app_hooks.requests.exceptions.ReadTimeout # type: ignore[attr-defined]
874+
875+
monkeypatch.setattr(app_hooks, "PORT", "6160")
876+
monkeypatch.setattr(app_hooks.requests, "get", fake_get)
877+
exited = False
878+
879+
def fake_exit() -> None:
880+
nonlocal exited
881+
exited = True
882+
883+
monkeypatch.setattr(app_hooks, "sys", SimpleNamespace(exit=fake_exit))
884+
app_hooks.on_session_destroyed(_DummySessionContext("user-2"))
885+
assert exited

0 commit comments

Comments
 (0)