commit 76432c77ab9b0e4209ffb43244c2c41e7e37be7c
parent 136e50147c86c416eec98087f3ec00b6f40c6ce5
Author: anon <anon@example.com>
Date: Fri, 6 Nov 2026 04:56:55 +0000
Batch database initialization and remove the logout test wait
Diffstat:
4 files changed, 70 insertions(+), 27 deletions(-)
diff --git a/TODO.md b/TODO.md
@@ -386,28 +386,6 @@ rewriting this document does not close them.
grids remain separate from original scenery. Corpse expiry and respawn tests
now reach and pass their assertions; related roaming, checkpoint, teleport
and fixture-isolation regressions pass as well.
-- [ ] **Measure server test costs and remove obvious inefficiencies.**
- The reported full run takes tens of minutes, but `-v` shows progress; quiet
- output alone does not establish a hang. Establish a baseline before setting
- a runtime target or changing which tests run by default.
- Measure compile/link time and binary size, package initialization, and test
- execution separately before attributing the delay to embedding alone.
- `gamedata` also embeds about 145 MB of definitions and eagerly loads/validates
- catalogs in `init`; default fixtures call production server initialization.
- Use per-test timings and focused profiling to identify dominant costs, such as
- repeated full-world/catalog setup, redundant database work or unnecessary
- waits. Fix measured waste with small changes while preserving assertions,
- fixture isolation and default coverage. Consider a separate expensive suite
- only if the measurements justify that tradeoff; do not move tests merely to
- meet an arbitrary time budget. Browser scenarios remain opt-in.
- Completion: record the baseline, dominant costs, focused fixes and comparable
- before/after timings on the same machine with the same test selection and
- cache conditions. Use `-count=1` to avoid cached test results and report cold
- builds separately. If no straightforward improvement is found, record that
- result and any justified follow-up rather than forcing a broad rewrite.
- Explain standard Go output behavior:
- plain `go test .` reports passing package results at completion; `-v` enables
- per-test output once compilation and package initialization finish.
- [ ] **Separate browser scenarios from Go internals through a test-only API.**
Put browser scenarios and end-to-end assertions in JavaScript under
`tests/e2e/`, reusing `tools/client/game_test.mjs`. A shared Playwright fixture
diff --git a/server/database.go b/server/database.go
@@ -33,11 +33,21 @@ func (server *server) database() (*sql.DB, error) {
db := sql.OpenDB(databaseConnector{&server.databaseQueries})
// One connection orders chat and world transactions; WAL permits external readers.
db.SetMaxOpenConns(1)
- _, err = db.Exec(worldSchema)
+ // Initialize the schema and world row with one durable commit.
+ tx, err := db.Begin()
+ if err != nil {
+ db.Close()
+ return nil, err
+ }
+ _, err = tx.Exec(worldSchema)
+ if err == nil {
+ _, err = tx.Exec("INSERT OR IGNORE INTO world (id, clock_started) VALUES (1, ?)", time.Now().UTC().Format(time.RFC3339Nano))
+ }
if err == nil {
- _, err = db.Exec("INSERT OR IGNORE INTO world (id, clock_started) VALUES (1, ?)", time.Now().UTC().Format(time.RFC3339Nano))
+ err = tx.Commit()
}
if err != nil {
+ tx.Rollback()
db.Close()
return nil, err
}
diff --git a/server/database_test.go b/server/database_test.go
@@ -1,6 +1,7 @@
package main
import (
+ "database/sql"
"encoding/json"
"fmt"
"net/http"
@@ -13,6 +14,37 @@ import (
"go.yaml.in/yaml/v3"
)
+func TestWorldDatabaseInitializationRollsBackAndRetries(t *testing.T) {
+ t.Chdir(t.TempDir())
+ world := &server{}
+ registerTestDatabaseCleanup(t, world)
+ originalSchema := worldSchema
+ t.Cleanup(func() { worldSchema = originalSchema })
+ worldSchema += "\nCREATE TRIGGER reject_world BEFORE INSERT ON world BEGIN SELECT RAISE(ABORT, 'reject initialization'); END;"
+
+ // Failure to create the initial world row must also undo the schema.
+ if db, err := world.database(); err == nil || db != nil || world.db != nil {
+ t.Fatal("failed initialization published a database", db, err)
+ }
+ probe := sql.OpenDB(databaseConnector{&world.databaseQueries})
+ t.Cleanup(func() { probe.Close() })
+ var tables int
+ if err := probe.QueryRow("SELECT count(*) FROM sqlite_master WHERE type='table'").Scan(&tables); err != nil || tables != 0 {
+ t.Fatal("failed initialization retained partial schema", tables, err)
+ }
+
+ // Correcting the cause permits a complete initialization in the same file.
+ worldSchema = originalSchema
+ db, err := world.database()
+ if err != nil {
+ t.Fatal(err)
+ }
+ var rows int
+ if err := db.QueryRow("SELECT count(*) FROM world WHERE id=1").Scan(&rows); err != nil || rows != 1 {
+ t.Fatal("retry did not create the initial world row", rows, err)
+ }
+}
+
func readTestPlayerRecord(id uint32, name string) ([]byte, error) {
world := &server{}
db, err := world.database()
diff --git a/server/main_test.go b/server/main_test.go
@@ -261,11 +261,12 @@ func TestLogoutCompletesAfterCountdown(t *testing.T) {
world := newTestServer(t)
world.newCharacter()
client, connection := net.Pipe()
+ serverConn := &logoutDeadlineConn{Conn: connection, deadlines: make(chan time.Time, 1)}
defer client.Close()
- client.SetDeadline(time.Now().Add(35 * time.Second))
+ client.SetDeadline(time.Now().Add(5 * time.Second))
done := make(chan error, 1)
go func() {
- err := world.serveWorldConnection(connection)
+ err := world.serveWorldConnection(serverConn)
connection.Close()
done <- err
}()
@@ -275,7 +276,29 @@ func TestLogoutCompletesAfterCountdown(t *testing.T) {
logout, _ := hex.DecodeString("04a9000a0001002e000000010000000200000024789c8b732f2f6060381cc0c0c0c0c80001150c6800005d4a03190000")
client.Write(logout)
- _, err := readPacket(client)
+ select {
+ case deadline := <-serverConn.deadlines:
+ if remaining := time.Until(deadline); remaining < 29*time.Second || remaining > 30*time.Second {
+ t.Fatal("logout did not schedule the full countdown", remaining)
+ }
+ case <-time.After(time.Second):
+ t.Fatal("logout did not set a disconnect deadline")
+ }
+
+ // The session stays usable before expiry; expire the pipe deadline explicitly.
+ ping := systemPacket(1, nil)
+ binary.BigEndian.PutUint16(ping[2:4], 11)
+ if _, err := client.Write(ping); err != nil {
+ t.Fatal(err)
+ }
+ pong, err := readPacket(client)
+ if err != nil || binary.BigEndian.Uint32(pong[16:20]) != 2 {
+ t.Fatal("logout disconnected before its deadline", err)
+ }
+ if err := connection.SetReadDeadline(time.Now()); err != nil {
+ t.Fatal(err)
+ }
+ _, err = readPacket(client)
serverError := <-done
if err != io.EOF || serverError != nil {