summaryrefslogtreecommitdiffstats
path: root/tests/state_machine.rs
diff options
context:
space:
mode:
authoraur-build <aur-build@localhost>2026-11-18 16:31:58 +0000
committeraur-build <aur-build@localhost>2026-11-18 16:31:58 +0000
commit101f077200e80c7869e4549db645ed94b01acb1b (patch)
treef8560d143ef2afe1a3d675a203c693a09cbc451b /tests/state_machine.rs
parentf8370b48f3e4464a1454d0ff7af1819b5448c619 (diff)
downloadaur-build-101f077200e80c7869e4549db645ed94b01acb1b.tar.gz
aur-build-101f077200e80c7869e4549db645ed94b01acb1b.zip
db: the build state machine is an enum, and a state write is a compare-and-swap
`build_state` was seven `&str` constants and `set_state(&str)` would write any string a caller invented, so the machine existed only as `WHERE state IN (...)` clauses scattered across six modules and nothing could say that `succeeded → running` is nonsense. It is `db::model::BuildState` now: parsed where a row is read (a state the code cannot name is an error, not an empty string — which also retires m-12's `current_state` returning `""`), and `set_state` takes the variant and writes only if the row's current state is one of `to`'s legal predecessors. A refused transition writes nothing and says why. Writing the machine down surfaced three things that were true of the flows but of nobody's documentation. The step loop re-asserts `Running` at every step boundary, so a self-write is a no-op and not a move — every state accepts its own name. A failed build is resumed *on its own row* (I5: the step checkpoints say where to continue), so `failed → running` is a real transition. And `builds::retry` — the UI's requeue button — moved any row to `queued` unconditionally, including one that had already published, re-queueing a build whose every step was checkpointed as done; it goes through the machine now and is refused with a reason. `requeue_transient` likewise. What the CAS buys is the zombie case, which no test covered: a worker's lease expires mid-step, the reaper returns the build to the queue, and the original worker — still running — comes back and writes `succeeded` over whatever the new owner has since done. That write now fails loudly and the queue keeps ownership. `builds_for_pkgbase` had a second, hand-copied row mapper that dropped unreadable rows with `filter_map(|r| r.ok())`; it shares `row_to_build` now and a bad row fails the query instead of vanishing from the package page (m-9's policy, applied where it bit). `tests/state_machine.rs` pins the whole table, the zombie refusal, the unknown state, the terminal build that does not move, and the requeue-of-a-published- build refusal. `tests/live.rs` walks a build through `running` before succeeding instead of teleporting it.
Diffstat (limited to 'tests/state_machine.rs')
-rw-r--r--tests/state_machine.rs170
1 files changed, 170 insertions, 0 deletions
diff --git a/tests/state_machine.rs b/tests/state_machine.rs
new file mode 100644
index 0000000..c5203d7
--- /dev/null
+++ b/tests/state_machine.rs
@@ -0,0 +1,170 @@
+//! The build state machine (§6 M-8).
+//!
+//! Before this was an enum, `set_state(&str)` wrote any string a caller
+//! invented and the machine lived only in `WHERE state IN (...)` clauses. These
+//! tests are the ones that would have failed then.
+
+mod common;
+
+use aur_build::db::{builds, model::BuildState};
+use common::Env;
+
+fn build(env: &Env, id: &str) -> String {
+ env.exec(
+ "INSERT INTO pkgbase (name, aur_url, created_at) VALUES ('x', 'u', '2026-01-01T00:00:00Z')",
+ &[],
+ );
+ let pb = env.query_i64("SELECT id FROM pkgbase WHERE name='x'", &[]).expect("pkgbase id");
+ builds::enqueue(&env.db, id, pb, None, "manual", None, "default", 0).unwrap();
+ id.to_string()
+}
+
+fn state(env: &Env, id: &str) -> BuildState {
+ builds::get(&env.db, id).unwrap().expect("build").state
+}
+
+#[test]
+fn every_state_round_trips_through_its_column_text() {
+ for s in BuildState::ALL {
+ assert_eq!(BuildState::parse(s.as_str()), Some(*s), "{s:?} does not round-trip");
+ }
+ assert_eq!(BuildState::parse("exploded"), None);
+ assert_eq!(BuildState::parse(""), None);
+}
+
+#[test]
+fn the_machine_refuses_a_transition_it_does_not_define() {
+ let env = Env::new();
+ let id = build(&env, "b1");
+ assert_eq!(state(&env, "b1"), BuildState::Queued);
+
+ // Queued → Succeeded: a build cannot finish without running. Before the
+ // enum this wrote happily and produced a succeeded build that never built.
+ let err = builds::set_state(&env.db, &id, BuildState::Succeeded, None).unwrap_err();
+ assert!(err.to_string().contains("illegal build transition"), "{err}");
+ assert_eq!(state(&env, "b1"), BuildState::Queued, "a refused transition writes nothing");
+
+ // And a build that does not exist is named as such, not silently updated.
+ let err = builds::set_state(&env.db, "nope", BuildState::Cancelled, None).unwrap_err();
+ assert!(err.to_string().contains("no build nope"), "{err}");
+}
+
+#[test]
+fn a_terminal_build_is_history_and_does_not_move() {
+ let env = Env::new();
+ let id = build(&env, "b1");
+ builds::claim(&env.db, "worker-1", &id, 90).unwrap();
+ builds::set_state(&env.db, &id, BuildState::Running, None).unwrap();
+ builds::set_state(&env.db, &id, BuildState::Succeeded, None).unwrap();
+ assert_eq!(state(&env, "b1"), BuildState::Succeeded);
+
+ for to in [BuildState::Running, BuildState::Queued, BuildState::Failed] {
+ assert!(
+ !BuildState::Succeeded.can_transition(to),
+ "succeeded must not transition to {to:?}"
+ );
+ assert!(builds::set_state(&env.db, &id, to, None).is_err(), "accepted {to:?} after success");
+ }
+ assert_eq!(state(&env, "b1"), BuildState::Succeeded);
+ // Rows are never deleted (§15); a finished build stays what it was.
+ assert_eq!(env.query_i64("SELECT count(*) FROM build", &[]), Some(1));
+}
+
+#[test]
+fn a_zombie_worker_cannot_finish_a_build_it_no_longer_owns() {
+ // The failure this pins: a worker's lease expires mid-step, the reaper hands
+ // the build back to the queue, and the original worker — still running —
+ // comes back and writes `succeeded` over whatever the new owner did. The
+ // compare-and-swap in `set_state` is what makes that an error.
+ let env = Env::new();
+ let id = build(&env, "b1");
+ builds::claim(&env.db, "worker-1", &id, 90).unwrap();
+ builds::set_state(&env.db, &id, BuildState::Running, None).unwrap();
+
+ env.exec("UPDATE build SET lease_expires_at='2000-01-01T00:00:00Z' WHERE id=?1", &[common::P::S(id.clone())]);
+ assert_eq!(builds::reap_expired(&env.db).unwrap(), 1);
+ assert_eq!(state(&env, &id), BuildState::Queued);
+
+ let err = builds::set_state(&env.db, &id, BuildState::Succeeded, None).unwrap_err();
+ assert!(err.to_string().contains("illegal build transition"), "the zombie was allowed to write: {err}");
+ assert_eq!(state(&env, &id), BuildState::Queued, "the queue still owns this build");
+}
+
+#[test]
+fn a_state_the_code_does_not_know_is_an_error_not_an_empty_string() {
+ // A row written by an older binary, a hand-edit, or a future migration the
+ // code has not caught up with must stop the process, not render as a build
+ // in no state at all.
+ let env = Env::new();
+ let id = build(&env, "b1");
+ env.exec("UPDATE build SET state='exploded' WHERE id=?1", &[common::P::S(id.clone())]);
+ let err = builds::get(&env.db, &id).unwrap_err();
+ assert!(err.to_string().contains("outside the state machine"), "{err}");
+ // The package page lists a pkgbase's builds with no state filter: an
+ // unreadable row must fail the query, not vanish from the list.
+ let pb = env.query_i64("SELECT id FROM pkgbase WHERE name='x'", &[]).unwrap();
+ assert!(aur_build::db::builds_for_pkgbase(&env.db, pb, 10).is_err());
+}
+
+#[test]
+fn the_transition_table_is_the_machine_the_code_actually_uses() {
+ // Every state may be re-written with its own name — that is a no-op, not a
+ // move (the step loop re-asserts Running at each boundary). What matters is
+ // what else is reachable, and this table is the whole machine.
+ let reachable = |from: BuildState| -> Vec<&'static str> {
+ BuildState::ALL
+ .iter()
+ .filter(|to| from.can_transition(**to))
+ .map(|to| to.as_str())
+ .collect()
+ };
+ assert_eq!(reachable(BuildState::Queued), ["queued", "leased", "running", "blocked_missing_dep", "cancelled"]);
+ assert_eq!(
+ reachable(BuildState::Leased),
+ ["queued", "leased", "running", "blocked_missing_dep", "succeeded", "failed", "cancelled"]
+ );
+ assert_eq!(
+ reachable(BuildState::Running),
+ ["queued", "running", "blocked_missing_dep", "succeeded", "failed", "cancelled"]
+ );
+ assert_eq!(reachable(BuildState::BlockedMissingDep), ["queued", "blocked_missing_dep", "cancelled"]);
+ // A failed build is resumed on its own row (I5) or requeued by a human (§8.4).
+ assert_eq!(reachable(BuildState::Failed), ["queued", "running", "failed"]);
+ assert_eq!(reachable(BuildState::Cancelled), ["queued", "cancelled"]);
+ // A published build is history. Nothing revives it; a new version is a new
+ // build id (§8.1), not a resurrection.
+ assert_eq!(reachable(BuildState::Succeeded), ["succeeded"]);
+}
+
+#[test]
+fn requeueing_a_succeeded_build_is_refused_not_silently_requeued() {
+ // `builds::retry` resets the failed steps and moves the row back to the
+ // queue. On a build that already published there is nothing to retry, and
+ // the old unconditional UPDATE happily requeued it anyway.
+ let env = Env::new();
+ let id = build(&env, "b1");
+ builds::claim(&env.db, "worker-1", &id, 90).unwrap();
+ builds::set_state(&env.db, &id, BuildState::Running, None).unwrap();
+ builds::set_state(&env.db, &id, BuildState::Succeeded, None).unwrap();
+ let err = builds::retry(&env.db, &id).unwrap_err();
+ assert!(err.to_string().contains("illegal build transition succeeded"), "{err}");
+ assert_eq!(state(&env, "b1"), BuildState::Succeeded);
+}
+
+#[test]
+fn a_failed_build_is_resumed_on_its_own_row() {
+ // I5: the resume happens on the same row, because the step checkpoints say
+ // where to continue. The machine has to know that, or the resume is a bug.
+ let env = Env::new();
+ let id = build(&env, "b1");
+ builds::claim(&env.db, "worker-1", &id, 90).unwrap();
+ builds::set_state(&env.db, &id, BuildState::Running, None).unwrap();
+ builds::set_state(&env.db, &id, BuildState::Failed, Some("publish: injected")).unwrap();
+ builds::retry(&env.db, &id).unwrap();
+ assert_eq!(state(&env, &id), BuildState::Queued);
+ builds::set_state(&env.db, &id, BuildState::Running, None).unwrap();
+ assert_eq!(state(&env, &id), BuildState::Running);
+ builds::set_state(&env.db, &id, BuildState::Succeeded, None).unwrap();
+ assert_eq!(state(&env, &id), BuildState::Succeeded);
+}
+