server_cold_status_test.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. package service
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database"
  6. )
  7. // A panel restarts with an empty snapshot until the @2s ticker fires, and a
  8. // master probing that window reads the empty answer as an offline node.
  9. func TestCurrentStatusSamplesBeforeFirstTick(t *testing.T) {
  10. dbDir := t.TempDir()
  11. t.Setenv("XUI_DB_FOLDER", dbDir)
  12. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  13. t.Fatalf("InitDB: %v", err)
  14. }
  15. t.Cleanup(func() { _ = database.CloseDB() })
  16. svc := &ServerService{}
  17. if svc.LastStatus() != nil {
  18. t.Fatal("a fresh ServerService should hold no snapshot yet")
  19. }
  20. status := svc.CurrentStatus()
  21. if status == nil {
  22. t.Fatal("CurrentStatus returned nil before the first ticker run, want an on-demand sample")
  23. }
  24. if svc.LastStatus() != status {
  25. t.Fatal("the on-demand sample should be stored as LastStatus")
  26. }
  27. if again := svc.CurrentStatus(); again != status {
  28. t.Fatal("a warm CurrentStatus should reuse the stored snapshot, not resample")
  29. }
  30. }