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