Skip to content

test(database): lock-wait tests share one helper that says why it polls - #691

Open
PiquelChips wants to merge 2 commits into
mainfrom
test/lock-waits-one-helper
Open

PiquelChips wants to merge 2 commits into
mainfrom
test/lock-waits-one-helper

Conversation

@PiquelChips

@PiquelChips PiquelChips commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Tests now wait for events, not time, so a test may only sleep where nothing can be awaited, and it has to say why. Two copies of until_blocked, one in auth-service and one in the database crate, polled pg_locks every 10ms and gave no reason. PostgreSQL really does offer no notification when a transaction starts waiting for a lock, so the poll has to stay. It now lives in one place and says why.

First of a stack that takes the sleeps out of sourceorigin's tests. cargo xtask test-waits, which will enforce the rule, comes in a later PR once it is published in a shared crate.

Changes

  • DatabaseTransaction::until_blocked(waiters) replaces both copies. It is a test-util fixture in crates/database/src/fixtures.rs with a // wall-clock: comment on its poll. blocked_transactions becomes pub(crate) since only this fixture reads it.
  • The database crate's own Database::wait_for_lock gets the same // wall-clock: comment.
  • Every caller changes from until_blocked(&mut holder, n) to holder.until_blocked(n), with no change in behaviour.
  • docs/dev/testing.md lists the fixture with the others.

Tests

The tests that call until_blocked assert the same things as before, through the same query and the same 10-second limit. I ran all 23 of them (auth-service and sourceorigin-repository) 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)

Scope: database test fixture (crates/database)

  • Adds DatabaseTransaction::until_blocked(&mut self, waiters: i64) in crates/database/src/fixtures.rs (test-util). It polls blocked_transactions() every 10 ms until at least waiters transactions are blocked.
  • The helper uses a 10-second timeout. Query failures and timeout expiry panic.
  • Updates fixture imports. Duration is now imported directly.
  • Changes DatabaseTransaction::blocked_transactions from pub to pub(crate) in crates/database/src/transaction.rs. The query and return type are unchanged. Its doc comment no longer describes ordering concurrent test work.
  • Adds a comment in Database::wait_for_lock (crates/database/src/connection/mod.rs). The comment states that lock-wait detection polls PostgreSQL catalog views because PostgreSQL emits no signal when a wait begins. Runtime behavior is unchanged.

Scope: database repository tests

  • crates/database/src/repositories/announcements/tests.rs: removes the local polling helper. Purge/send tests call DatabaseTransaction::until_blocked with the same wait counts.

Scope: auth-service test helper removal

  • apps/auth-service/src/tests.rs: removes the free function until_blocked(holder: &mut DatabaseTransaction<'_>, waiters: i64).
  • Removes the matching imports from the test modules that used it.

Scope: auth-service call sites

  • Replaces until_blocked(&mut tx, n) with tx.until_blocked(n) in test modules under account, admin, alerts, clients, oidc, passkey, recovery, security, and web/admin.
  • Blocked-task counts, synchronization points, and assertions are unchanged.

Scope: documentation

  • docs/dev/testing.md: describes until_blocked as waiting for transactions queued behind a lock held by another transaction. Also clarifies that test_channels! declares channels whose deliveries the test inserts.

Totals: 24 files, +76 / −70 lines. No runtime production behavior change is described. The planned cargo xtask test-waits check is not part of this change.

@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The database test fixture adds DatabaseTransaction::until_blocked, which polls for blocked transactions. Auth-service and announcement concurrency tests use this method instead of standalone helpers. The blocked-transaction query is now crate-visible.

Changes: Transaction lock-wait test synchronization

Layer / File(s) Summary
Shared transaction wait method
crates/database/src/fixtures.rs, crates/database/src/transaction.rs, crates/database/src/connection/mod.rs, docs/dev/testing.md
The database fixture adds DatabaseTransaction::until_blocked, which polls every 10 ms for up to 10 seconds. blocked_transactions becomes crate-visible. The documentation describes the wait method.
Concurrency test migration
apps/auth-service/src/**/tests*.rs, crates/database/src/repositories/announcements/tests.rs
Auth-service and announcement tests call until_blocked on their transaction instead of using standalone polling helpers. The existing wait counts and synchronization points remain unchanged.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Other

Possibly related issues

Merge Risk: Minimal · up to 9263a

This PR consolidates duplicated lock-wait test helpers into one shared fixture and documents why it polls. It has no production behavior impact, and no merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the test scope and the shared helper that now documents why it polls. The changeset consolidates two until_blocked helpers into DatabaseTransaction::until_blocked with a wall-clock com…
Description check Passed The description follows the template's main sections. It explains what changed and why, gives the rationale for keeping the PostgreSQL poll, and describes testing (23 tests run five times against a sc…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the test scope and the shared helper that now documents why it polls. The changeset consolidates two until_blocked helpers into DatabaseTransaction::until_blocked with a wall-clock comment, so the title matches the main change. It is somewhat dense but specific and conventional-commit formatted.

Full details: Description check

Explanation

The description follows the template's main sections. It explains what changed and why, gives the rationale for keeping the PostgreSQL poll, and describes testing (23 tests run five times against a scratch cluster). It has a Council review section and a note that is not in the template. The checklist is absent, and the description does not reference an issue number. Those gaps are non-critical for this check.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • 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

A rabbit watched the lock waits slow,
Then moved the polling where tests go.
Each transaction knows the chore,
No scattered helpers hop once more.
The tests wait, then onward flow.

Recent review info

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: 94ff6dce-039e-465e-9c37-1cc5f862e77d
Commits

Reviewing files that changed from the base of the PR and between e99cc0e and 9263ae4.

Files selected for processing: 24

  • apps/auth-service/src/account/tests.rs
  • apps/auth-service/src/admin/tests/administrators.rs
  • apps/auth-service/src/admin/tests/invitations.rs
  • apps/auth-service/src/admin/tests/mod.rs
  • apps/auth-service/src/admin/tests/mutations.rs
  • apps/auth-service/src/alerts/tests.rs
  • apps/auth-service/src/clients/tests/deletion.rs
  • apps/auth-service/src/clients/tests/mod.rs
  • apps/auth-service/src/oidc/tests/app_removal.rs
  • apps/auth-service/src/oidc/tests/authorization.rs
  • apps/auth-service/src/oidc/tests/mod.rs
  • apps/auth-service/src/oidc/tests/policy.rs
  • apps/auth-service/src/oidc/tests/tokens.rs
  • apps/auth-service/src/passkey/tests/related_pages.rs
  • apps/auth-service/src/recovery/tests/undo.rs
  • apps/auth-service/src/security/tests/sessions.rs
  • apps/auth-service/src/tests.rs
  • apps/auth-service/src/web/admin/tests/mod.rs
  • apps/auth-service/src/web/admin/tests/sessions.rs
  • crates/database/src/connection/mod.rs
  • crates/database/src/fixtures.rs
  • crates/database/src/repositories/announcements/tests.rs
  • crates/database/src/transaction.rs
  • docs/dev/testing.md
Files with no reviewable changes: 1

  • apps/auth-service/src/tests.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@PiquelChips
PiquelChips marked this pull request as ready for review November 22, 2026 14:55
@github-project-automation github-project-automation Bot moved this from Needs Triage to Ready in sourceorigin.dev Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Ready

Development

Successfully merging this pull request may close these issues.

1 participant