Skip to content

test(coderd/database): share one database across TestGetUserStatusCounts subtests - #29464

Draft
ibetitsmike wants to merge 1 commit into
mainfrom
mike/test-user-status-counts-fixtures
Draft

ibetitsmike wants to merge 1 commit into
mainfrom
mike/test-user-status-counts-fixtures

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator

TestGetUserStatusCounts cloned a Postgres database for each of its 756 leaf subtests (6 timezones x 7 report dates x 18 scenarios). The leaves only need query isolation, so they now run inside a rolled-back transaction on one shared database via a new dbtestutil.StartRolledBackTx helper (the rollback counterpart of the existing StartTx). Fixtures, assertions, subtest names and parallelism are unchanged.

One database per scenario, as first sketched, does not work here: every fixture derives from the case's reportUntil and the query aggregates over all users, so users from different dates cannot coexist in one database without changing every expected count. Per-transaction isolation keeps the fixtures byte-identical. The two deleted-user scenarios set deleted_at through the Store (UpdateUserStatus with the intended updated_at, which the deletion trigger copies into user_deleted) instead of raw SQL, because a transaction-bound Store cannot run raw statements; the resulting user_status_changes and user_deleted rows are the same as before.

Before / After

Local, go test -count=1 -tags=testsmallbatch -parallel=8:

Before After
dbtestutil.NewDB calls in the test 756 1
./coderd/database/ -run '^TestGetUserStatusCounts$' ok 63.2s ok 28.2s
./coderd/database/ ok 57.2s ok 33.0s
Subtests (=== RUN) 925 925
Sum of subtest durations 503s 222s
Together with #29465 (jit = off in dbtestutil) test ok 0.7s, package ok 9.7s

The remaining 28s is the query itself being JIT-compiled per call, which #29465 removes; the two changes compose.

Validation

  • go test -count=1 -tags=testsmallbatch -parallel=8 ./coderd/database/ -run '^TestGetUserStatusCounts$' -v passes with 925 subtests
  • go test -count=2 -shuffle=on -race -run '^TestGetUserStatusCounts$' ./coderd/database/ passes (shared-fixture leakage check)
  • golangci-lint run on coderd/database/ and coderd/database/dbtestutil/: 0 issues
  • Repository pre-commit hook passed on the commit

Part of the test-suite speedup series (see #29457).

Remote validation

Remote run on a dogfood workspace (separate compute), chat https://dogfood.cdr.dev/agents/5bbe73d0-866a-4841-9cc3-c5e5b4d97e44, tested head 16c8d556c4d against base 9d973372774 on the same machine: ./coderd/database/ -run '^TestGetUserStatusCounts$' 46.97s to 15.05s (925 subtests, 925 pass on both); ./coderd/database/ 56.43s to 22.66s; -race -count=2 -shuffle=on on the test: pass; -parallel=1: ok 23.3s (the rolled-back transactions do not deadlock when serialized). With #29465 cherry-picked on top: test ok 0.58s, package ok 7.49s. Endorsed verdict: PASS.

Xum (an AI agent) authored this PR on behalf of @ibetitsmike.

…nts subtests

Each of the 756 leaf subtests cloned its own Postgres database. They only
need query isolation, so run every leaf inside a rolled-back transaction on
one shared database via the new dbtestutil.StartRolledBackTx helper. The
deleted-user scenarios set deleted_at through the Store instead of raw SQL,
which the transaction-bound Store cannot run.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant