server_cold_status_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. package service
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
  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. dbtest.InitDB(t, filepath.Join(dbDir, "x-ui.db"))
  13. // A settled IP cache keeps GetStatus from starting a public-IP resolver that
  14. // outlives this test and races http.DefaultTransport in later ones.
  15. svc := &ServerService{cachedIPv4: "192.0.2.1", noIPv6: true}
  16. if svc.LastStatus() != nil {
  17. t.Fatal("a fresh ServerService should hold no snapshot yet")
  18. }
  19. status := svc.CurrentStatus()
  20. if status == nil {
  21. t.Fatal("CurrentStatus returned nil before the first ticker run, want an on-demand sample")
  22. }
  23. if svc.LastStatus() != status {
  24. t.Fatal("the on-demand sample should be stored as LastStatus")
  25. }
  26. if again := svc.CurrentStatus(); again != status {
  27. t.Fatal("a warm CurrentStatus should reuse the stored snapshot, not resample")
  28. }
  29. }