fix: stream long-running website operations
This commit is contained in:
@@ -408,3 +408,79 @@ def test_sync_upstream_models_empty_key_value_skips_account(db_session, monkeypa
|
||||
assert len(update_calls) == 0, f"不应调用 update_account,实际调用:{update_calls}"
|
||||
finally:
|
||||
app.dependency_overrides.clear()
|
||||
|
||||
|
||||
def test_sync_upstream_models_concurrent_lock_json_endpoint(db_session, monkeypatch):
|
||||
"""同步上游模型 JSON 接口:并发调用返回 HTTP 409。"""
|
||||
w = Website(
|
||||
name="W1", site_type="sub2api", base_url="http://w1",
|
||||
enabled=True, auth_config_json="{}", timeout_seconds=30,
|
||||
)
|
||||
db_session.add(w)
|
||||
db_session.commit()
|
||||
db_session.refresh(w)
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from app.main import app
|
||||
from app.database import get_db
|
||||
from app.utils.auth import get_current_user
|
||||
|
||||
app.dependency_overrides[get_db] = lambda: db_session
|
||||
app.dependency_overrides[get_current_user] = lambda: None
|
||||
|
||||
try:
|
||||
client = TestClient(app)
|
||||
from app.routers.websites import _try_start_task, _finish_task
|
||||
ok, _sync_token = _try_start_task(w.id, "sync_models")
|
||||
assert ok
|
||||
try:
|
||||
resp = client.post(f"/api/websites/{w.id}/accounts/sync-upstream-models")
|
||||
assert resp.status_code == 409
|
||||
assert "正在执行中" in resp.json()["detail"]
|
||||
finally:
|
||||
_finish_task(w.id, "sync_models", _sync_token)
|
||||
|
||||
resp2 = client.post(f"/api/websites/{w.id}/accounts/sync-upstream-models")
|
||||
assert resp2.status_code == 200
|
||||
finally:
|
||||
app.dependency_overrides.clear()
|
||||
|
||||
|
||||
def test_sync_upstream_models_concurrent_lock_stream_endpoint(db_session, monkeypatch):
|
||||
"""同步上游模型流式接口:并发调用返回 error 事件。"""
|
||||
w = Website(
|
||||
name="W1", site_type="sub2api", base_url="http://w1",
|
||||
enabled=True, auth_config_json="{}", timeout_seconds=30,
|
||||
)
|
||||
db_session.add(w)
|
||||
db_session.commit()
|
||||
db_session.refresh(w)
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from app.main import app
|
||||
from app.database import get_db
|
||||
from app.utils.auth import get_current_user
|
||||
|
||||
app.dependency_overrides[get_db] = lambda: db_session
|
||||
app.dependency_overrides[get_current_user] = lambda: None
|
||||
|
||||
try:
|
||||
client = TestClient(app)
|
||||
from app.routers.websites import _try_start_task, _finish_task
|
||||
ok, _sync_token = _try_start_task(w.id, "sync_models")
|
||||
assert ok
|
||||
try:
|
||||
resp = client.post(f"/api/websites/{w.id}/accounts/sync-upstream-models/stream")
|
||||
assert resp.status_code == 200
|
||||
lines = [line if isinstance(line, str) else line.decode("utf-8") for line in resp.iter_lines() if line]
|
||||
parsed_events = [json.loads(line) for line in lines]
|
||||
assert len(parsed_events) == 1
|
||||
assert parsed_events[0]["event"] == "error"
|
||||
assert "正在执行中" in parsed_events[0]["data"]["message"]
|
||||
finally:
|
||||
_finish_task(w.id, "sync_models", _sync_token)
|
||||
|
||||
resp2 = client.post(f"/api/websites/{w.id}/accounts/sync-upstream-models/stream")
|
||||
assert resp2.status_code == 200
|
||||
finally:
|
||||
app.dependency_overrides.clear()
|
||||
|
||||
Reference in New Issue
Block a user