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
- func CheckGroups(tb testing.TB, pool *pgxpool.Pool, schema string)
- func DirectURL(tb testing.TB) string
- func GiveUp(tb testing.TB, pool *pgxpool.Pool, held, wanted string)
- func ListenConfig(tb testing.TB) *pgx.ConnConfig
- func Main(m *testing.M)
- func Pool(tb testing.TB) *pgxpool.Pool
- func PoolSize(tb testing.TB, conns int32) *pgxpool.Pool
- func Schema(tb testing.TB, pool *pgxpool.Pool) string
- func URL(tb testing.TB) string
Constants ¶
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
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
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
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
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 ¶
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 ¶
PoolSize is Pool with a given number of connections, for tests that run many operations at once.
func Schema ¶
Schema returns a fresh schema name, and drops that schema when the test ends, on a connection of pool: see teardown.DropSchema.
Types ¶
This section is empty.