package job import ( "log" "path/filepath" "sync" "testing" "github.com/op/go-logging" "github.com/mhsanaei/3x-ui/v3/internal/database" xuilogger "github.com/mhsanaei/3x-ui/v3/internal/logger" ) // 3x-ui logger must be initialised once before any code path that can // log a warning, otherwise log.Warningf panics on a nil logger. These // helpers used to live in the CheckClientIpJob integration tests; they // moved here when that job was retired in P9(4b), because the node // heartbeat and traffic-sync integration tests share them. var loggerInitOnce sync.Once // setupIntegrationDB wires a temp sqlite db and log folder so job code // can run end to end. Closes the db before TempDir cleanup so windows // doesn't complain about the file being in use. func setupIntegrationDB(t *testing.T) { t.Helper() loggerInitOnce.Do(func() { xuilogger.InitLogger(logging.ERROR) }) dbDir := t.TempDir() logDir := t.TempDir() t.Setenv("XUI_DB_FOLDER", dbDir) t.Setenv("XUI_LOG_FOLDER", logDir) origLogWriter := log.Writer() origLogFlags := log.Flags() t.Cleanup(func() { log.SetOutput(origLogWriter) log.SetFlags(origLogFlags) }) if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil { t.Fatalf("database.InitDB failed: %v", err) } t.Cleanup(func() { if err := database.CloseDB(); err != nil { t.Logf("database.CloseDB warning: %v", err) } }) }