Keep each test database when the pool replaces its connection (closes #209)
check / check (push) Canceled after 0s
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. NewTestDatabaseURL in internal/db returns the URL of a new in-memory database with a random name. It holds one connection to it open until the test ends. The db, handlers, ircserver, server and service tests now take their database URL from it. 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
This commit is contained in:
@@ -5,21 +5,14 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//nolint:gochecknoglobals // test counter
|
||||
var testDBCounter atomic.Int64
|
||||
|
||||
// NewTestDatabase creates an in-memory database for testing.
|
||||
func NewTestDatabase() (*Database, error) {
|
||||
counter := testDBCounter.Add(1)
|
||||
func NewTestDatabase(tb testing.TB) (*Database, error) {
|
||||
tb.Helper()
|
||||
|
||||
dsn := fmt.Sprintf(
|
||||
"file:testdb%d?mode=memory"+
|
||||
"&cache=shared&_pragma=foreign_keys(1)",
|
||||
counter,
|
||||
)
|
||||
dsn := NewTestDatabaseURL(tb) + "&_pragma=foreign_keys(1)"
|
||||
|
||||
conn, err := sql.Open("sqlite", dsn)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user