diff options
| author | aur-build <aur-build@localhost> | 2026-11-18 16:49:40 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-18 16:49:40 +0000 |
| commit | 41fa1684de8eb13c5b76c3a3e174fece04ac5afa (patch) | |
| tree | 491a2faf2dd2314f537300ccaf1e0150b54d1123 /tests/state_machine.rs | |
| parent | 101f077200e80c7869e4549db645ed94b01acb1b (diff) | |
| download | aur-build-41fa1684de8eb13c5b76c3a3e174fece04ac5afa.tar.gz aur-build-41fa1684de8eb13c5b76c3a3e174fece04ac5afa.zip | |
db: the review, step, release and subscription states are enums too
M-8 named four more machines after `build_state`, and each had the same defect
in miniature: `&str` constants, a writer that took any `&str`, and a rule that
lived only in the caller's discipline. They are `CandidateState`, `StepState`,
`ReleaseState` and `SubscriptionState` now, all parsed through one
`db::state_col` at the row boundary — a column value the code cannot name fails
the read rather than becoming a state it did not ask for.
The one that mattered is the review machine. `resolve_in(tx, id, &str)` would
move a candidate to any state from any state, so an approval racing a
rejection, or a stale worker resolving a review that had already been
superseded, silently overwrote what a human had decided — the direct route to
breaking I2 and I3 with no symptom anywhere. It is a compare-and-swap on
`CandidateState` now: a decided review is only ever frozen, never re-decided,
and a refusal names both states. `supersede_in` goes through it, so the freeze
cannot resurrect a superseded review either.
`StepState` gives I5 teeth. `begin_step` upserted over any existing checkpoint,
including one recorded `succeeded`, so a resume that asked to re-run a finished
step could rewrite the very row that told it to skip; the upsert now refuses to
touch a done checkpoint and `finish_step` is a CAS. The blocked path
(`running → pending`) and the reaper's `running → interrupted` are unchanged,
and both are in the table.
Two behaviour changes, both for the better and both tested: the UI's pause
button now says "orphaned: nothing to pause" instead of rewriting an orphaned
subscription back to a pausable state, and a step checkpoint that is already
done makes `begin_step` an error rather than a quiet overwrite.
Five tests in `tests/state_machine.rs` cover the review CAS in both directions,
the frozen checkpoint, the interrupted/blocked transitions, and the unknown
value in a candidate and a step row.
Diffstat (limited to 'tests/state_machine.rs')
| -rw-r--r-- | tests/state_machine.rs | 117 |
1 files changed, 115 insertions, 2 deletions
diff --git a/tests/state_machine.rs b/tests/state_machine.rs index c5203d7..1c05fff 100644 --- a/tests/state_machine.rs +++ b/tests/state_machine.rs @@ -6,12 +6,12 @@ mod common; -use aur_build::db::{builds, model::BuildState}; +use aur_build::db::{builds, model::{BuildState, CandidateState, StepState}}; 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')", + "INSERT OR IGNORE 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"); @@ -168,3 +168,116 @@ fn a_failed_build_is_resumed_on_its_own_row() { assert_eq!(state(&env, &id), BuildState::Succeeded); } + +// ---------------------------------------------------------- the other machines -- + +/// A candidate row in `state`, inserted directly: these tests are about the +/// state column, not about how a candidate is discovered. +fn candidate(env: &Env, state: &str) -> i64 { + env.exec( + "INSERT OR IGNORE 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'", &[]).unwrap(); + let snap = aur_build::db::put_snapshot(&env.db.writer, pb, "commit", "rh", "{}", "[]").unwrap(); + env.exec( + "INSERT INTO candidate (pkgbase_id, kind, snapshot_id, pkgver, pkgrel, identity_hash, state, detected_at) + VALUES (?1,'new_version',?2,'1.0','1','ih-1',?3,'2026-01-01T00:00:00Z')", + &[common::P::I(pb), common::P::I(snap), common::P::S(state.into())], + ); + env.query_i64("SELECT id FROM candidate WHERE identity_hash='ih-1'", &[]).unwrap() +} + +fn resolve(env: &Env, id: i64, to: CandidateState) -> aur_build::error::Result<()> { + env.db + .writer + .tx_for_tests(move |tx| aur_build::db::candidates::resolve_in(tx, id, to, "2026-01-01T00:00:00Z")) +} + +fn candidate_state(env: &Env, id: i64) -> CandidateState { + aur_build::db::candidates::by_id(&env.db, id) + .unwrap() + .expect("candidate") + .state +} + +#[test] +fn a_decided_review_is_never_re_decided() { + // The one write in this system that must never overwrite a human's decision. + let env = Env::new(); + let id = candidate(&env, "approved"); + let err = resolve(&env, id, CandidateState::Rejected).unwrap_err(); + assert!(err.to_string().contains("illegal candidate transition approved"), "{err}"); + assert_eq!(candidate_state(&env, id), CandidateState::Approved); + + // Superseding a decided review is the one move it makes (§7.1), and a + // superseded review is history. + resolve(&env, id, CandidateState::Superseded).unwrap(); + assert_eq!(candidate_state(&env, id), CandidateState::Superseded); + assert!(resolve(&env, id, CandidateState::Approved).is_err()); +} + +#[test] +fn only_an_open_review_can_be_approved() { + let env = Env::new(); + let id = candidate(&env, "rejected"); + assert!(resolve(&env, id, CandidateState::Approved).is_err(), "a rejection was re-decided"); + assert_eq!(candidate_state(&env, id), CandidateState::Rejected); + // Re-writing the state it already has is a no-op, not a re-decision. + resolve(&env, id, CandidateState::Rejected).unwrap(); +} + +#[test] +fn a_completed_step_checkpoint_is_frozen() { + // I5: a resumed build reads these rows to decide what not to redo. A + // checkpoint that can be rewritten is not a checkpoint. + let env = Env::new(); + let id = build(&env, "b1"); + builds::begin_step(&env.db, &id, "build", 1).unwrap(); + builds::finish_step(&env.db, &id, "build", StepState::Succeeded, None, None).unwrap(); + + let err = builds::begin_step(&env.db, &id, "build", 2).unwrap_err(); + assert!(err.to_string().contains("checkpointed as done"), "{err}"); + assert!(builds::finish_step(&env.db, &id, "build", StepState::Failed, None, Some("x")).is_err()); + assert_eq!(builds::step_status(&env.db, &id, "build").unwrap(), Some(StepState::Succeeded)); +} + +#[test] +fn an_interrupted_step_resumes_and_a_blocked_step_waits() { + let env = Env::new(); + let id = build(&env, "b1"); + builds::begin_step(&env.db, &id, "build", 1).unwrap(); + // The reaper's write, then the resume's re-begin. + env.exec( + "UPDATE build_step SET status='interrupted' WHERE build_id=?1 AND step='build'", + &[common::P::S(id.clone())], + ); + assert_eq!(builds::step_status(&env.db, &id, "build").unwrap(), Some(StepState::Interrupted)); + builds::begin_step(&env.db, &id, "build", 2).unwrap(); + // The blocked path puts the checkpoint back to waiting rather than failing it. + builds::finish_step(&env.db, &id, "build", StepState::Pending, None, Some("blocked")).unwrap(); + assert_eq!(builds::step_status(&env.db, &id, "build").unwrap(), Some(StepState::Pending)); +} + +#[test] +fn an_unnamed_state_in_any_row_is_an_error() { + // Every state column is parsed through `db::state_col`, so a value the code + // does not know stops the read instead of rendering as some other state. + let env = Env::new(); + let cid = candidate(&env, "approved"); + env.exec( + "UPDATE candidate SET state='reconsidered' WHERE id=?1", + &[common::P::I(cid)], + ); + let err = aur_build::db::candidates::by_id(&env.db, cid).unwrap_err(); + assert!(err.to_string().contains("candidate has a state outside"), "{err}"); + + let id = build(&env, "b1"); + builds::begin_step(&env.db, &id, "build", 1).unwrap(); + env.exec( + "UPDATE build_step SET status='maybe' WHERE build_id=?1 AND step='build'", + &[common::P::S(id.clone())], + ); + let err = builds::step_status(&env.db, &id, "build").unwrap_err(); + assert!(err.to_string().contains("build step has a state outside"), "{err}"); +} |
