Give each IRC listener test its own database (closes #129)
check / check (push) Waiting to run
check / check (push) Waiting to run
Each test environment in internal/ircserver named its shared in-memory database after the test. A repeat run of a test in the same process could open the database that an earlier run's server was still holding, and start on that run's users. The database now gets a random name, and a new test checks that two environments open at once do not share one. The database close and the server stop are now registered as soon as each is opened, so a setup that fails part-way still closes its database. Model: opus-5-5
This commit is contained in:
@@ -2,6 +2,7 @@ package ircserver_test
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/rand"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
@@ -69,9 +70,11 @@ func newTestEnvWithConfig(
|
||||
) *testEnv {
|
||||
t.Helper()
|
||||
|
||||
// A random name, so that no other test environment, not
|
||||
// even an earlier run of the same test, can share it.
|
||||
dsn := fmt.Sprintf(
|
||||
"file:%s?mode=memory&cache=shared&_journal_mode=WAL",
|
||||
t.Name(),
|
||||
rand.Text(),
|
||||
)
|
||||
|
||||
conn, err := sql.Open("sqlite", dsn)
|
||||
@@ -79,6 +82,13 @@ func newTestEnvWithConfig(
|
||||
t.Fatalf("open db: %v", err)
|
||||
}
|
||||
|
||||
t.Cleanup(func() {
|
||||
err := conn.Close()
|
||||
if err != nil {
|
||||
t.Logf("close db: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
conn.SetMaxOpenConns(1)
|
||||
|
||||
_, err = conn.ExecContext(
|
||||
@@ -123,14 +133,9 @@ func newTestEnvWithConfig(
|
||||
t.Fatalf("start irc server: %v", err)
|
||||
}
|
||||
|
||||
t.Cleanup(func() {
|
||||
srv.Stop()
|
||||
|
||||
err := conn.Close()
|
||||
if err != nil {
|
||||
t.Logf("close db: %v", err)
|
||||
}
|
||||
})
|
||||
// Cleanups run last registered first, so the server stops
|
||||
// before its database is closed.
|
||||
t.Cleanup(srv.Stop)
|
||||
|
||||
return &testEnv{
|
||||
database: database,
|
||||
@@ -342,6 +347,20 @@ func TestRegistration(t *testing.T) {
|
||||
assertContains(t, lines, " 001 ", "RPL_WELCOME")
|
||||
}
|
||||
|
||||
// TestEachTestEnvHasItsOwnDatabase checks that a test
|
||||
// environment starts on an empty database while another one
|
||||
// is still open.
|
||||
func TestEachTestEnvHasItsOwnDatabase(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
first := newTestEnv(t)
|
||||
first.dial(t).register("samenick")
|
||||
|
||||
second := newTestEnv(t)
|
||||
lines := second.dial(t).register("samenick")
|
||||
assertContains(t, lines, " 001 ", "RPL_WELCOME")
|
||||
}
|
||||
|
||||
func TestWelcomeContainsNick(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user