Help us shape the future of Go! Take the 2026 Go Developer Survey

pgtest

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package pgtest is test support: it starts one Dockerized Postgres per test binary and hands out fresh databases — migrated pools via NewPool, or bare DSNs via FreshDB for suites that exercise store.Open/Migrate themselves. Every Postgres-backed suite uses it; production code must never import it. A missing Docker daemon is a hard failure, not a skip: skipped contract tests would silently hollow out the coverage gate.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultRepeatableRead added in v0.5.0

func DefaultRepeatableRead(t *testing.T, pool *pgxpool.Pool)

DefaultRepeatableRead sets pool's database to begin every transaction that names no isolation level at REPEATABLE READ, as an operator may, and drops the pool's connections so each new one takes the setting. The database is the test's own, so no other test sees it.

func FreshDB added in v0.2.0

func FreshDB(t *testing.T) string

FreshDB creates a new empty database in the shared container and returns its DSN, un-migrated, so a suite can exercise store.Open/Migrate itself from a clean slate.

func Main

func Main(m *testing.M) int

Main wraps testing.M: it starts the shared container, runs the suite, and tears the container down. Use from TestMain: os.Exit(pgtest.Main(m)). The start is attempted twice, with a fresh container in between (#265; see readyTimeout). The retry is unconditional on the error's class because even a failed `docker run` can be load-induced (port programming races under a crowded daemon); when the daemon is simply absent, the second attempt fails in milliseconds and costs nothing.

Teardown below is a defer, which a killed process never reaches, so the run opens by reaping what an earlier killed run left (#346; dockertest holds the whole rationale, including why a container young enough to be a peer's is never touched).

func NewChildThread added in v0.4.0

func NewChildThread(t *testing.T, pool *pgxpool.Pool, sessionID domain.ID) domain.ID

NewChildThread inserts an idle child thread under the session's primary — the test seam plan 35 slice 3 runs the thread substrate through until slice 4's delegation spawns real ones — carrying a minimal agent snapshot (model "fixture-model", no tools) named "worker", and returns its id.

func NewChildThreadWithAgent added in v0.4.0

func NewChildThreadWithAgent(t *testing.T, pool *pgxpool.Pool, sessionID domain.ID, agentJSON string) domain.ID

NewChildThreadWithAgent is NewChildThread with the given SessionThreadAgent snapshot (its name is the thread's agent_name).

func NewPool

func NewPool(t *testing.T) *pgxpool.Pool

NewPool creates a fresh database in the shared container, migrates it, and returns a pool closed at test end.

func NewSession

func NewSession(t *testing.T, pool *pgxpool.Pool, envKind string) (sessionID, envID domain.ID)

NewSession inserts the minimum fixture rows (agent, agent version, environment of the given kind, session) and returns the session and environment ids.

func NewSessionInEnv

func NewSessionInEnv(t *testing.T, pool *pgxpool.Pool, envID domain.ID) (sessionID domain.ID)

NewSessionInEnv inserts an additional idle session (with its own throwaway agent + version) into an existing environment and returns the session id. Use it to place several sessions — and thus several work items — under one environment, since Enqueue dedupes per (session, kind) while a live item exists.

func SetSessionStatus added in v0.4.0

func SetSessionStatus(t *testing.T, pool *pgxpool.Pool, sessionID domain.ID, status string)

SetSessionStatus moves a fixture session to status the way the platform does — the primary thread's row (its status, and the transitioned flag a real move sets) and the session's column together (plan 35 decision 4: the session's status is a fold over its threads', so a fixture that moved the column alone would be read back as its idle primary).

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL