144 lines
4.6 KiB
Rust
144 lines
4.6 KiB
Rust
//! Concurrent applies with the same scoped idempotency key: one coherent
|
|
//! commit, deterministic conflicts for changed digests.
|
|
|
|
use super::*;
|
|
|
|
#[tokio::test]
|
|
async fn concurrent_applies_with_same_key_and_digest_converge() {
|
|
let database = TestDatabase::new().await;
|
|
let registry = database.registry().await;
|
|
let raw_pool = database.raw_pool().await;
|
|
let now = OffsetDateTime::now_utc();
|
|
let ws = create_workspace(®istry, "ws_apply_race").await;
|
|
let job = create_pending_openapi_job(
|
|
®istry,
|
|
&ws,
|
|
"job_apply_race",
|
|
now,
|
|
now + Duration::hours(1),
|
|
)
|
|
.await;
|
|
activate_source(&raw_pool, &ws, now).await;
|
|
|
|
let fp = fingerprint();
|
|
let (plan, digest) = stored_plan(®istry, &ws, &job, "plan_race", &fp).await;
|
|
let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap();
|
|
let request = ApplyImportPlanRequest {
|
|
workspace_id: &ws,
|
|
plan_id: &plan,
|
|
plan_digest: &digest,
|
|
idempotency_key: "idem_race",
|
|
verified_fingerprint: &fp,
|
|
applied_at: &applied_at,
|
|
};
|
|
|
|
// Two identical applies race: both must converge on the same report and
|
|
// exactly one operation / application row may exist.
|
|
let (a, b) = tokio::join!(
|
|
registry.apply_import_plan(&request),
|
|
registry.apply_import_plan(&request)
|
|
);
|
|
let a = a.unwrap();
|
|
let b = b.unwrap();
|
|
assert_eq!(a.report, b.report, "racers must see the same report");
|
|
let fresh = (a.replayed, b.replayed);
|
|
assert!(
|
|
(fresh == (false, true)) || (fresh == (true, false)),
|
|
"exactly one racer produces the report, the other replays: {a:?} / {b:?}"
|
|
);
|
|
assert_eq!(
|
|
sqlx::query_scalar::<_, i64>("select count(*) from operations")
|
|
.fetch_one(&raw_pool)
|
|
.await
|
|
.unwrap(),
|
|
1,
|
|
"the race must materialize exactly one operation"
|
|
);
|
|
assert_eq!(
|
|
sqlx::query_scalar::<_, i64>("select count(*) from import_plan_applications")
|
|
.fetch_one(&raw_pool)
|
|
.await
|
|
.unwrap(),
|
|
1,
|
|
"the race must persist exactly one application row"
|
|
);
|
|
database.cleanup().await;
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn concurrent_applies_same_key_changed_digest_conflicts_deterministically() {
|
|
let database = TestDatabase::new().await;
|
|
let registry = database.registry().await;
|
|
let raw_pool = database.raw_pool().await;
|
|
let now = OffsetDateTime::now_utc();
|
|
let ws = create_workspace(®istry, "ws_apply_race2").await;
|
|
let job = create_pending_openapi_job(
|
|
®istry,
|
|
&ws,
|
|
"job_apply_race2",
|
|
now,
|
|
now + Duration::hours(1),
|
|
)
|
|
.await;
|
|
activate_source(&raw_pool, &ws, now).await;
|
|
|
|
let fp = fingerprint();
|
|
let (plan, digest) = stored_plan(®istry, &ws, &job, "plan_race2", &fp).await;
|
|
let applied_at = OffsetDateTime::from_unix_timestamp(1_800_000_000).unwrap();
|
|
let good = ApplyImportPlanRequest {
|
|
workspace_id: &ws,
|
|
plan_id: &plan,
|
|
plan_digest: &digest,
|
|
idempotency_key: "idem_race2",
|
|
verified_fingerprint: &fp,
|
|
applied_at: &applied_at,
|
|
};
|
|
let other_digest = "ef".repeat(32);
|
|
let bad = ApplyImportPlanRequest {
|
|
plan_digest: &other_digest,
|
|
..good
|
|
};
|
|
|
|
// Same key, different plan digest: exactly one may succeed.
|
|
let (a, b) = tokio::join!(
|
|
registry.apply_import_plan(&good),
|
|
registry.apply_import_plan(&bad)
|
|
);
|
|
assert!(
|
|
a.is_ok() ^ b.is_ok(),
|
|
"one winner, one typed conflict: a={a:?}, b={b:?}"
|
|
);
|
|
for result in [&a, &b] {
|
|
if let Err(error) = result {
|
|
assert!(
|
|
matches!(error, RegistryError::ImportPlanKeyDigestConflict { .. }),
|
|
"the loser must get the typed digest conflict, got {error:?}"
|
|
);
|
|
}
|
|
}
|
|
// The committed state is coherent: exactly one application row, and it
|
|
// binds the digest of the winner's successful apply.
|
|
let winner = match &a {
|
|
Ok(result) => result,
|
|
Err(_) => b.as_ref().unwrap(),
|
|
};
|
|
assert_eq!(
|
|
sqlx::query_scalar::<_, i64>("select count(*) from import_plan_applications")
|
|
.fetch_one(&raw_pool)
|
|
.await
|
|
.unwrap(),
|
|
1
|
|
);
|
|
assert!(
|
|
!winner.replayed,
|
|
"the winner of a changed-digest race must be a fresh apply"
|
|
);
|
|
let stored = registry
|
|
.get_import_plan_application(&ws, "idem_race2")
|
|
.await
|
|
.unwrap()
|
|
.expect("the committed apply must be readable");
|
|
assert_eq!(stored.plan_digest, winner.report.plan_digest);
|
|
database.cleanup().await;
|
|
}
|