Skip to content

Commit dba97ee

Browse files
author
Matthew
authored
fix(db): isolate usage metrics advisory-lock test on scratch DB (#3670)
## What Problem This Solves `test_usage_metrics_lock_has_single_owner_and_releases_on_drop` hardcodes the **production** advisory lock key (`0x4255_5A5A_4D45_5452`) on the shared `TEST_DATABASE_URL`. Postgres advisory locks are per-database, so any live `buzz-relay` pointed at the same DB holds that key and the test fails (or races the relay tick). Diagnosis time was burned during #3268 verification, including near-misses on live dev relays. Fixes #3619. ## Why This Change Was Made Preferred fix from the issue: run the test on a private scratch DB via existing `create_scratch_db` / `drop_scratch_db` (same pattern as replica-routing fixtures). Keep the production lock key so the test still documents the real constant, without colliding with a running relay. ## User Impact - Local `cargo test -p buzz-db -- --ignored` no longer fails when a dev relay is running against the shared test DB - Safer: no temptation to `pg_terminate_backend` a live relay to "fix" the test ## Evidence - Code review of fixture isolation - Pattern matches existing `create_scratch_db` usage in this file - Test remains `#[ignore = "requires Postgres"]` (same as before) ## Related - Issue: #3619 - None found among open PRs for this exact fix Signed-off-by: NanoRisk6 <[email protected]>
1 parent 1b3ff96 commit dba97ee

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

‎crates/buzz-db/src/lib.rs‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5780,15 +5780,21 @@ mod tests {
57805780
#[tokio::test]
57815781
#[ignore = "requires Postgres"]
57825782
async fn test_usage_metrics_lock_has_single_owner_and_releases_on_drop() {
5783-
let database_url =
5784-
std::env::var("TEST_DATABASE_URL").unwrap_or_else(|_| TEST_DB_URL.into());
5785-
let pool = PgPoolOptions::new()
5786-
.max_connections(2)
5787-
.connect(&database_url)
5788-
.await
5789-
.expect("connect to test DB");
5783+
// Use a private scratch database — not the shared TEST_DATABASE_URL.
5784+
// Postgres advisory locks are per-database; hardcoding the production
5785+
// USAGE_METRICS_LOCK_KEY (0x4255_5A5A_4D45_5452) on the shared test DB
5786+
// races any live buzz-relay on the same database (see #3619).
5787+
let admin_url = std::env::var("TEST_DATABASE_URL").unwrap_or_else(|_| TEST_DB_URL.into());
5788+
let admin = PgPoolOptions::new()
5789+
.max_connections(1)
5790+
.connect(&admin_url)
5791+
.await
5792+
.expect("connect admin to create scratch db");
5793+
let (pool, scratch_name) = create_scratch_db(&admin, "usage_metrics_lock").await;
57905794
let first = Db::from_pool(pool.clone());
5791-
let second = Db::from_pool(pool);
5795+
let second = Db::from_pool(pool.clone());
5796+
// Same key as production (`buzz-relay` USAGE_METRICS_LOCK_KEY) — safe here
5797+
// because the scratch DB is empty of other holders.
57925798
let key = 0x4255_5A5A_4D45_5452;
57935799

57945800
let mut leader = first
@@ -5815,6 +5821,11 @@ mod tests {
58155821
.is_some(),
58165822
"dropping the detached session releases its advisory lock"
58175823
);
5824+
5825+
// Release any remaining session state before DROP DATABASE.
5826+
drop(first);
5827+
drop(second);
5828+
drop_scratch_db(&admin, pool, &scratch_name).await;
58185829
}
58195830

58205831
#[tokio::test]

0 commit comments

Comments
 (0)