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:
MHSanaei
2026-09-27 03:04:50 +02:00
parent 33a469315a
commit 12d51d7195
82 changed files with 289 additions and 451 deletions
@@ -7,6 +7,7 @@ import (
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database"
"github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"github.com/mymmrac/telego"
@@ -15,10 +16,7 @@ import (
func seedPickerInbounds(t *testing.T, protocols ...model.Protocol) {
t.Helper()
if err := database.InitDB(filepath.Join(t.TempDir(), "x-ui.db")); err != nil {
t.Fatalf("InitDB: %v", err)
}
t.Cleanup(func() { _ = database.CloseDB() })
dbtest.InitDB(t, filepath.Join(t.TempDir(), "x-ui.db"))
for i, protocol := range protocols {
port := 20000 + i
ib := &model.Inbound{Remark: string(protocol), Enable: true, Port: port, Protocol: protocol, Tag: fmt.Sprintf("inbound-%d", port), Settings: `{}`}
@@ -4,7 +4,7 @@ import (
"path/filepath"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database"
"github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
"github.com/mymmrac/telego"
)
@@ -12,10 +12,7 @@ import (
// Regression test: keying the add-client wizard by chat alone left the two
// admins of a group chat filling in one client between them.
func TestAddClientDraftIsPerAdminInGroupChat(t *testing.T) {
if err := database.InitDB(filepath.Join(t.TempDir(), "x-ui.db")); err != nil {
t.Fatalf("InitDB: %v", err)
}
t.Cleanup(func() { _ = database.CloseDB() })
dbtest.InitDB(t, filepath.Join(t.TempDir(), "x-ui.db"))
const (
groupChat = int64(-1001234567890)
@@ -11,7 +11,7 @@ import (
"testing"
"time"
"github.com/mhsanaei/3x-ui/v3/internal/database"
"github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
"github.com/mymmrac/telego"
)
@@ -84,10 +84,7 @@ func lastDraftCard(t *testing.T, texts []string) string {
// Regression test: one package-level draft per bot meant an admin's new client
// was filled in by another chat's steps.
func TestAddClientDraftIsPerChat(t *testing.T) {
if err := database.InitDB(filepath.Join(t.TempDir(), "x-ui.db")); err != nil {
t.Fatalf("InitDB: %v", err)
}
t.Cleanup(func() { _ = database.CloseDB() })
dbtest.InitDB(t, filepath.Join(t.TempDir(), "x-ui.db"))
const (
chatA = int64(7101)
@@ -6,7 +6,7 @@ import (
"strings"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database"
"github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
"github.com/mhsanaei/3x-ui/v3/internal/web/locale"
"github.com/mhsanaei/3x-ui/v3/internal/xray"
@@ -35,10 +35,7 @@ func clientInfoLocalizer(t *testing.T) {
// Regression test: a start-after-first-use client is stored as a negative duration,
// and a disabled one rendered it as a 1969 date.
func TestClientInfoShowsStartAfterFirstUseWhenDisabled(t *testing.T) {
if err := database.InitDB(filepath.Join(t.TempDir(), "x-ui.db")); err != nil {
t.Fatalf("InitDB: %v", err)
}
t.Cleanup(func() { _ = database.CloseDB() })
dbtest.InitDB(t, filepath.Join(t.TempDir(), "x-ui.db"))
clientInfoLocalizer(t)
traffic := &xray.ClientTraffic{
@@ -6,6 +6,7 @@ import (
"time"
"github.com/mhsanaei/3x-ui/v3/internal/database"
"github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"github.com/mhsanaei/3x-ui/v3/internal/web/global"
"github.com/mhsanaei/3x-ui/v3/internal/xray"
@@ -33,10 +34,7 @@ func newLinksCallbackTgbot(t *testing.T, email string) (*Tgbot, func(string) int
swapTestBot(t, mock.URL)
t.Cleanup(mock.Close)
if err := database.InitDB(filepath.Join(t.TempDir(), "x-ui.db")); err != nil {
t.Fatalf("InitDB: %v", err)
}
t.Cleanup(func() { _ = database.CloseDB() })
dbtest.InitDB(t, filepath.Join(t.TempDir(), "x-ui.db"))
inbound := &model.Inbound{
UserId: 1,
@@ -12,6 +12,7 @@ import (
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database"
"github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"github.com/mhsanaei/3x-ui/v3/internal/xray"
@@ -59,10 +60,7 @@ func seedReportClients(t *testing.T, remark string, emails []string) {
func initReportDB(t *testing.T) *Tgbot {
t.Helper()
if err := database.InitDB(filepath.Join(t.TempDir(), "x-ui.db")); err != nil {
t.Fatalf("InitDB: %v", err)
}
t.Cleanup(func() { _ = database.CloseDB() })
dbtest.InitDB(t, filepath.Join(t.TempDir(), "x-ui.db"))
origRunning := isRunning
t.Cleanup(func() { isRunning = origRunning })
@@ -8,7 +8,7 @@ import (
"sync"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database"
"github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"github.com/mymmrac/telego"
@@ -58,10 +58,7 @@ func swapTestBot(t *testing.T, url string) {
func newStaleButtonTgbot(t *testing.T) *Tgbot {
t.Helper()
if err := database.InitDB(filepath.Join(t.TempDir(), "x-ui.db")); err != nil {
t.Fatalf("InitDB: %v", err)
}
t.Cleanup(func() { _ = database.CloseDB() })
dbtest.InitDB(t, filepath.Join(t.TempDir(), "x-ui.db"))
return &Tgbot{}
}