Documentation
¶
Index ¶
- func Await[T any](t *testing.T, done <-chan T) T
- func AwaitArrivalsOrLockWaits(ctx context.Context, db LockDB, parties int, arrived func() int, ...) error
- func AwaitBlockedBy[T any](ctx context.Context, t *testing.T, db LockDB, holderPID int, done <-chan T) (T, bool)
- func BackendPID(ctx context.Context, t *testing.T, tx pgx.Tx) int
- func LockWaiters(ctx context.Context, db LockDB, queryLike string) (int, error)
- func NewIsolatedSchema(ctx context.Context, t *testing.T, c *PostgresContainer) *pgxpool.Pool
- func TerminateSharedPostgres() error
- func WaitForLockWait(ctx context.Context, t *testing.T, db LockDB, queryLike string)
- type Footprint
- type LockDB
- type PostgresContainer
- func RequireEmptyDatabase(ctx context.Context, t *testing.T) *PostgresContainer
- func RequireMigratedDatabase(ctx context.Context, t *testing.T, migrationsPath string) *PostgresContainer
- func RequirePostgresContainer(ctx context.Context, t *testing.T) *PostgresContainer
- func SetupPostgresContainer(ctx context.Context, t *testing.T) (*PostgresContainer, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Await ¶
Await returns the value a background writer sends on done, failing fast instead of hanging until the go test timeout when the writer is stuck.
func AwaitArrivalsOrLockWaits ¶
func AwaitArrivalsOrLockWaits(ctx context.Context, db LockDB, parties int, arrived func() int, queryLike string) error
AwaitArrivalsOrLockWaits is a race-test barrier: it returns once each of parties has either arrived (arrived reports how many have) or is waiting on a lock in a statement matching queryLike. Under a serializing lock the parties that have not arrived are provably parked on it, so waiting longer cannot change the outcome; without the lock every party arrives. Either way the barrier opens on observed state, never on a guessed timeout. It returns an error rather than failing a test so the goroutine under test can call it.
func AwaitBlockedBy ¶
func AwaitBlockedBy[T any](ctx context.Context, t *testing.T, db LockDB, holderPID int, done <-chan T) (T, bool)
AwaitBlockedBy proves a contender is queued behind holderPID's transaction: it returns once some backend waits on a lock that holderPID holds, so the contender cannot finish before the holder ends. If done delivers first, the contender answered without contending; AwaitBlockedBy returns that value and true. A fixed quiet window (select on done against time.After) cannot tell "blocked" from "not there yet", so on a slow runner it passes a contender that never contends. This observation cannot.
Query db from a connection other than the holder's: pg_locks is read live, but the holder's session is busy holding the lock.
func BackendPID ¶
BackendPID returns the server process ID of tx's session, the holderPID that AwaitBlockedBy takes.
func LockWaiters ¶
LockWaiters counts backends of this database that wait on a lock in a statement matching queryLike (a SQL LIKE pattern).
func NewIsolatedSchema ¶
NewIsolatedSchema creates a schema of its own on the database c points at and returns a pool whose search_path is that schema, so tests can run in parallel on one database without seeing each other's rows. The schema is named from t.Name() plus a unique suffix and dropped, with the pool closed, in t.Cleanup.
Apply migrations with the same call RequireMigratedDatabase uses, passing the returned pool: migrations.RunMigrations(ctx, pool, path, "", ""). The migrator creates its tables and schema_migrations in the first schema on search_path, which is the isolated one.
"public" stays second on search_path because the migrations install the uuid-ossp and pg_trgm extensions. Left to the migrations they would land in the first test's schema and vanish with it while other schemas still use them, so they are created once in public here.
func TerminateSharedPostgres ¶
func TerminateSharedPostgres() error
TerminateSharedPostgres stops the shared container if one was started. Call it from TestMain after m.Run. Ryuk reaps the container if the process dies first, so this is tidiness rather than the only line of defense.
func WaitForLockWait ¶
WaitForLockWait blocks until some backend is waiting on a lock in a statement matching queryLike (a SQL LIKE pattern; "" matches any), which proves the writer under test is queued behind the footprint.
Types ¶
type Footprint ¶
Footprint stands in for the editor command's lock footprint on one plan so the race tests of legacy writers run before the command exists: the plan row FOR UPDATE, then its executions ordered FOR UPDATE, then its accounts FOR SHARE. It does NOT take the command's first two lock levels, the identity advisory lock and the sorted provider advisory locks (LockPlanProvidersTx). The embedded Tx is the command's transaction: Exec applies the command's writes (SetManaged, inserted roots) and Commit/Rollback end it. Executions lists the rows the footprint locked, i.e. what the command captured: a writer that committed before the footprint must appear here.
Swap contract for the ExecutePlanScheduleCommand race tests: replace HoldCommandFootprint with the real command paused after its capture, wrapped as a *Footprint (Tx = the command's transaction, Executions = its captured set). Then (1) the notifier claim queues first on the provider advisory lock, so its WaitForLockWait pattern changes from "%FOR UPDATE%" to the advisory wait (wait_event 'advisory'; match the pg_advisory_xact_lock statement); (2) the direct Exec/QueryRow calls of the retry, prune and account-delete tests become the command's own writes and reads; every other assertion stays.
func HoldCommandFootprint ¶
HoldCommandFootprint opens the footprint on planID. The transaction rolls back at test end unless the test ended it first.
type LockDB ¶
type LockDB interface {
Begin(ctx context.Context) (pgx.Tx, error)
QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
}
LockDB is the slice of a pool the lock helpers need. The config store's dbConn and a *pgxpool.Pool both satisfy it.
type PostgresContainer ¶
type PostgresContainer struct {
Container testcontainers.Container
Config *database.Config
DB *database.Connection
}
PostgresContainer wraps a testcontainers PostgreSQL instance.
func RequireEmptyDatabase ¶
func RequireEmptyDatabase(ctx context.Context, t *testing.T) *PostgresContainer
RequireEmptyDatabase gives t an empty database of its own on the same shared container RequireMigratedDatabase uses: no tables, no schema_migrations, nothing but what CREATE DATABASE ... TEMPLATE template0 puts there. It is for tests whose subject is the migration chain itself (up, down, dirty, heal), which need to start from nothing and must not see a template another test migrated.
Cleanup, skips and the missing Container are exactly as for RequireMigratedDatabase. Being per database, it is no substitute for a dedicated server where a test touches server-wide state (roles, ALTER SYSTEM, connection limits, other sessions' backends); see RequirePostgresContainer for those.
func RequireMigratedDatabase ¶
func RequireMigratedDatabase(ctx context.Context, t *testing.T, migrationsPath string) *PostgresContainer
RequireMigratedDatabase gives t a database of its own on a PostgreSQL container shared by the whole test binary, with every migration under migrationsPath already applied. The database is dropped when t ends, and the connection pool is closed before that, so callers have nothing to clean up.
Skips follow RequirePostgresContainer: only an unusable Docker provider skips. A container that will not start or a migration that will not apply fails the test (issue #1597).
The returned value has no Container: it is shared, and terminating it is the job of TerminateSharedPostgres, called from the package's TestMain.
func RequirePostgresContainer ¶
func RequirePostgresContainer(ctx context.Context, t *testing.T) *PostgresContainer
RequirePostgresContainer starts a PostgreSQL test container for t, skipping the test only when this environment has no usable Docker provider and failing loudly for every other error.
Drawing that line is the whole point (issue #1597). "No usable Docker daemon" is an environment fact and the one legitimate reason to skip, so it is probed explicitly before anything is started. Past that probe the daemon answered a health check, which makes a container that still refuses to come up -- a missing image, a database that never accepts connections -- a real failure. Reporting it as a skip would turn a broken run green, which is indistinguishable from a run that had nothing to say.
The probe owns the whole environment side of that line, so a daemon that is present but unhealthy skips too, rather than failing.
Callers remain responsible for Cleanup, matching SetupPostgresContainer.
Prefer RequireMigratedDatabase or RequireEmptyDatabase, which share one server per test binary. Use this only for a test that needs a server to itself (see SetupPostgresContainer for when), with a comment saying why.
func SetupPostgresContainer ¶
SetupPostgresContainer creates and starts a PostgreSQL test container.
Prefer RequireMigratedDatabase or RequireEmptyDatabase in tests. Each call here starts a server of its own, about 2s before the test has even migrated it, where those hand out a fresh database on one server per test binary in milliseconds. A dedicated server is still right when a test changes or depends on server-wide state: roles, ALTER SYSTEM or other server settings, terminating other sessions' backends, connection limits, or stopping the server. Say why next to the call. Tests should go through RequirePostgresContainer, which only skips when Docker is unusable; this function remains for the shared server itself and for that wrapper.
Not marked Deprecated: the shared server is built on it and the dedicated cases above are legitimate, so a staticcheck SA1019 warning on every call would be noise rather than guidance.
func (*PostgresContainer) Cleanup ¶
func (c *PostgresContainer) Cleanup(ctx context.Context) error
Cleanup terminates the test container and closes database connection.
func (*PostgresContainer) ResetDatabase ¶
func (c *PostgresContainer) ResetDatabase(ctx context.Context) error
ResetDatabase drops and recreates all tables (useful for clean state).
func (*PostgresContainer) TruncateTables ¶
func (c *PostgresContainer) TruncateTables(ctx context.Context, tables ...string) error
TruncateTables removes all data from tables (useful between tests).