test: add test database cleaner in subprocess (#19844)

fixes https://github.com/coder/internal/issues/927

Adds a small subprocess that outlives the testing process to clean up any leaked test databases.
This commit is contained in:
Spike Curtis
2025-09-22 15:27:06 +04:00
committed by GitHub
parent 596fdcba81
commit e2f5401fb2
4 changed files with 249 additions and 1 deletions
@@ -3,6 +3,7 @@ package dbtestutil_test
import (
"database/sql"
"testing"
"time"
_ "github.com/lib/pq"
"github.com/stretchr/testify/require"
@@ -110,3 +111,27 @@ func TestOpen_ValidDBFrom(t *testing.T) {
require.True(t, rows.Next())
require.NoError(t, rows.Close())
}
func TestOpen_Panic(t *testing.T) {
t.Skip("unskip this to manually test that we don't leak a database into postgres")
t.Parallel()
if !dbtestutil.WillUsePostgres() {
t.Skip("this test requires postgres")
}
_, err := dbtestutil.Open(t)
require.NoError(t, err)
panic("now check SELECT datname FROM pg_database;")
}
func TestOpen_Timeout(t *testing.T) {
t.Skip("unskip this and set a short timeout to manually test that we don't leak a database into postgres")
t.Parallel()
if !dbtestutil.WillUsePostgres() {
t.Skip("this test requires postgres")
}
_, err := dbtestutil.Open(t)
require.NoError(t, err)
time.Sleep(11 * time.Minute)
}