summaryrefslogtreecommitdiffstats
path: root/tests/state_machine.rs
diff options
context:
space:
mode:
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);
+}
+