weak_signal_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // weak_signal_test.go - ciclo de vida de la alerta de señal débil (#1452).
  2. package alerts
  3. import "testing"
  4. func intPtr(v int) *int { return &v }
  5. func TestWeakSignalStepFiresOncePerEpisode(t *testing.T) {
  6. var st WeakSignalState
  7. thr, hyst, samples := -70, 3, 3
  8. fire, resolve, _ := WeakSignalStep(&st, "aa", intPtr(-80), true, thr, hyst, samples, 1000)
  9. if fire == "" {
  10. t.Fatal("debe disparar con señal bajo el umbral")
  11. }
  12. if st.OpenID != fire {
  13. t.Fatalf("el episodio abierto debe ser el disparado: %q vs %q", st.OpenID, fire)
  14. }
  15. // Segunda muestra mala: sin re-disparo (un episodio = una alerta).
  16. fire, resolve, _ = WeakSignalStep(&st, "aa", intPtr(-78), true, thr, hyst, samples, 2000)
  17. if fire != "" || resolve != "" {
  18. t.Fatal("con el episodio abierto no hay ni disparo ni cierre")
  19. }
  20. }
  21. func TestWeakSignalStepResolvesAfterNGoodSamples(t *testing.T) {
  22. var st WeakSignalState
  23. thr, hyst, samples := -70, 3, 3
  24. fire, _, _ := WeakSignalStep(&st, "aa", intPtr(-80), true, thr, hyst, samples, 1000)
  25. // -66 >= -70+3: buena. Dos muestras no cierran (N=3).
  26. for i := 0; i < 2; i++ {
  27. f, r, _ := WeakSignalStep(&st, "aa", intPtr(-66), true, thr, hyst, samples, int64(2000+i))
  28. if f != "" || r != "" {
  29. t.Fatalf("muestra %d: no debe cerrar aún", i+1)
  30. }
  31. }
  32. f, r, start := WeakSignalStep(&st, "aa", intPtr(-65), true, thr, hyst, samples, 3000)
  33. if f != "" || r != fire {
  34. t.Fatalf("la tercera muestra buena cierra el episodio: fire=%q resolve=%q", f, r)
  35. }
  36. if start != 1000 {
  37. t.Fatalf("el ts de apertura del episodio es %d, quiero 1000", start)
  38. }
  39. if st.OpenID != "" || st.Good != 0 {
  40. t.Fatal("tras cerrar, el episodio queda cerrado y la racha a cero")
  41. }
  42. // #1521: LastFire se conserva al cerrar (el guarda de 24 h es entre
  43. // EPISODIOS, no se anula con la resolución).
  44. if st.LastFire != 1000 {
  45. t.Fatalf("LastFire tras cerrar = %d, quiero 1000 (el disparo original)", st.LastFire)
  46. }
  47. }
  48. func TestWeakSignalStepDeadZoneBreaksStreak(t *testing.T) {
  49. var st WeakSignalState
  50. thr, hyst, samples := -70, 3, 3
  51. fire, _, _ := WeakSignalStep(&st, "aa", intPtr(-80), true, thr, hyst, samples, 1000)
  52. _ = fire
  53. // Dos buenas, una en zona muerta (-69: >= thr pero < thr+hyst), una buena.
  54. WeakSignalStep(&st, "aa", intPtr(-66), true, thr, hyst, samples, 2000)
  55. WeakSignalStep(&st, "aa", intPtr(-66), true, thr, hyst, samples, 3000)
  56. WeakSignalStep(&st, "aa", intPtr(-69), true, thr, hyst, samples, 4000)
  57. f, r, _ := WeakSignalStep(&st, "aa", intPtr(-66), true, thr, hyst, samples, 5000)
  58. if f != "" || r != "" {
  59. t.Fatal("la zona muerta rompe la racha: una buena tras ella no cierra")
  60. }
  61. // Dos buenas más sí cierran (racha nueva de 3).
  62. WeakSignalStep(&st, "aa", intPtr(-66), true, thr, hyst, samples, 6000)
  63. f, r, _ = WeakSignalStep(&st, "aa", intPtr(-66), true, thr, hyst, samples, 7000)
  64. if r == "" {
  65. t.Fatal("tras recomponer la racha de 3 buenas, el episodio cierra")
  66. }
  67. }
  68. func TestWeakSignalStepNoDataNeverResolves(t *testing.T) {
  69. var st WeakSignalState
  70. thr, hyst, samples := -70, 3, 2
  71. fire, _, _ := WeakSignalStep(&st, "aa", intPtr(-80), true, thr, hyst, samples, 1000)
  72. _ = fire
  73. // Offline: no cierra ni rompe la racha de buenas acumulada.
  74. WeakSignalStep(&st, "aa", intPtr(-66), true, thr, hyst, samples, 2000)
  75. WeakSignalStep(&st, "aa", nil, false, thr, hyst, samples, 3000)
  76. WeakSignalStep(&st, "aa", nil, true, thr, hyst, samples, 4000)
  77. if st.Good != 1 {
  78. t.Fatalf("sin datos no debe tocar la racha: good=%d", st.Good)
  79. }
  80. f, r, _ := WeakSignalStep(&st, "aa", intPtr(-66), true, thr, hyst, samples, 5000)
  81. if f != "" || r != fire {
  82. t.Fatal("la racha continúa tras el hueco sin datos y cierra en la 2ª buena")
  83. }
  84. }
  85. func TestWeakSignalStepRefireGuard(t *testing.T) {
  86. var st WeakSignalState
  87. thr, hyst, samples := -70, 3, 1
  88. f, _, _ := WeakSignalStep(&st, "aa", intPtr(-80), true, thr, hyst, samples, 1000)
  89. if f == "" {
  90. t.Fatal("primer episodio dispara")
  91. }
  92. // Cierra al instante (N=1).
  93. _, r, _ := WeakSignalStep(&st, "aa", intPtr(-66), true, thr, hyst, samples, 2000)
  94. if r == "" {
  95. t.Fatal("cierra con una buena cuando N=1")
  96. }
  97. // Caída inmediata de nuevo (#1521): DENTRO del guarda de 24 h desde el
  98. // último disparo real. No abre episodio (un cliente oscilando no vuelve a
  99. // alertar el mismo día, aunque haya resuelto entremedias).
  100. f, r, _ = WeakSignalStep(&st, "aa", intPtr(-81), true, thr, hyst, samples, 3000)
  101. if f != "" || r != "" {
  102. t.Fatalf("re-disparo a los 2 s de cerrar: fire=%q resolve=%q, esperaba nada", f, r)
  103. }
  104. // Pasado el guarda (24 h desde el disparo de 1000), vuelve a alertar.
  105. f, _, _ = WeakSignalStep(&st, "aa", intPtr(-81), true, thr, hyst, samples, 1000+weakRefireGapMs+1)
  106. if f == "" {
  107. t.Fatal("tras 24 h del último disparo, un episodio nuevo dispara")
  108. }
  109. }
  110. func TestWeakSignalStepDefaultGetters(t *testing.T) {
  111. if got := WeakSignalHysteresisDbm(nil); got != DefaultWeakSignalHysteresisDbm {
  112. t.Fatalf("default histéresis: %d", got)
  113. }
  114. if got := WeakSignalResolveSamples(nil); got != DefaultWeakSignalResolveSamples {
  115. t.Fatalf("default muestras: %d", got)
  116. }
  117. if WeakSignalDbm(nil) != DefaultWeakSignalDbm {
  118. t.Fatal("default dbm")
  119. }
  120. }