Skip to content

Clone per-test Postgres databases from a template to fix Go server test timeout - #20700

Merged
pelikhan merged 2 commits into
mainfrom
pelikhan/fix-go-server-test-timeout
Nov 12, 2026
Merged

pelikhan merged 2 commits into
mainfrom
pelikhan/fix-go-server-test-timeout

Conversation

@pelikhan

Copy link
Copy Markdown
Collaborator

"Go unit tests (http, shard 0)" in cgo.yml times out after 10 minutes in server/internal/server, on both PRs and main. The SQLite removal (#20668) made every testutil.Postgres fixture provision a full canonical store: about 360 daily partitions in about 90 DDL transactions, roughly 5s per test. With 489 tests, the package runs past the timeout. No test hangs, and the CI split in #20689 is not the cause.

Approach

  • New server/internal/testutil/pgtemplate package.
    • Each test process provisions one template database through the production store (postgresx.NewConfig).
    • Each test gets its own clone via CREATE DATABASE ... TEMPLATE, so tests stay isolated.
    • If the role cannot create databases, it falls back to the previous per-schema setup.
    • The package has no store dependency, so postgresx's own package tests can use it without an import cycle.
  • Fixtures moved to the template:
    • testutil.PostgresWithConfig
    • postgresx's nativeTestStore
  • Three collect subprocess tests now receive the clone's DSN (CAO_NATIVE_*_CANONICAL_DSN) when they reopen the canonical store.
  • Stale cleanup. Database names encode host, PID and start time. Later runs drop databases whose owning process on this host has exited, and anything older than 24h.
  • Redundant work trimmed.
    • TestSessionArchiveResponseClosesReader: the six subtests share one app.
    • TestRepositoryQueriesUsePostgres: now only checks that the eight repository queries are in the deployed corpus, since TestDashboardQueryCorpusUsesPostgres already executes the full corpus.

Results

Measured locally with -p 1 (as in CI) against postgres:16.

Package Before After
internal/server 539s 102s
internal/postgresx 601s 296-370s

Most of the remaining postgresx time is production publication cost (about 0.6s per publication), not setup.

These packages pass: internal/server, postgresx, collect, collectnative, simulator, adxingest, dashboardstore, cmd/cao-dashboard and testutil/.... No workflow YAML changed.

Review notes

  • Clones open with schema outcome contract-match rather than fresh-schema; no test depends on that difference. schemaTestSchema still exercises fresh initialization directly.
  • DROP DATABASE ... WITH (FORCE) requires PG13+; CI uses postgres:16.
  • Running several packages concurrently against one server (-p > 1) can exhaust the shared lock table, because dashboard queries lock every daily partition. Raising max_locks_per_transaction (for example to 512) fixes it, and the package doc notes this. CI runs with -p 1.
  • TestPostgresRuntimePrivilegeBoundary failed once locally in a full run, then passed on every repeat (including -count=8). It uses a separate roles fixture this change does not touch, so it looks like a rare pre-existing flake.

…edundant fixtures

Since the SQLite removal, every testutil.Postgres fixture provisions a full
canonical store (~360 daily partitions, ~5s), pushing internal/server past
the 10m go test timeout. Provision one template database per test process
through the production store and clone it per test (CREATE DATABASE ...
TEMPLATE), falling back to a private schema when the role cannot create
databases. Reclaim databases left by exited processes.

Also share one app across TestSessionArchiveResponseClosesReader subtests
and stop re-executing repository queries already covered by the full
dashboard corpus test.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@pelikhan
pelikhan marked this pull request as ready for review November 12, 2026 18:29
@pelikhan
pelikhan requested a review from mnkiefer as a code owner November 12, 2026 18:29
…r-test-timeout

# Conflicts:
#	server/internal/testutil/pgtemplate/pgtemplate.go
@pelikhan
pelikhan merged commit 433d929 into main Nov 12, 2026
6 checks passed
@pelikhan
pelikhan deleted the pelikhan/fix-go-server-test-timeout branch November 12, 2026 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant