1 Commits
Author SHA1 Message Date
clawbot 9116bc7446 Keep each test database when the pool replaces its connection (closes #209)
check / check (push) Canceled after 0s
SQLite deletes a shared in-memory database when its last connection
closes. A query cancelled while it runs makes the pool close its
connection, so a test could lose all its tables and rows at random.

The new package internal/dbtest has NewTestDatabaseURL. It returns the
URL of a new in-memory database with a random name and holds one
connection to it open until the test ends. The db, handlers,
ircserver, server and service tests take their database URL from it.
The test-support lint rule keeps the package out of the server binary.

A new test cancels a query on such a database, checks with a TEMP
table that the connection was replaced, and checks that a row is
still there.

Model: opus-5-5
2026-10-27 10:52:25 +00:00
8 changed files with 17 additions and 18 deletions

No files matched your search

-1
View File
@@ -3342,7 +3342,6 @@ neoirc/
│ │ └── schema/ │ │ └── schema/
│ │ ├── 000.sql # schema_migrations bootstrap │ │ ├── 000.sql # schema_migrations bootstrap
│ │ └── 001_initial.sql │ │ └── 001_initial.sql
│ ├── dbtest/ # Gives each test its own database; only tests import it
│ ├── globals/ # Application-wide metadata │ ├── globals/ # Application-wide metadata
│ │ └── globals.go │ │ └── globals.go
│ ├── handlers/ # HTTP request handlers │ ├── handlers/ # HTTP request handlers
+6 -6
View File
@@ -156,15 +156,15 @@ func TestForeignKeysOnAfterCancelledQuery(t *testing.T) {
} }
} }
// TestNewURLKeepsDataAfterCancelledQuery cancels a query on a test // TestNewTestDatabaseURLKeepsDataAfterCancelledQuery cancels a query
// database while it runs, which makes the pool close its only // on a test database while it runs, which makes the pool close its
// connection and open a new one. A TEMP table lasts only as long as // only connection and open a new one. A TEMP table lasts only as long
// its connection, so its absence shows the connection was replaced. // as its connection, so its absence shows the connection was replaced.
// The database must still have its tables and rows. // The database must still have its tables and rows.
func TestNewURLKeepsDataAfterCancelledQuery(t *testing.T) { func TestNewTestDatabaseURLKeepsDataAfterCancelledQuery(t *testing.T) {
t.Parallel() t.Parallel()
database, err := startDatabase(t, dbtest.NewURL(t)) database, err := startDatabase(t, dbtest.NewTestDatabaseURL(t))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
+1 -1
View File
@@ -14,7 +14,7 @@ import (
func NewTestDatabase(tb testing.TB) (*Database, error) { func NewTestDatabase(tb testing.TB) (*Database, error) {
tb.Helper() tb.Helper()
dsn := dbtest.NewURL(tb) + "&_pragma=foreign_keys(1)" dsn := dbtest.NewTestDatabaseURL(tb) + "&_pragma=foreign_keys(1)"
conn, err := sql.Open("sqlite", dsn) conn, err := sql.Open("sqlite", dsn)
if err != nil { if err != nil {
+6 -6
View File
@@ -10,12 +10,12 @@ import (
_ "modernc.org/sqlite" // registers the sqlite driver _ "modernc.org/sqlite" // registers the sqlite driver
) )
// NewURL returns the URL of a new, empty in-memory database for one // NewTestDatabaseURL returns the URL of a new, empty in-memory
// test. SQLite deletes an in-memory database when its last connection // database for one test. SQLite deletes an in-memory database when
// closes. A pool closes a connection when a query on it is cancelled // its last connection closes. A pool closes a connection when a query
// while it runs. So this function holds a connection of its own open // on it is cancelled while it runs. So this function holds a
// until the test ends. // connection of its own open until the test ends.
func NewURL(tb testing.TB) string { func NewTestDatabaseURL(tb testing.TB) string {
tb.Helper() tb.Helper()
// A random name, so that no other test can share the database. // A random name, so that no other test can share the database.
+1 -1
View File
@@ -164,7 +164,7 @@ func newTestServerWith(
viperMu.Lock() viperMu.Lock()
defer viperMu.Unlock() defer viperMu.Unlock()
dbURL := dbtest.NewURL(t) dbURL := dbtest.NewTestDatabaseURL(t)
copyTemplateDB(t, dbURL) copyTemplateDB(t, dbURL)
var ( var (
+1 -1
View File
@@ -107,7 +107,7 @@ func newTestEnvWithResolver(
) *testEnv { ) *testEnv {
t.Helper() t.Helper()
conn, err := sql.Open("sqlite", dbtest.NewURL(t)) conn, err := sql.Open("sqlite", dbtest.NewTestDatabaseURL(t))
if err != nil { if err != nil {
t.Fatalf("open db: %v", err) t.Fatalf("open db: %v", err)
} }
+1 -1
View File
@@ -241,7 +241,7 @@ func newApp(
stats.New, stats.New,
), ),
fx.Decorate(func(cfg *config.Config) *config.Config { fx.Decorate(func(cfg *config.Config) *config.Config {
cfg.DBURL = dbtest.NewURL(t) cfg.DBURL = dbtest.NewTestDatabaseURL(t)
cfg.Port = port cfg.Port = port
return cfg return cfg
+1 -1
View File
@@ -46,7 +46,7 @@ type testEnv struct {
func newTestEnv(t *testing.T) *testEnv { func newTestEnv(t *testing.T) *testEnv {
t.Helper() t.Helper()
dbURL := dbtest.NewURL(t) dbURL := dbtest.NewTestDatabaseURL(t)
var ( var (
database *db.Database database *db.Database