2026-02-25 19:00:17 +08:00
|
|
|
import asyncio
|
|
|
|
|
import importlib
|
2026-06-06 15:44:00 +08:00
|
|
|
import os
|
2026-02-25 19:00:17 +08:00
|
|
|
import sys
|
|
|
|
|
import threading
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
2026-06-06 15:44:00 +08:00
|
|
|
os.environ["JOB_BACKEND"] = "memory"
|
2026-02-25 19:00:17 +08:00
|
|
|
|
|
|
|
|
BACKEND_DIR = Path(__file__).resolve().parents[1]
|
|
|
|
|
if str(BACKEND_DIR) not in sys.path:
|
|
|
|
|
sys.path.insert(0, str(BACKEND_DIR))
|
|
|
|
|
|
2026-06-06 15:44:00 +08:00
|
|
|
import job_handlers # type: ignore
|
|
|
|
|
import job_system # type: ignore
|
2026-06-08 11:51:39 +08:00
|
|
|
import risk_control # type: ignore
|
|
|
|
|
import session_store # type: ignore
|
|
|
|
|
import audit_store # type: ignore
|
2026-02-25 19:00:17 +08:00
|
|
|
|
2026-06-06 15:44:00 +08:00
|
|
|
main = importlib.import_module("main")
|
2026-02-25 19:00:17 +08:00
|
|
|
|
|
|
|
|
API_KEY_HEADERS = {"X-API-Key": "your-secret-key-here"}
|
|
|
|
|
|
|
|
|
|
|
2026-06-06 15:44:00 +08:00
|
|
|
def setup_function():
|
|
|
|
|
job_system.reset_job_manager()
|
2026-06-08 11:51:39 +08:00
|
|
|
risk_control.reset_risk_controller()
|
|
|
|
|
session_store.reset_session_store()
|
|
|
|
|
audit_store.reset_audit_store()
|
2026-06-06 15:44:00 +08:00
|
|
|
main._handlers_registered = False
|
|
|
|
|
|
|
|
|
|
|
2026-02-25 19:00:17 +08:00
|
|
|
def _completion_payload():
|
|
|
|
|
return {
|
|
|
|
|
"prefix": "hello",
|
|
|
|
|
"suffix": "",
|
|
|
|
|
"languageId": "markdown",
|
|
|
|
|
"model_thinking": "low",
|
|
|
|
|
"privacy_mode": True,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_cancel_endpoint_cancels_running_task(monkeypatch):
|
|
|
|
|
started = threading.Event()
|
|
|
|
|
cancelled = threading.Event()
|
|
|
|
|
|
|
|
|
|
async def fake_call_ollama(*args, **kwargs):
|
|
|
|
|
started.set()
|
|
|
|
|
try:
|
|
|
|
|
while True:
|
|
|
|
|
await asyncio.sleep(0.05)
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
|
cancelled.set()
|
|
|
|
|
raise
|
|
|
|
|
|
2026-06-06 15:44:00 +08:00
|
|
|
monkeypatch.setattr(job_handlers, "call_ollama", fake_call_ollama)
|
|
|
|
|
request_id = "req-cancel-1"
|
2026-02-25 19:00:17 +08:00
|
|
|
|
|
|
|
|
with TestClient(main.app) as client:
|
|
|
|
|
response_box = {}
|
|
|
|
|
|
|
|
|
|
def send_completion():
|
2026-06-06 15:44:00 +08:00
|
|
|
with client.stream(
|
|
|
|
|
"POST",
|
2026-02-25 19:00:17 +08:00
|
|
|
"/v1/completions",
|
2026-06-06 15:44:00 +08:00
|
|
|
headers={**API_KEY_HEADERS, "X-Request-Id": request_id},
|
2026-02-25 19:00:17 +08:00
|
|
|
json=_completion_payload(),
|
2026-06-06 15:44:00 +08:00
|
|
|
) as response:
|
|
|
|
|
response_box["status_code"] = response.status_code
|
|
|
|
|
response_box["body"] = "".join(response.iter_text())
|
2026-02-25 19:00:17 +08:00
|
|
|
|
|
|
|
|
completion_thread = threading.Thread(target=send_completion, daemon=True)
|
|
|
|
|
completion_thread.start()
|
|
|
|
|
|
|
|
|
|
assert started.wait(timeout=2.0)
|
|
|
|
|
|
|
|
|
|
cancel_response = client.post(
|
|
|
|
|
"/v1/completions/cancel",
|
|
|
|
|
headers=API_KEY_HEADERS,
|
|
|
|
|
json={"request_id": request_id, "reason": "superseded"},
|
|
|
|
|
)
|
|
|
|
|
assert cancel_response.status_code == 200
|
|
|
|
|
assert cancel_response.json() == {"cancelled": True, "status": "ok"}
|
|
|
|
|
|
|
|
|
|
completion_thread.join(timeout=5.0)
|
|
|
|
|
assert not completion_thread.is_alive()
|
|
|
|
|
assert cancelled.wait(timeout=2.0)
|
2026-06-06 15:44:00 +08:00
|
|
|
assert "event: cancelled" in response_box["body"]
|
2026-02-25 19:00:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_cancel_not_found():
|
|
|
|
|
with TestClient(main.app) as client:
|
|
|
|
|
response = client.post(
|
|
|
|
|
"/v1/completions/cancel",
|
|
|
|
|
headers=API_KEY_HEADERS,
|
|
|
|
|
json={"request_id": "missing", "reason": "abort"},
|
|
|
|
|
)
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert response.json() == {"cancelled": False, "status": "not_found"}
|