mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-10-01 03:32:09 +03:00
perf(tests): copy a migrated template DB instead of migrating per test
Most tests opened a throwaway panel DB with database.InitDB, which runs the full AutoMigrate + seed on an empty file every time: ~230ms, and ~850ms under -race because GORM's reflection-heavy migration is what the detector slows most. internal/web/service does this in ~550 of its 830 tests, so the CI race job spent ~10 of its ~14.6 minutes re-migrating empty databases. internal/database/dbtest.InitDB migrates once per test process, then hands each test its own copy of that file (~130ms under -race) and registers the CloseDB cleanup. The copy then goes through InitDB like a panel restart, so every test still starts from the state a fresh install has. Tests that reopen an existing file, migrate a hand-built legacy DB or target Postgres keep calling database.InitDB. Locally under -race: internal/web/service 626s (last CI run) -> 114s, internal/sub 246s -> 35s.
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
// Package dbtest opens throwaway panel databases for tests. Migrating a new
|
||||
// SQLite file costs ~850ms under -race; copying a migrated template ~130ms.
|
||||
package dbtest
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/config"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
||||
)
|
||||
|
||||
var migrated struct {
|
||||
once sync.Once
|
||||
data []byte
|
||||
err error
|
||||
}
|
||||
|
||||
// InitDB opens a new, fully migrated panel database at path and closes it when
|
||||
// t ends. Reopen an existing file with database.InitDB instead.
|
||||
func InitDB(t testing.TB, path string) {
|
||||
t.Helper()
|
||||
if config.GetDBKind() != "postgres" {
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
t.Fatalf("dbtest.InitDB would overwrite existing %s; reopen it with database.InitDB", path)
|
||||
}
|
||||
data, err := migratedTemplate()
|
||||
if err != nil {
|
||||
t.Fatalf("build template database: %v", err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
||||
t.Fatalf("create database dir: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(path, data, 0o600); err != nil {
|
||||
t.Fatalf("copy template database: %v", err)
|
||||
}
|
||||
}
|
||||
if err := database.InitDB(path); err != nil {
|
||||
t.Fatalf("InitDB: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = database.CloseDB() })
|
||||
}
|
||||
|
||||
func migratedTemplate() ([]byte, error) {
|
||||
migrated.once.Do(func() {
|
||||
dir, err := os.MkdirTemp("", "xui-dbtest-")
|
||||
if err != nil {
|
||||
migrated.err = err
|
||||
return
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
path := filepath.Join(dir, "template.db")
|
||||
if err := database.InitDB(path); err != nil {
|
||||
migrated.err = err
|
||||
return
|
||||
}
|
||||
// Closing the last connection checkpoints the WAL into the main file.
|
||||
if err := database.CloseDB(); err != nil {
|
||||
migrated.err = err
|
||||
return
|
||||
}
|
||||
migrated.data, migrated.err = os.ReadFile(path)
|
||||
})
|
||||
return migrated.data, migrated.err
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package dbtest
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
||||
)
|
||||
|
||||
func TestInitDBGivesEachTestItsOwnDatabase(t *testing.T) {
|
||||
t.Run("first test writes", func(t *testing.T) {
|
||||
InitDB(t, filepath.Join(t.TempDir(), "x-ui.db"))
|
||||
if err := database.GetDB().Create(&model.Setting{Key: "dbtestProbe", Value: "first"}).Error; err != nil {
|
||||
t.Fatalf("write probe: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("next test starts clean", func(t *testing.T) {
|
||||
InitDB(t, filepath.Join(t.TempDir(), "x-ui.db"))
|
||||
var leaked int64
|
||||
if err := database.GetDB().Model(&model.Setting{}).Where("key = ?", "dbtestProbe").Count(&leaked).Error; err != nil {
|
||||
t.Fatalf("count probe: %v", err)
|
||||
}
|
||||
if leaked != 0 {
|
||||
t.Fatalf("database holds %d probe rows written by the previous test; copies must not share state", leaked)
|
||||
}
|
||||
var admins int64
|
||||
if err := database.GetDB().Model(&model.User{}).Count(&admins).Error; err != nil {
|
||||
t.Fatalf("count users: %v", err)
|
||||
}
|
||||
if admins != 1 {
|
||||
t.Fatalf("users = %d, want the 1 seeded admin a fresh install has", admins)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user