2026-11-11 11:44:50 +08:00
|
|
|
"""The optional owner queue cap must not constrain normal video creation."""
|
2026-11-14 14:07:26 +08:00
|
|
|
|
2026-11-11 11:44:50 +08:00
|
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
2026-11-14 14:07:26 +08:00
|
|
|
from app.core.config import Settings
|
2026-11-11 11:44:50 +08:00
|
|
|
from app.models import Job, new_id, utcnow
|
2026-11-14 14:07:26 +08:00
|
|
|
from app.models import WorkflowRun, WorkflowStep
|
2026-11-11 11:44:50 +08:00
|
|
|
from sqlalchemy import select
|
|
|
|
|
from test_workflows_v2 import wf, draft, run
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_interactive_gpu_limit_is_disabled_by_default(tmp_path):
|
2026-11-14 14:07:26 +08:00
|
|
|
settings = Settings(
|
|
|
|
|
mode="test", database_url="sqlite+aiosqlite:///:memory:", workspace_root=tmp_path
|
|
|
|
|
)
|
2026-11-11 11:44:50 +08:00
|
|
|
assert settings.max_interactive_gpu_jobs == 0
|
|
|
|
|
|
|
|
|
|
|
2026-11-14 14:07:26 +08:00
|
|
|
@pytest.mark.parametrize("limit", [-1, -8])
|
2026-11-11 11:44:50 +08:00
|
|
|
def test_negative_interactive_gpu_limit_is_rejected(tmp_path, limit):
|
2026-11-14 14:07:26 +08:00
|
|
|
with pytest.raises(ValueError, match="MAX_INTERACTIVE_GPU_JOBS.*non-negative"):
|
|
|
|
|
Settings(
|
|
|
|
|
mode="test",
|
|
|
|
|
database_url="sqlite+aiosqlite:///:memory:",
|
|
|
|
|
workspace_root=tmp_path,
|
|
|
|
|
max_interactive_gpu_jobs=limit,
|
|
|
|
|
)
|
2026-11-11 11:44:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def future_video(system, index):
|
2026-11-14 14:07:26 +08:00
|
|
|
return await system.client.post(
|
|
|
|
|
"/api/jobs",
|
|
|
|
|
json={
|
|
|
|
|
"task_type": "video.generate",
|
|
|
|
|
"input": {"prompt": f"future video {index}"},
|
|
|
|
|
"parameters": {},
|
|
|
|
|
"scheduled_at": (datetime.now(timezone.utc) + timedelta(days=1)).isoformat(),
|
|
|
|
|
},
|
|
|
|
|
)
|
2026-11-11 11:44:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_zero_limit_accepts_nine_unfinished_future_video_jobs(system):
|
|
|
|
|
system.settings.max_interactive_gpu_jobs = 0
|
|
|
|
|
system.settings.max_creation_commands_per_minute = 20
|
|
|
|
|
for index in range(9):
|
|
|
|
|
response = await future_video(system, index)
|
|
|
|
|
assert response.status_code == 202, response.text
|
2026-11-14 14:07:26 +08:00
|
|
|
assert response.json()["jobs"][0]["state"] == "scheduled"
|
|
|
|
|
assert (await system.client.get("/api/jobs")).json()["total"] == 9
|
2026-11-11 11:44:50 +08:00
|
|
|
await system.app.state.coordinator.run_once(force=True)
|
|
|
|
|
assert not system.provider.accepted
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_positive_limit_two_rejects_third_unfinished_video(system):
|
|
|
|
|
system.settings.max_interactive_gpu_jobs = 2
|
|
|
|
|
assert (await future_video(system, 0)).status_code == 202
|
|
|
|
|
assert (await future_video(system, 1)).status_code == 202
|
|
|
|
|
response = await future_video(system, 2)
|
|
|
|
|
assert response.status_code == 429
|
2026-11-14 14:07:26 +08:00
|
|
|
assert response.json()["detail"]["code"] == "quota_exceeded"
|
|
|
|
|
assert (await system.client.get("/api/jobs")).json()["total"] == 2
|
2026-11-11 11:44:50 +08:00
|
|
|
assert not system.provider.accepted
|
|
|
|
|
|
|
|
|
|
|
2026-11-14 14:07:26 +08:00
|
|
|
def fixture_job(owner_id, *, state="scheduled", **fields):
|
2026-11-11 11:44:50 +08:00
|
|
|
identifier = new_id()
|
2026-11-14 14:07:26 +08:00
|
|
|
return Job(
|
|
|
|
|
id=identifier,
|
|
|
|
|
owner_id=owner_id,
|
|
|
|
|
task_type="video.generate",
|
|
|
|
|
execution_mode="interactive",
|
|
|
|
|
state=state,
|
|
|
|
|
phase=state,
|
|
|
|
|
input_json={"prompt": "future fixture video"},
|
|
|
|
|
parameters={},
|
|
|
|
|
request_hash="fixture",
|
|
|
|
|
upstream_key=identifier,
|
|
|
|
|
scheduled_at=utcnow() + timedelta(days=1),
|
|
|
|
|
**fields,
|
|
|
|
|
)
|
2026-11-11 11:44:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_default_limit_allows_standalone_retry_with_eight_existing_active_jobs(system):
|
2026-11-14 14:07:26 +08:00
|
|
|
owner = (await system.client.get("/api/auth/me")).json()["user"]["id"]
|
2026-11-11 11:44:50 +08:00
|
|
|
async with system.app.state.db.session() as session:
|
|
|
|
|
session.add_all(fixture_job(owner) for _ in range(8))
|
2026-11-14 14:07:26 +08:00
|
|
|
old = fixture_job(owner, state="failed")
|
2026-11-11 11:44:50 +08:00
|
|
|
session.add(old)
|
|
|
|
|
await session.commit()
|
2026-11-14 14:07:26 +08:00
|
|
|
response = await system.client.post("/api/jobs/" + old.id + "/retry")
|
2026-11-11 11:44:50 +08:00
|
|
|
assert response.status_code == 202, response.text
|
2026-11-14 14:07:26 +08:00
|
|
|
assert response.json()["jobs"][0]["id"] != old.id
|
|
|
|
|
assert (await system.client.get("/api/jobs")).json()["total"] == 10
|
2026-11-11 11:44:50 +08:00
|
|
|
assert not system.provider.accepted
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_default_limit_allows_workflow_step_retry_with_eight_existing_active_jobs(wf):
|
|
|
|
|
snapshot = await run(wf, await draft(wf))
|
2026-11-14 14:07:26 +08:00
|
|
|
rid = snapshot["run"]["id"]
|
|
|
|
|
owner = (await wf.client.get("/api/auth/me")).json()["user"]["id"]
|
2026-11-11 11:44:50 +08:00
|
|
|
async with wf.app.state.db.session() as session:
|
|
|
|
|
session.add_all(fixture_job(owner) for _ in range(8))
|
2026-11-14 14:07:26 +08:00
|
|
|
old = fixture_job(owner, state="failed", run_id=rid, step_key="shot_1", explicit_attempt=1)
|
2026-11-11 11:44:50 +08:00
|
|
|
session.add(old)
|
2026-11-14 14:07:26 +08:00
|
|
|
step = await session.scalar(
|
|
|
|
|
select(WorkflowStep).where(
|
|
|
|
|
WorkflowStep.run_id == rid, WorkflowStep.step_key == "shot_1"
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
step.selected_job_id, step.state, step.explicit_attempt = old.id, "blocked", 1
|
2026-11-11 11:44:50 +08:00
|
|
|
parent = await session.get(WorkflowRun, rid)
|
2026-11-14 14:07:26 +08:00
|
|
|
parent.state = "blocked"
|
2026-11-11 11:44:50 +08:00
|
|
|
await session.commit()
|
2026-11-14 14:07:26 +08:00
|
|
|
response = await wf.client.post(
|
|
|
|
|
f"/api/workflow-runs/{rid}/steps/shot_1/retry",
|
|
|
|
|
json={"expected_run_version": snapshot["run"]["version"], "target_job_id": old.id},
|
|
|
|
|
headers={"Idempotency-Key": "retry-with-eight-active"},
|
|
|
|
|
)
|
2026-11-11 11:44:50 +08:00
|
|
|
assert response.status_code == 202, response.text
|
2026-11-14 14:07:26 +08:00
|
|
|
selected = next(step for step in response.json()["steps"] if step["step_key"] == "shot_1")
|
|
|
|
|
assert selected["selected_job_id"] != old.id
|
|
|
|
|
assert (await wf.client.get("/api/jobs")).json()["total"] == 10
|
2026-11-11 11:44:50 +08:00
|
|
|
assert not wf.provider.accepted
|