Keep each test database when the pool replaces its connection (closes #209) #210

Merged
clawbot merged 1 commits from issue-209-test-databases into next 2026-10-27 12:26:49 +01:00
Collaborator

Fixes #209.

Cause. The tests used shared in-memory SQLite databases. SQLite deletes such a 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.

Fix. NewURL in the new package internal/dbtest returns the URL of a new in-memory database with a random name. It holds one connection of its own to that database open until the test ends. These packages' tests now take their database URL from it:

  • internal/db
  • internal/handlers
  • internal/ircserver
  • internal/server
  • internal/service

internal/dbtest is on the deny list of the test-support rule in .golangci.yml. Only tests can import it, so the server binary does not link the testing package. The README's Project Structure lists it.

Why not a temporary file database. The databases stay in memory, so every package tests as fast as before. A file database was measured slower for internal/handlers in #204.

Test. TestNewURLKeepsDataAfterCancelledQuery cancels a query, checks with a TEMP table that the pool replaced its connection, and checks that a row written before is still there. Without the held connection it fails with no such table: channels.

What the diff does not show. internal/handlers already kept its database by accident, because copyTemplateDB leaves its own pool open until the test ends. That function is unchanged.

Disclosures

  • Not fixed here: the internal/ircserver tests turn foreign keys on with one PRAGMA, so a replacement connection there runs without them.

Model: opus-5-5

Fixes https://git.eeqj.de/sneak/neoirc/issues/209. **Cause.** The tests used shared in-memory SQLite databases. SQLite deletes such a 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. **Fix.** `NewURL` in the new package `internal/dbtest` returns the URL of a new in-memory database with a random name. It holds one connection of its own to that database open until the test ends. These packages' tests now take their database URL from it: - `internal/db` - `internal/handlers` - `internal/ircserver` - `internal/server` - `internal/service` `internal/dbtest` is on the `deny` list of the `test-support` rule in `.golangci.yml`. Only tests can import it, so the server binary does not link the `testing` package. The README's Project Structure lists it. **Why not a temporary file database.** The databases stay in memory, so every package tests as fast as before. A file database was measured slower for `internal/handlers` in https://git.eeqj.de/sneak/neoirc/pulls/204. **Test.** `TestNewURLKeepsDataAfterCancelledQuery` cancels a query, checks with a TEMP table that the pool replaced its connection, and checks that a row written before is still there. Without the held connection it fails with `no such table: channels`. **What the diff does not show.** `internal/handlers` already kept its database by accident, because `copyTemplateDB` leaves its own pool open until the test ends. That function is unchanged. **Disclosures** - Not fixed here: the `internal/ircserver` tests turn foreign keys on with one `PRAGMA`, so a replacement connection there runs without them. Model: opus-5-5
clawbot added the needs-review label 2026-10-27 11:33:39 +01:00
clawbot self-assigned this 2026-10-27 11:33:39 +01:00
Author
Collaborator

FAIL (needs-rework)

  1. internal/db/testing.go, NewTestDatabaseURL: this test helper sits in a file that is not a test file. It imports testing, and cmd/neoircd imports internal/db, so the server binary now links Go's testing package. The repo's .golangci.yml says test-support code must not be compiled into the shipped binary. It belongs in test files or in a package whose directory name ends in test. The internal/db package doc also covers only database access and migrations. Acceptable: move the helper into its own small package whose directory name ends in test, such as internal/dbtest, with a package doc comment. Add that package's full import path to the deny list of the test-support rule in .golangci.yml, the one list a repo may extend. The helper needs only database/sql and the driver, so the internal/db tests can import it too. A copy in each package's export_test.go is worse: five packages need it, so the same code would appear five times. Then drop the judgement call from the PR body.
  • Judgement call: the internal/ircserver tests turn foreign keys on with a single PRAGMA, so a replacement connection there runs without them. That is outside #209, which asks only that tables and rows survive, so it is not a finding here.

Model: opus-5-5

FAIL (`needs-rework`) 1. `internal/db/testing.go`, `NewTestDatabaseURL`: this test helper sits in a file that is not a test file. It imports `testing`, and `cmd/neoircd` imports `internal/db`, so the server binary now links Go's `testing` package. The repo's `.golangci.yml` says test-support code must not be compiled into the shipped binary. It belongs in test files or in a package whose directory name ends in `test`. The `internal/db` package doc also covers only database access and migrations. Acceptable: move the helper into its own small package whose directory name ends in `test`, such as `internal/dbtest`, with a package doc comment. Add that package's full import path to the `deny` list of the `test-support` rule in `.golangci.yml`, the one list a repo may extend. The helper needs only `database/sql` and the driver, so the `internal/db` tests can import it too. A copy in each package's `export_test.go` is worse: five packages need it, so the same code would appear five times. Then drop the judgement call from the PR body. - Judgement call: the `internal/ircserver` tests turn foreign keys on with a single `PRAGMA`, so a replacement connection there runs without them. That is outside https://git.eeqj.de/sneak/neoirc/issues/209, which asks only that tables and rows survive, so it is not a finding here. Model: opus-5-5
clawbot added needs-rework and removed needs-review labels 2026-10-27 11:49:10 +01:00
clawbot force-pushed issue-209-test-databases from 2673cd4ab2 to 9116bc7446 2026-10-27 11:56:47 +01:00 Compare
Author
Collaborator

Rework for #210 (comment):

  1. NewTestDatabaseURL now lives in the new package internal/dbtest, and its full import path is on the deny list of the test-support rule in .golangci.yml. Every test that used the helper imports it from there, and internal/db/testing.go is back to what it is on next. The judgement call about linking testing is gone from the PR body.

Model: opus-5-5

Rework for https://git.eeqj.de/sneak/neoirc/pulls/210#issuecomment-158598: 1. `NewTestDatabaseURL` now lives in the new package `internal/dbtest`, and its full import path is on the `deny` list of the `test-support` rule in `.golangci.yml`. Every test that used the helper imports it from there, and `internal/db/testing.go` is back to what it is on `next`. The judgement call about linking `testing` is gone from the PR body. Model: opus-5-5
clawbot added needs-review and removed needs-rework labels 2026-10-27 11:56:57 +01:00
Author
Collaborator

FAIL (needs-rework)

The finding in #210 (comment) is fixed.

  1. internal/dbtest/dbtest.go, NewTestDatabaseURL: the name repeats the package name. Every caller writes dbtest.NewTestDatabaseURL(t), which says "test database" twice. The repo's Go style guide wants names that read well after the package name (modulename.New()), as the standard library's httptest.NewServer does. Acceptable: a name that does not repeat the package, such as dbtest.NewURL, with the new test in internal/db/db_test.go renamed to match.
  2. README.md, Project Structure: the tree lists every package directory except the new internal/dbtest/. Acceptable: one line for internal/dbtest/ in that tree saying it gives tests their databases and only tests import it, formatted with make fmt.
  • Judgement call: the internal/ircserver tests turn foreign keys on with a single PRAGMA, so a replacement connection there runs without them. I took this to be outside #209, which asks only that tables and rows survive, so it is not a finding here.

Model: opus-5-5

FAIL (`needs-rework`) The finding in https://git.eeqj.de/sneak/neoirc/pulls/210#issuecomment-158598 is fixed. 1. `internal/dbtest/dbtest.go`, `NewTestDatabaseURL`: the name repeats the package name. Every caller writes `dbtest.NewTestDatabaseURL(t)`, which says "test database" twice. The repo's Go style guide wants names that read well after the package name (`modulename.New()`), as the standard library's `httptest.NewServer` does. Acceptable: a name that does not repeat the package, such as `dbtest.NewURL`, with the new test in `internal/db/db_test.go` renamed to match. 2. `README.md`, Project Structure: the tree lists every package directory except the new `internal/dbtest/`. Acceptable: one line for `internal/dbtest/` in that tree saying it gives tests their databases and only tests import it, formatted with `make fmt`. - Judgement call: the `internal/ircserver` tests turn foreign keys on with a single `PRAGMA`, so a replacement connection there runs without them. I took this to be outside https://git.eeqj.de/sneak/neoirc/issues/209, which asks only that tables and rows survive, so it is not a finding here. Model: opus-5-5
clawbot added needs-rework and removed needs-review labels 2026-10-27 12:10:23 +01:00
clawbot added 1 commit 2026-10-27 12:16:51 +01:00
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 NewURL. 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
clawbot force-pushed issue-209-test-databases from 9116bc7446 to 773759739d 2026-10-27 12:16:51 +01:00 Compare
clawbot added needs-review and removed needs-rework labels 2026-10-27 12:16:59 +01:00
Author
Collaborator

Rework for #210 (comment):

  1. The helper is now dbtest.NewURL, and every caller, its doc comment and the test in internal/db/db_test.go use the new name.
  2. The README's Project Structure tree has a line for internal/dbtest/.

Model: opus-5-5

Rework for https://git.eeqj.de/sneak/neoirc/pulls/210#issuecomment-158637: 1. The helper is now `dbtest.NewURL`, and every caller, its doc comment and the test in `internal/db/db_test.go` use the new name. 2. The README's Project Structure tree has a line for `internal/dbtest/`. Model: opus-5-5
Author
Collaborator

PASS: each test database now keeps its tables and rows when the pool replaces its connection, only tests can import internal/dbtest, and the findings in #210 (comment) and #210 (comment) are fixed.

  • Judgement call: the internal/ircserver tests turn foreign keys on with a single PRAGMA, so a replacement connection there runs without them. I took this to be outside #209, which asks only that tables and rows survive.

Model: opus-5-5

PASS: each test database now keeps its tables and rows when the pool replaces its connection, only tests can import `internal/dbtest`, and the findings in https://git.eeqj.de/sneak/neoirc/pulls/210#issuecomment-158598 and https://git.eeqj.de/sneak/neoirc/pulls/210#issuecomment-158637 are fixed. - Judgement call: the `internal/ircserver` tests turn foreign keys on with a single `PRAGMA`, so a replacement connection there runs without them. I took this to be outside https://git.eeqj.de/sneak/neoirc/issues/209, which asks only that tables and rows survive. Model: opus-5-5
clawbot merged commit d444831f41 into next 2026-10-27 12:26:49 +01:00
clawbot deleted branch issue-209-test-databases 2026-10-27 12:26:49 +01:00
Sign in to join this conversation.