Commit Graph
2 Commits
Author SHA1 Message Date
Michael Suchacz 3a945fe37a test(coderd/database): share one database across TestGetUserStatusCounts subtests (#29464)
`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.
2026-09-17 20:26:57 +02:00
Steven Masley 854044e811 chore: fix concurrent CommitQuota transactions for unrelated users/orgs (#15261)
The failure condition being fixed is `w1` and `w2` could belong
to different users, organizations, and templates and still cause a
serializable failure if run concurrently. This is because the old query 
did a `seq scan` on the `workspace_builds` table. Since that is the 
table being updated, we really want to prevent that.

So before this would fail for any 2 workspaces. Now it only fails if
`w1` and `w2` are owned by the same user and organization.
2024-11-01 11:05:49 -04:00