`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.
107 lines
2.4 KiB
Go
107 lines
2.4 KiB
Go
package dbtestutil
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
)
|
|
|
|
type DBTx struct {
|
|
database.Store
|
|
mu sync.Mutex
|
|
done chan error
|
|
finalErr chan error
|
|
}
|
|
|
|
// StartTx starts a transaction and returns a DBTx object. This allows running
|
|
// 2 transactions concurrently in a test more easily.
|
|
// Example:
|
|
//
|
|
// a := StartTx(t, db, opts)
|
|
// b := StartTx(t, db, opts)
|
|
//
|
|
// a.GetUsers(...)
|
|
// b.GetUsers(...)
|
|
//
|
|
// require.NoError(t, a.Done()
|
|
func StartTx(t *testing.T, db database.Store, opts *database.TxOptions) *DBTx {
|
|
done := make(chan error)
|
|
finalErr := make(chan error)
|
|
txC := make(chan database.Store)
|
|
|
|
go func() {
|
|
t.Helper()
|
|
once := sync.Once{}
|
|
count := 0
|
|
|
|
err := db.InTx(func(store database.Store) error {
|
|
// InTx can be retried
|
|
once.Do(func() {
|
|
txC <- store
|
|
})
|
|
count++
|
|
if count > 1 {
|
|
// If you recursively call InTx, then don't use this.
|
|
t.Logf("InTx called more than once: %d", count)
|
|
assert.NoError(t, xerrors.New("InTx called more than once, this is not allowed with the StartTx helper"))
|
|
}
|
|
|
|
<-done
|
|
// Just return nil. The caller should be checking their own errors.
|
|
return nil
|
|
}, opts)
|
|
finalErr <- err
|
|
}()
|
|
|
|
txStore := <-txC
|
|
close(txC)
|
|
|
|
return &DBTx{Store: txStore, done: done, finalErr: finalErr}
|
|
}
|
|
|
|
// Done can only be called once. If you call it twice, it will panic.
|
|
func (tx *DBTx) Done() error {
|
|
tx.mu.Lock()
|
|
defer tx.mu.Unlock()
|
|
|
|
close(tx.done)
|
|
return <-tx.finalErr
|
|
}
|
|
|
|
var errRollbackTestTx = xerrors.New("roll back test transaction")
|
|
|
|
// StartRolledBackTx returns a Store bound to a transaction that is rolled back
|
|
// when the test finishes. It lets parallel subtests that only need query
|
|
// isolation share one database instead of creating one each.
|
|
func StartRolledBackTx(t testing.TB, db database.Store) database.Store {
|
|
t.Helper()
|
|
done := make(chan struct{})
|
|
txC := make(chan database.Store, 1)
|
|
errC := make(chan error, 1)
|
|
|
|
go func() {
|
|
errC <- db.InTx(func(tx database.Store) error {
|
|
txC <- tx
|
|
<-done
|
|
return errRollbackTestTx
|
|
}, nil)
|
|
}()
|
|
|
|
var tx database.Store
|
|
select {
|
|
case tx = <-txC:
|
|
case err := <-errC:
|
|
require.NoError(t, err, "start transaction")
|
|
}
|
|
t.Cleanup(func() {
|
|
close(done)
|
|
assert.ErrorIs(t, <-errC, errRollbackTestTx)
|
|
})
|
|
return tx
|
|
}
|