Skip to content

test(database): database tests take locks and move expiries instead of sleeping - #692

Open
PiquelChips wants to merge 2 commits into
test/lock-waits-one-helperfrom
test/database-waits
Open

PiquelChips wants to merge 2 commits into
test/lock-waits-one-helperfrom
test/database-waits

Conversation

@PiquelChips

@PiquelChips PiquelChips commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Five database tests slept so that a lease, a code or a recovery link would expire, or polled until an advisory lock came free. Under load those waits are either too short, which makes the test flaky, or too long, which makes it slow. Each one now acts on the event itself. Expiries are ended in SQL at the exact point the test needs, and a test that needs a lock waits for PostgreSQL to grant it.

Stacked on #691.

Changes

  • New Database::exclusive(key) (test-util): takes key's advisory lock with pg_advisory_lock and waits its turn, where try_exclusive gives up at once. Later PRs in the stack use it for the audit and cleanup locks.
  • exclusive waits up to 30 seconds for the lock, the old polling helpers' budget, instead of failing at the pool's 5-second lock_timeout. It and run_migrations take their session lock through one lock_dedicated method, which lifts the lock limit for the wait and restores it after.
  • New expire_lease helper in the delivery repository's tests: sets leased_until = clock_timestamp().

Tests

  • locks_exclude_other_sessions_until_their_connection_ends: instead of polling try_exclusive, it waits on pg_advisory_lock, which PostgreSQL grants once the dropped holder's session ends. If the session never ends, the 5s limit fails the test as before. Checked: with the holder leaked (mem::forget), the test fails at that limit.
  • an_expired_lease_is_claimed_again_and_the_stale_outcome_discarded: the claim takes a full lease and the test then expires it, where it used to claim a 1ms lease and sleep 20ms. Checked: without the expiry, the test fails at "the expired lease is claimed again".
  • operators_retry_and_cancel_deliveries: the test still checks that the cancelled attempt's lease holds, then expires it, where it used to sleep 300ms. Checked: without the expiry, the retried claim fails.
  • removal_expires_codes_an_earlier_exchange_still_sees_live: the code now expires (clock_timestamp()) after the exchange's transaction begins and before the removal's does. That is the same ordering the 1.2s sleep produced. Checked: with expire_codes limited to codes its own now() sees as live, the test fails.
  • a_link_that_expires_while_finishing_does_not_complete: the link is expired (Owner::expire) after the finishing transaction has locked the account, instead of a 700ms sleep on a 500ms link. Checked: with complete comparing against now() instead of clock_timestamp(), the test fails.

I ran all 5 five times against a scratch cluster, and they passed every time.

Council review

Rounds: 3

Delegate Round 1 Round 2 Round 3
GPT 6.1 Sol no score no score no score
Claude Opus 5.5 8/10 9/10 9/10
Claude Fable 5.2 8/10 8/10 9/10

GPT 6.1 Sol could not take part: its account hit its usage limit.

Note

Written by Claude Opus 5.5 in Claude Code and reviewed by Claude Opus 5.5 and Claude Fable 5.2 (via T3 Code)

@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bbed8b24-00b2-47a5-b6a9-c6f6f277d4e3

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

…5-second lock timeout

exclusive and run_migrations now take their session lock through one
lock_dedicated method, which lifts the lock limit for the wait and
restores it after.
@PiquelChips
PiquelChips marked this pull request as ready for review November 22, 2026 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

1 participant