Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion deeptutor/api/routers/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2486,6 +2486,10 @@ async def stream_service_test_events(service: str, run_id: str, request: Request
_require_settings_admin()
runner = get_config_test_runner()
run = runner.get(run_id)
if run is None:
# Runs are in-memory only: after a restart, or when the page reconnects
# with an id from an earlier process, the stream has nothing to read.
raise HTTPException(status_code=404, detail="Unknown test run")

async def event_stream():
sent = 0
Expand All @@ -2509,7 +2513,8 @@ async def event_stream():
@router.post("/tests/{service}/{run_id}/cancel")
async def cancel_service_test(service: str, run_id: str):
_require_settings_admin()
get_config_test_runner().cancel(run_id)
if not get_config_test_runner().cancel(run_id):
raise HTTPException(status_code=404, detail="Unknown test run")
return {"message": "Cancelled"}


Expand Down
20 changes: 16 additions & 4 deletions deeptutor/services/config/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,23 @@ def start(self, service: str, catalog: dict[str, Any] | None = None) -> TestRun:
thread.start()
return run

def get(self, run_id: str) -> TestRun:
return self._runs[run_id]
def get(self, run_id: str) -> TestRun | None:
"""Return the run, or ``None`` when the id is unknown or already gone.

def cancel(self, run_id: str) -> None:
self.get(run_id).cancelled = True
Runs live in memory only, so a restarted server (or a stale event
stream after the page reloaded) hands the API an id it has never
seen. Callers decide how to answer that; the lookup itself must not
raise.
"""
return self._runs.get(run_id)

def cancel(self, run_id: str) -> bool:
"""Mark a run as cancelled; ``False`` when the id is unknown."""
run = self._runs.get(run_id)
if run is None:
return False
run.cancelled = True
return True

def _run_sync(self, run: TestRun, catalog: dict[str, Any]) -> None:
try:
Expand Down
54 changes: 54 additions & 0 deletions tests/api/test_settings_test_run_lookup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""The Settings connection-test endpoints answer 404 for an unknown run id.

Before this, a stale id (server restarted, page reconnected) turned into a
``KeyError`` and a 500 on the event stream, and the frontend's test card showed
a failed request instead of "this run is gone".
"""

from __future__ import annotations

import asyncio
from types import SimpleNamespace

from fastapi import HTTPException
import pytest

from deeptutor.api.routers import settings as settings_router
from deeptutor.services.config.test_runner import ConfigTestRunner


@pytest.fixture
def runner(monkeypatch: pytest.MonkeyPatch) -> ConfigTestRunner:
empty = ConfigTestRunner()
monkeypatch.setattr(settings_router, "_require_settings_admin", lambda: None)
monkeypatch.setattr(settings_router, "get_config_test_runner", lambda: empty)
return empty


def test_events_for_unknown_run_answer_404(runner: ConfigTestRunner) -> None:
request = SimpleNamespace()

with pytest.raises(HTTPException) as excinfo:
asyncio.run(settings_router.stream_service_test_events("llm", "llm-gone", request))

assert excinfo.value.status_code == 404
assert excinfo.value.detail == "Unknown test run"


def test_cancel_for_unknown_run_answers_404(runner: ConfigTestRunner) -> None:
with pytest.raises(HTTPException) as excinfo:
asyncio.run(settings_router.cancel_service_test("llm", "llm-gone"))

assert excinfo.value.status_code == 404


def test_cancel_for_known_run_confirms(runner: ConfigTestRunner) -> None:
from deeptutor.services.config.test_runner import TestRun

run = TestRun(id="llm-live", service="llm")
runner._runs[run.id] = run # noqa: SLF001 - the runner exposes no inject API

assert asyncio.run(settings_router.cancel_service_test("llm", run.id)) == {
"message": "Cancelled"
}
assert run.cancelled
50 changes: 50 additions & 0 deletions tests/services/config/test_test_runner_lookup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""``ConfigTestRunner`` lookups must not raise for an unknown run id.

Runs live in memory only. A restart, or a Settings page that reconnects with an
id from an earlier process, asks for a run the runner no longer has; that is a
normal outcome, not a crash.
"""

from __future__ import annotations

import pytest

from deeptutor.services.config import test_runner as test_runner_module
from deeptutor.services.config.test_runner import ConfigTestRunner


class _NoThread:
"""Keep ``start`` from launching the real probing thread."""

def __init__(self, *args: object, **kwargs: object) -> None:
pass

def start(self) -> None:
pass


@pytest.fixture
def runner(monkeypatch: pytest.MonkeyPatch) -> ConfigTestRunner:
monkeypatch.setattr(test_runner_module.threading, "Thread", _NoThread)
return ConfigTestRunner()


def test_get_unknown_run_returns_none(runner: ConfigTestRunner) -> None:
assert runner.get("llm-never-started") is None


def test_get_started_run_returns_it(runner: ConfigTestRunner) -> None:
run = runner.start("llm", catalog={})

assert runner.get(run.id) is run


def test_cancel_unknown_run_reports_false(runner: ConfigTestRunner) -> None:
assert runner.cancel("llm-never-started") is False


def test_cancel_known_run_sets_flag_and_reports_true(runner: ConfigTestRunner) -> None:
run = runner.start("llm", catalog={})

assert runner.cancel(run.id) is True
assert run.cancelled