block/buzz · error
connect to test DB
Error message
connect to test DB
What it means
Panic in the shared `setup_db` test helper: `PgPool::connect(&database_url).await.expect("connect to test DB")`. It fires when the test suite cannot establish a Postgres connection using TEST_DATABASE_URL (or the built-in TEST_DB_URL default). Every test in this module calls setup_db, so a connection failure fails them all with this message.
Source
Thrown at crates/buzz-db/src/store/community.rs:567
mod tests {
//! Pin the load-bearing contract for `Db::communities_of_channels`:
//! a channel id that does NOT exist MUST be absent from the result
//! map, never mapped to a default. The relay-side read-row emitter
//! relies on this — a missing entry triggers `MissingLookup →
//! ImplBug{row_community_lookup_missing} → CoverageBreach`. If this
//! helper ever started returning a default/zero entry for unknown
//! channels, that fail-closed chain would go blind.
use super::*;
use sqlx::PgPool;
const TEST_DB_URL: &str = "postgres://buzz:buzz_dev@localhost:5432/buzz"; // sadscan:disable np.postgres.1
async fn setup_db() -> Db {
let database_url =
std::env::var("TEST_DATABASE_URL").unwrap_or_else(|_| TEST_DB_URL.into());
let pool = PgPool::connect(&database_url)
.await
.expect("connect to test DB");
Db::from_pool(pool)
}
async fn make_community(pool: &PgPool) -> Uuid {
let id = Uuid::new_v4();
let host = format!("communities-of-channels-{}.example", id.simple());
sqlx::query("INSERT INTO communities (id, host) VALUES ($1, $2)")
.bind(id)
.bind(host)
.execute(pool)
.await
.expect("insert community");
id
}
async fn insert_channel(pool: &PgPool, community_id: Uuid, channel_id: Uuid) {
let creator: Vec<u8> = vec![0u8; 32];
sqlx::query(View on GitHub (pinned to eed74bde2f)
Solutions
- Start the test infrastructure: `just test` (or docker compose up the Postgres service) before running cargo test.
- Export TEST_DATABASE_URL to a reachable Postgres, e.g. postgres://postgres:postgres@localhost:5432/buzz_test.
- Verify credentials and database name; create the test database if missing.
- If connections are exhausted, close idle pools in tests (drop(pool)) or raise max_connections.
Example fix
// before
let database_url = std::env::var("TEST_DATABASE_URL").unwrap_or_else(|_| TEST_DB_URL.into());
let pool = PgPool::connect(&database_url).await.expect("connect to test DB");
// after
let database_url = std::env::var("TEST_DATABASE_URL").unwrap_or_else(|_| TEST_DB_URL.into());
let pool = PgPool::connect(&database_url).await
.unwrap_or_else(|e| panic!("connect to test DB at {database_url}: {e}")); Defensive patterns
Strategy: validation
Validate before calling
fn require_test_database_url() -> String {
std::env::var("TEST_DATABASE_URL")
.unwrap_or_else(|_| TEST_DB_URL.into())
}
// pre-flight check before the suite:
let url = require_test_database_url();
PgPool::connect(&url).await.expect("TEST_DATABASE_URL unreachable — start Postgres (just test / docker compose up)"); Try / catch
let pool = PgPool::connect(&database_url).await
.unwrap_or_else(|e| panic!("connect to test DB ({database_url}): {e} — is Postgres running and TEST_DATABASE_URL set?")); Prevention
- Always run integration tests through `just test`, which provisions Postgres.
- Set TEST_DATABASE_URL in .env and keep it consistent across worktrees.
- Add a pre-flight connectivity check before running long suites.
- In CI, verify the Postgres service container is healthy before cargo test.
When it happens
Trigger: TEST_DATABASE_URL unset and the default URL unreachable; Postgres container not started; wrong host/port/user/password/database name; Postgres up but max_connections exhausted; DNS failure for a non-localhost host.
Common situations: Running `cargo test` directly instead of `just test` (which starts the Docker Postgres), switching between worktrees with different .env values, CI missing the Postgres service container, or a stale TEST_DATABASE_URL pointing at a torn-down scratch instance.
Related errors
- load desired-state live roster
- ban failed: {e}
- timeout failed: {e}
- routed query, gate on
- count, gate off
AI-assisted analysis of block/buzz@eed74bde2f (2026-08-30).
Data as JSON: /api/errors/79c07c58eb43c516.
Report an issue: GitHub.