Files
vdm/daemon/tests/sched_scheduler_test.cpp
T
samiandClaude Sonnet 5 d93c8e10a0 daemon: sched/scheduler — governor <-> store <-> engine, against an EnginePort seam
The Scheduler that D4 was waiting on. Built against CORE's engine
HEADERS (now in main); the real EnginePort and the veloxd wiring wait
for lane/core's stage-8 bodies to reach main (deferrals.md D4a/D4b) —
core/src/task/ is still .gitkeep there, so linking vdm::Engine now
would be an unresolved symbol.

- sched/engine_port — the abstract seam: start/pause/resume/cancel/
  provide_auth/decide/refresh_url + the ADR 0011 admission config
  (set_task_order / set_max_active_segments / set_host_segment_cap).
  Keeps the Scheduler testable without a live engine and the daemon
  unbound from the concrete vdm::Engine.
- sched/fake_engine_port — a recording impl for tests.
- sched/scheduler:
  * owns the wire-UUID <-> vdm::TaskId map.
  * tick(): snapshot queues (schedule window evaluated with an
    injectable clock) + non-terminal tasks -> governor.evaluate ->
    apply. to_start builds a vdm::task::DownloadSpec from the row and
    calls EnginePort::start; to_resume -> resume(); to_pause ->
    pause() + writes the pause_reason; priority_order -> set_task_order
    over the mapped engine ids. `new` tasks are parked (startMode
    manual) and skipped.
  * on_engine_state(wire_id, state, err): projects an engine
    transition onto the store row (state, pause_reason='auto' when an
    error rides a paused transition per ADR 0013 §2, flattened error
    columns) so the next tick sees ground truth. This is also the hook
    event.task.state will fire from (D5).
  * reconcile_after_restart(): CORE-owned states -> queued, paused
    keeps its reason (ADR 0013 §5).
  * reload_config(): reads connection.maxConcurrentDownloads /
    maxActiveSegments + a daemon-local host-cap map, pushes caps to
    the engine, updates the governor.
  * Deps: injectable local-now clock and a post_to_loop marshaller
    (engine callbacks arrive on engine threads; default runs inline
    for tests).

Test veloxd.sched_scheduler (ASan+UBSan and TSan clean): admission +
ordering, a slot freeing on completion, queue-stop -> pause
(queue_stopped) then queue-restart -> resume (not a fresh start),
engine auto-pause -> pause_reason 'auto' + never auto-resumed,
reconcile_after_restart, reload_config caps push. 35 daemon/cli tests
green.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Upd9WhG9oppieig5nRDLig
2026-09-10 20:29:37 +04:00

192 lines
8.0 KiB
C++

// Scheduler against a FakeEnginePort and an in-memory store: admission, priority order,
// queue stop, restart reconciliation, and the engine-state -> store projection.
#include <string>
#include "check.hpp"
#include "sched/fake_engine_port.hpp"
#include "sched/governor.hpp"
#include "sched/scheduler.hpp"
#include "store/migrations.hpp"
#include "store/settings.hpp"
#include "store/sqlite.hpp"
#include "store/tasks.hpp"
using namespace velox::daemon;
using sched::FakeEnginePort;
using sched::Governor;
using sched::GovernorConfig;
using sched::Scheduler;
namespace {
store::TaskRow task(std::string id, std::string state, std::string created,
std::optional<std::string> queue = std::nullopt, std::int64_t pos = 0) {
store::TaskRow r;
r.task_id = std::move(id);
r.url = "https://cdn.example/" + r.task_id;
r.save_dir = "/tmp";
r.filename = r.task_id + ".bin";
r.state = std::move(state);
r.created_at = std::move(created);
r.queue_id = std::move(queue);
if (r.queue_id) r.queue_position = pos;
return r;
}
std::string task_state(store::Db& db, const std::string& id) {
store::Tasks t(db);
auto g = t.get(id);
return (g && *g) ? (*g)->state : std::string("<none>");
}
} // namespace
void run() {
auto db = store::Db::open(":memory:");
CHECK(db.has_value());
if (!db) return;
CHECK(store::migrate_to_head(*db).has_value());
store::Tasks tasks(*db);
// --- admission: 4 queued, cap 2 -> start the 2 oldest, in order -----------------
{
FakeEnginePort engine;
Scheduler sched(*db, engine,
Governor(GovernorConfig{.max_concurrent_downloads = 2,
.max_active_segments = 32}));
for (int i = 0; i < 4; ++i)
CHECK(tasks.insert(task("a" + std::to_string(i), "queued",
"2026-09-10T10:0" + std::to_string(i) + ":00Z"))
.has_value());
CHECK(sched.tick().has_value());
CHECK_EQ(engine.starts.size(), 2u);
CHECK_EQ(engine.starts[0].url, std::string("https://cdn.example/a0"));
CHECK_EQ(engine.starts[1].url, std::string("https://cdn.example/a1"));
CHECK_EQ(engine.starts[0].save_path, std::string("/tmp/a0.bin"));
CHECK_EQ(task_state(*db, "a0"), std::string("probing"));
CHECK_EQ(task_state(*db, "a2"), std::string("queued")); // not admitted
// set_task_order carries exactly the started tasks, oldest first.
CHECK_EQ(engine.last_order().size(), 2u);
CHECK(engine.last_order()[0] == engine.starts[0].id);
// A second tick with no free slots starts nothing new.
CHECK(sched.tick().has_value());
CHECK_EQ(engine.starts.size(), 2u);
}
// --- engine reports downloading, then one completes -> a slot frees ------------
{
for (const char* id : {"a0", "a1", "a2", "a3"}) tasks.remove(id);
FakeEnginePort engine;
Scheduler sched(*db, engine,
Governor(GovernorConfig{.max_concurrent_downloads = 1,
.max_active_segments = 32}));
CHECK(tasks.insert(task("b0", "queued", "2026-09-10T10:00:00Z")).has_value());
CHECK(tasks.insert(task("b1", "queued", "2026-09-10T10:01:00Z")).has_value());
CHECK(sched.tick().has_value());
CHECK_EQ(engine.starts.size(), 1u); // b0 only
CHECK_EQ(task_state(*db, "b0"), std::string("probing"));
sched.on_engine_state("b0", "downloading", std::nullopt);
CHECK_EQ(task_state(*db, "b0"), std::string("downloading"));
sched.on_engine_state("b0", "complete", std::nullopt);
CHECK_EQ(task_state(*db, "b0"), std::string("complete"));
CHECK(sched.tick().has_value()); // b0 terminal -> b1 admitted
CHECK_EQ(engine.starts.size(), 2u);
CHECK_EQ(engine.starts[1].url, std::string("https://cdn.example/b1"));
}
// --- a stopped queue: running tasks get paused with reason queue_stopped -------
{
for (const char* id : {"b0", "b1"}) tasks.remove(id);
CHECK(db->exec("UPDATE queues SET state='running' WHERE queue_id='main'").has_value());
FakeEnginePort engine;
Scheduler sched(*db, engine,
Governor(GovernorConfig{.max_concurrent_downloads = 10,
.max_active_segments = 32}));
CHECK(tasks.insert(task("q0", "queued", "2026-09-10T10:00:00Z", "main", 0)).has_value());
CHECK(sched.tick().has_value());
CHECK_EQ(engine.starts.size(), 1u);
sched.on_engine_state("q0", "downloading", std::nullopt);
CHECK(db->exec("UPDATE queues SET state='stopped' WHERE queue_id='main'").has_value());
CHECK(sched.tick().has_value());
CHECK_EQ(engine.paused.size(), 1u);
CHECK_EQ(task_state(*db, "q0"), std::string("paused"));
store::Tasks t(*db);
CHECK_EQ(t.get("q0").value().value().pause_reason.value_or(""), std::string("queue_stopped"));
// Restart the queue -> the task resumes (not a fresh start).
CHECK(db->exec("UPDATE queues SET state='running' WHERE queue_id='main'").has_value());
CHECK(sched.tick().has_value());
CHECK_EQ(engine.resumed.size(), 1u);
CHECK_EQ(engine.starts.size(), 1u); // no new start
}
// --- an engine auto-pause (error present) -> pause_reason 'auto', not touched --
{
for (const char* id : {"q0"}) tasks.remove(id);
FakeEnginePort engine;
Scheduler sched(*db, engine,
Governor(GovernorConfig{.max_concurrent_downloads = 10,
.max_active_segments = 32}));
CHECK(tasks.insert(task("auth", "queued", "2026-09-10T10:00:00Z")).has_value());
CHECK(sched.tick().has_value());
sched::TaskErrorFields ef;
ef.code = "auth_required";
ef.message = "401";
ef.http_status = 401;
sched.on_engine_state("auth", "paused", ef);
store::Tasks t(*db);
auto row = t.get("auth").value().value();
CHECK_EQ(row.state, std::string("paused"));
CHECK_EQ(row.pause_reason.value_or(""), std::string("auto"));
CHECK_EQ(row.error_code.value_or(""), std::string("auth_required"));
// A tick must NOT resume an auto-paused task.
engine.resumed.clear();
CHECK(sched.tick().has_value());
CHECK_EQ(engine.resumed.size(), 0u);
}
// --- reconcile_after_restart: CORE-owned states -> queued --------------------
{
for (const char* id : {"auth"}) tasks.remove(id);
CHECK(tasks.insert(task("r0", "downloading", "2026-09-10T10:00:00Z")).has_value());
CHECK(tasks.insert(task("r1", "verifying", "2026-09-10T10:01:00Z")).has_value());
CHECK(tasks.insert(task("r2", "paused", "2026-09-10T10:02:00Z")).has_value());
CHECK(db->exec("UPDATE tasks SET pause_reason='user' WHERE task_id='r2'").has_value());
FakeEnginePort engine;
Scheduler sched(*db, engine, Governor(GovernorConfig{}));
CHECK(sched.reconcile_after_restart().has_value());
CHECK_EQ(task_state(*db, "r0"), std::string("queued"));
CHECK_EQ(task_state(*db, "r1"), std::string("queued"));
CHECK_EQ(task_state(*db, "r2"), std::string("paused")); // paused survives
store::Tasks t(*db);
CHECK_EQ(t.get("r2").value().value().pause_reason.value_or(""), std::string("user"));
}
// --- reload_config pushes the caps to the engine ---------------------------
{
store::Settings settings(*db);
CHECK(settings.set_raw("connection.maxActiveSegments", "12").has_value());
FakeEnginePort engine;
Scheduler sched(*db, engine, Governor(GovernorConfig{}));
CHECK(sched.reload_config().has_value());
CHECK_EQ(engine.max_active_segments.size(), 1u);
CHECK_EQ(engine.max_active_segments.back(), 12u);
CHECK_EQ(sched.reload_config().has_value() ? 0 : 1, 0);
}
}
TEST_MAIN()