testdb

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package testdb connects integration tests to PostgreSQL.

Tests that need a database call Pool. They are skipped when the TASKMILL_TEST_DATABASE_URL environment variable is unset; `make test` sets it to the database that `make db` starts. In CI they fail instead, so that a run that tested nothing cannot pass: see URL. Each test gets its own schema, so tests can run in parallel and leave nothing behind.

The tests run far from UTC, so that code that depends on a time zone fails: the sessions of a Pool in a time zone of their own, and the process of a package whose TestMain calls Main in another.

To run the tests through a transaction-pooling proxy such as PgBouncer, point TASKMILL_TEST_DATABASE_URL at the proxy and TASKMILL_TEST_LISTEN_URL at the database itself: workers then listen for notifications on a direct connection, which the proxy cannot provide.

Index

Constants

View Source
const (
	EnvURL       = "TASKMILL_TEST_DATABASE_URL" // the test database URL
	EnvListenURL = "TASKMILL_TEST_LISTEN_URL"   // a direct URL, if EnvURL goes through a proxy
)

Environment variables that configure the test database.

Variables

This section is empty.

Functions

func CheckGroups added in v0.2.0

func CheckGroups(tb testing.TB, pool *pgxpool.Pool, schema string)

CheckGroups fails the test unless every FIFO group of schema satisfies the invariants in docs/design/task-lifecycle.md: its count matches its unfinished tasks plus a halting dead head, it has exactly one head, and every unfinished grouped task belongs to a group row.

func DirectURL added in v0.2.0

func DirectURL(tb testing.TB) string

DirectURL returns a URL that reaches the test database without a proxy: EnvListenURL if it is set, or else EnvURL. Tests that set connection parameters, which proxies refuse, connect with it.

func GiveUp added in v0.11.1

func GiveUp(tb testing.TB, pool *pgxpool.Pool, held, wanted string)

GiveUp runs a statement on pool and gives it up, as a worker that is told to stop gives up the statements that it had sent, and returns while the statement goes on in the database. The statement locks the table held, outlives the request to cancel it, as a statement does that the server had not read by then, and 300 milliseconds later locks the table wanted. A drop of the tables' schema that begins meanwhile, and locks wanted first, waits for the statement, and the statement for the drop.

func ListenConfig added in v0.2.0

func ListenConfig(tb testing.TB) *pgx.ConnConfig

ListenConfig returns the connection that workers listen on: EnvListenURL's, or nil if it is unset, which makes workers listen on their pools'.

func Main added in v0.11.0

func Main(m *testing.M)

Main runs the tests of a package, as m.Run does, in a process whose local time zone is 13 hours and 45 minutes ahead of UTC, as the Chatham Islands are in summer. The driver reads a time into the local zone, so Go code that formats such a time, or takes its day or its hour, without naming a zone, gets another hour at any time of day, and another day for most of it. The machines of CI run in UTC, where such code passes.

A package's tests call it from their TestMain:

func TestMain(m *testing.M) { testdb.Main(m) }

func Pool

func Pool(tb testing.TB) *pgxpool.Pool

Pool returns a pool of up to 8 connections to the test database, closed when the test ends. Its sessions run in a time zone far from UTC.

func PoolSize

func PoolSize(tb testing.TB, conns int32) *pgxpool.Pool

PoolSize is Pool with a given number of connections, for tests that run many operations at once.

func Schema

func Schema(tb testing.TB, pool *pgxpool.Pool) string

Schema returns a fresh schema name, and drops that schema when the test ends, on a connection of pool: see teardown.DropSchema.

func URL

func URL(tb testing.TB) string

URL returns the test database URL. If none is set, it skips the test, so that `go test ./...` works on a machine without a database. In CI, which sets the CI environment variable to any value, it fails the test instead.

Types

This section is empty.

Jump to

Keyboard shortcuts

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