1
0

xray_restart_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package service
  2. import (
  3. "testing"
  4. )
  5. // A background (non-forced) restart — the pending-config-change cron, warp/ldap/
  6. // outbound reconcile jobs — must not revive an Xray the admin deliberately
  7. // stopped. Only an explicit forced restart clears the manual-stop state.
  8. func TestRestartXrayRespectsManualStop(t *testing.T) {
  9. setupSettingTestDB(t)
  10. if err := (&SettingService{}).saveSetting("xrayTemplateConfig", "{ not valid json"); err != nil {
  11. t.Fatalf("seed template: %v", err)
  12. }
  13. t.Cleanup(func() { isManuallyStopped.Store(false) })
  14. isManuallyStopped.Store(true)
  15. _ = (&XrayService{}).RestartXray(false)
  16. if !isManuallyStopped.Load() {
  17. t.Fatal("a non-forced restart cleared a deliberate manual stop and would revive xray")
  18. }
  19. }
  20. // When the pending-restart reconcile consumes the need-restart flag but the
  21. // restart itself fails, the flag must be re-armed so the config change is
  22. // retried rather than silently dropped.
  23. func TestApplyPendingRestartReArmsFlagOnFailure(t *testing.T) {
  24. setupSettingTestDB(t)
  25. if err := (&SettingService{}).saveSetting("xrayTemplateConfig", "{ not valid json"); err != nil {
  26. t.Fatalf("seed template: %v", err)
  27. }
  28. t.Cleanup(func() {
  29. isManuallyStopped.Store(false)
  30. isNeedXrayRestart.Store(false)
  31. })
  32. isManuallyStopped.Store(false)
  33. svc := &XrayService{}
  34. svc.SetToNeedRestart()
  35. svc.ApplyPendingRestart()
  36. if !isNeedXrayRestart.Load() {
  37. t.Fatal("a failed restart must re-arm the need-restart flag so the pending config change is retried")
  38. }
  39. }