cmvr-visual/backend/tests/test_remote_policy_refresh.py

142 lines
5.2 KiB
Python
Raw Normal View History

"""Remote policy admission stays correct when no browser is polling status."""
import asyncio
import json
import time
from types import SimpleNamespace
import httpx
import pytest
import pytest_asyncio
from app.adapters.provider.client import Provider
from app.core.runtime_policy import read_runtime_policy
from test_jobs import submit
@pytest_asyncio.fixture
async def remote_policy(system):
settings = system.settings
settings.inference_remote_files = True
settings.runtime_policy_file = settings.workspace_root / "config" / "runtime-policy.json"
settings.runtime_policy_file.parent.mkdir(parents=True)
settings.runtime_policy_file.write_text(
json.dumps({"schema_version": 1, "batch_window_enabled": False, "workflow_max_shots": 240})
)
boundary = SimpleNamespace(
calls=0,
status=200,
hang=False,
policy={
"batch_window_enabled": False,
"workflow_max_shots": 240,
"configuration_error": None,
},
)
async def respond(request):
assert request.url.path == "/runtime/status"
boundary.calls += 1
if boundary.hang:
await asyncio.sleep(60)
return httpx.Response(
boundary.status,
json={
"schema_version": 1,
"monitor": {
"schema_version": 1,
"generated_at": None,
"error": "gpu_monitor_unavailable",
"gpus": [],
},
"scheduler": None,
"runtime_policy": boundary.policy,
},
)
settings.inference_api_key_file = None
real = Provider(settings)
await real.client.aclose()
real.client = httpx.AsyncClient(transport=httpx.MockTransport(respond))
system.provider.runtime_status = real.runtime_status
boundary.real = real
try:
yield boundary
finally:
await real.aclose()
async def test_tick_refreshes_remote_policy_without_browser_and_recovers_batch(
system, remote_policy
):
job = (
await submit(
system, task_type="math.solve", input={"prompt": "2+3"}, execution_mode="batch"
)
)[0]
remote_policy.policy["configuration_error"] = "runtime_policy_invalid"
await system.app.state.coordinator.run_once(force=True)
assert remote_policy.calls == 1
assert system.provider.cpu_runs == 0
assert read_runtime_policy(system.settings)["configuration_error"] == "runtime_policy_invalid"
# No HTTP status route runs between invalidation and automatic recovery.
remote_policy.policy["configuration_error"] = None
remote_policy.real._runtime_cache = None
await system.app.state.coordinator.run_once(force=True)
await system.app.state.coordinator._cpu[job["id"]]
assert remote_policy.calls == 2
assert system.provider.cpu_runs == 1
assert read_runtime_policy(system.settings)["configuration_error"] is None
async def test_unreachable_policy_keeps_file_but_only_blocks_batch_work(system, remote_policy):
await remote_policy.real.runtime_status() # Previously permissive, recently valid file.
policy_bytes = system.settings.runtime_policy_file.read_bytes()
await submit(system, task_type="math.solve", input={"prompt": "batch"}, execution_mode="batch")
interactive = (await submit(system, task_type="math.solve", input={"prompt": "interactive"}))[0]
remote_policy.real._runtime_cache = None
remote_policy.status = 503
await system.app.state.coordinator.run_once(force=True)
await system.app.state.coordinator._cpu[interactive["id"]]
assert remote_policy.calls == 2
assert system.provider.cpu_runs == 1
assert system.settings.runtime_policy_file.read_bytes() == policy_bytes
assert (
read_runtime_policy(system.settings)["configuration_error"] == "runtime_policy_unavailable"
)
async def test_remote_refresh_timeout_does_not_stall_cancellation(system, remote_policy):
# Establish a remote GPU job without depending on the new refresh path.
system.settings.inference_remote_files = False
job = (await submit(system))[0]
coordinator = system.app.state.coordinator
await coordinator.run_once(force=True)
current = await coordinator._job(job["id"])
assert current.upstream_id
response = await system.client.post("/api/jobs/" + job["id"] + "/cancel")
assert response.status_code == 200
system.settings.inference_remote_files = True
await remote_policy.real.runtime_status()
remote_policy.real._runtime_cache = None
remote_policy.hang = True
started = time.monotonic()
await asyncio.wait_for(coordinator.run_once(force=True), timeout=5)
assert time.monotonic() - started < 5
assert (await coordinator._job(job["id"])).state == "cancelled"
assert (
read_runtime_policy(system.settings)["configuration_error"] == "runtime_policy_unavailable"
)
async def test_coordinator_shutdown_cancellation_is_not_swallowed(system, remote_policy):
remote_policy.hang = True
task = asyncio.create_task(system.app.state.coordinator.run_once(force=True))
for _ in range(100):
if remote_policy.calls:
break
await asyncio.sleep(0.01)
task.cancel()
with pytest.raises(asyncio.CancelledError):
await task