mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-09-30 19:22:10 +03:00
12d51d7195
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.
77 lines
2.4 KiB
Go
77 lines
2.4 KiB
Go
package service
|
|
|
|
import (
|
|
"path/filepath"
|
|
"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"
|
|
)
|
|
|
|
func TestSetInboundSubSortIndexLeavesSettingsUntouched(t *testing.T) {
|
|
dbtest.InitDB(t, filepath.Join(t.TempDir(), "x-ui.db"))
|
|
|
|
const settings = `{"clients":[{"email":"a@example.test","id":"11111111-1111-1111-1111-111111111111"}]}`
|
|
ib := &model.Inbound{UserId: 1, Remark: "r", Port: 21001, Protocol: model.VLESS, Settings: settings, SubSortIndex: 1, Enable: true}
|
|
if err := database.GetDB().Create(ib).Error; err != nil {
|
|
t.Fatalf("seed: %v", err)
|
|
}
|
|
|
|
svc := InboundService{}
|
|
if err := svc.SetInboundSubSortIndex(ib.Id, 7); err != nil {
|
|
t.Fatalf("set: %v", err)
|
|
}
|
|
|
|
var got model.Inbound
|
|
if err := database.GetDB().First(&got, ib.Id).Error; err != nil {
|
|
t.Fatalf("reload: %v", err)
|
|
}
|
|
if got.SubSortIndex != 7 {
|
|
t.Fatalf("subSortIndex = %d, want 7", got.SubSortIndex)
|
|
}
|
|
if got.Settings != settings {
|
|
t.Fatalf("settings were rewritten:\n got %s\nwant %s", got.Settings, settings)
|
|
}
|
|
}
|
|
|
|
func TestSetInboundSubSortIndexUsesNarrowNodeUpdate(t *testing.T) {
|
|
setupBulkDB(t)
|
|
nodeID, fake := setupNodeRuntime(t)
|
|
ib := nodeInbound(t, nodeID, 21002, []model.Client{{Email: "a@example.test", ID: "11111111-1111-1111-1111-111111111111"}})
|
|
ib.SubSortIndex = 1
|
|
if err := database.GetDB().Model(ib).Update("sub_sort_index", 1).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := (&InboundService{}).SetInboundSubSortIndex(ib.Id, 7); err != nil {
|
|
t.Fatalf("set: %v", err)
|
|
}
|
|
if got := fake.updateSubSort.Load(); got != 1 {
|
|
t.Fatalf("narrow node updates = %d, want 1", got)
|
|
}
|
|
if got := fake.updateInbound.Load(); got != 0 {
|
|
t.Fatalf("full snapshot node updates = %d, want 0", got)
|
|
}
|
|
}
|
|
|
|
func TestSetInboundSubSortIndexPreservesNegative(t *testing.T) {
|
|
dbtest.InitDB(t, filepath.Join(t.TempDir(), "x-ui.db"))
|
|
|
|
ib := &model.Inbound{UserId: 1, Remark: "r", Port: 21003, Protocol: model.VLESS, Settings: `{"clients":[]}`, SubSortIndex: 1, Enable: true}
|
|
if err := database.GetDB().Create(ib).Error; err != nil {
|
|
t.Fatalf("seed: %v", err)
|
|
}
|
|
|
|
if err := (&InboundService{}).SetInboundSubSortIndex(ib.Id, -2); err != nil {
|
|
t.Fatalf("set: %v", err)
|
|
}
|
|
|
|
var got model.Inbound
|
|
if err := database.GetDB().First(&got, ib.Id).Error; err != nil {
|
|
t.Fatalf("reload: %v", err)
|
|
}
|
|
if got.SubSortIndex != -2 {
|
|
t.Fatalf("subSortIndex = %d, want -2", got.SubSortIndex)
|
|
}
|
|
}
|