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.
57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
package service
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
|
|
)
|
|
|
|
// A box with no IPv6 route spends 3s per lookup service, and a status sample
|
|
// that waits for that is a panel reporting nothing for the first ~15s.
|
|
func TestStatusSampleDoesNotWaitOnPublicIPLookup(t *testing.T) {
|
|
dbDir := t.TempDir()
|
|
t.Setenv("XUI_DB_FOLDER", dbDir)
|
|
dbtest.InitDB(t, filepath.Join(dbDir, "x-ui.db"))
|
|
|
|
release := make(chan struct{})
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
select {
|
|
case <-release:
|
|
case <-r.Context().Done():
|
|
return
|
|
}
|
|
_, _ = w.Write([]byte("203.0.113.7"))
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
|
|
savedV4, savedV6 := publicIPv4Services, publicIPv6Services
|
|
publicIPv4Services = []string{srv.URL}
|
|
publicIPv6Services = []string{srv.URL}
|
|
t.Cleanup(func() { publicIPv4Services, publicIPv6Services = savedV4, savedV6 })
|
|
|
|
svc := &ServerService{}
|
|
status := svc.CurrentStatus()
|
|
if status == nil {
|
|
t.Fatal("CurrentStatus returned nil while the IP lookup was in flight")
|
|
}
|
|
if status.PublicIP.IPv4 != "" {
|
|
t.Fatalf("the sample waited for the lookup: PublicIP.IPv4 = %q, want it still unresolved", status.PublicIP.IPv4)
|
|
}
|
|
|
|
close(release)
|
|
deadline := time.Now().Add(10 * time.Second)
|
|
for {
|
|
if ipv4, _ := svc.publicIPs(); ipv4 == "203.0.113.7" {
|
|
return
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("the background lookup never cached the public IP")
|
|
}
|
|
time.Sleep(20 * time.Millisecond)
|
|
}
|
|
}
|