scan_throttle_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package probe
  2. // scan_throttle_test.go — #591: el scan pasivo de vecinos (`iw dev scan`)
  3. // saca la radio del canal y degrada a los clientes si corre en cada push
  4. // (~30 s). No debe repetirse dentro de scanMinInterval salvo que el server
  5. // pida un refresh explícito (ForceScan).
  6. import (
  7. "context"
  8. "strings"
  9. "testing"
  10. "time"
  11. )
  12. // countingRunner cuenta los scans por interfaz (#1338: antes era UNA llamada
  13. // a CmdScan; ahora es un `iw dev <iface> scan` por interfaz) sobre un
  14. // fakeRunner.
  15. type countingRunner struct {
  16. fakeRunner
  17. scanCalls int
  18. // calls cuenta por comando exacto (#1450: comprobar que el volcado de
  19. // `uci show wireless` se cachea y no se relanza). nil = no se cuenta.
  20. calls map[string]int
  21. }
  22. func (r *countingRunner) Run(ctx context.Context, cmd string, timeout time.Duration) (string, error) {
  23. if strings.HasPrefix(cmd, `iw dev "`) && strings.HasSuffix(cmd, `" scan 2>/dev/null`) {
  24. r.scanCalls++
  25. }
  26. if r.calls != nil {
  27. r.calls[cmd]++
  28. }
  29. return r.fakeRunner.Run(ctx, cmd, timeout)
  30. }
  31. // newScanProber devuelve un Prober cuyo runner solo responde a `iw dev` (una
  32. // interfaz wlan0) y a su scan (el resto de sondas falla y Build sigue sin
  33. // radios: suficiente para probar el throttle).
  34. func newScanProber() (*Prober, *countingRunner) {
  35. run := &countingRunner{fakeRunner: fakeRunner{outs: map[string]string{
  36. CmdIwDev: "phy#0\n\tInterface wlan0\n\t\taddr 00:11:22:33:44:66\n\t\ttype AP\n",
  37. CmdScanIface("wlan0"): "BSS 00:11:22:33:44:55\n\tfreq: 2412\n\tsignal: -45.00 dBm\n\tSSID: test\n",
  38. }}}
  39. return NewProber(run, Options{}), run
  40. }
  41. func TestScanThrottleNoRepiteDentroDelIntervalo(t *testing.T) {
  42. p, run := newScanProber()
  43. ctx := context.Background()
  44. p.Build(ctx, "rt2", "test")
  45. if run.scanCalls != 1 {
  46. t.Fatalf("primera Build debería escanear (arranque), scanCalls=%d", run.scanCalls)
  47. }
  48. p.Build(ctx, "rt2", "test")
  49. if run.scanCalls != 1 {
  50. t.Fatalf("segunda Build dentro del intervalo NO debería escanear, scanCalls=%d", run.scanCalls)
  51. }
  52. }
  53. func TestScanThrottleForceScanDelServer(t *testing.T) {
  54. p, run := newScanProber()
  55. ctx := context.Background()
  56. p.Build(ctx, "rt2", "test") // arranque: 1 scan
  57. p.ForceScan()
  58. p.Build(ctx, "rt2", "test") // refresh del server: 2º scan sin esperar al intervalo
  59. if run.scanCalls != 2 {
  60. t.Fatalf("ForceScan debería escanear en el siguiente Build, scanCalls=%d", run.scanCalls)
  61. }
  62. p.Build(ctx, "rt2", "test") // y de nuevo dentro del intervalo: no escanea
  63. if run.scanCalls != 2 {
  64. t.Fatalf("Build tras ForceScan dentro del intervalo no debería escanear, scanCalls=%d", run.scanCalls)
  65. }
  66. }
  67. func TestScanThrottleExpiraIntervalo(t *testing.T) {
  68. p, run := newScanProber()
  69. ctx := context.Background()
  70. p.Build(ctx, "rt2", "test") // arranque: 1 scan
  71. // Envejece el último scan más allá del intervalo efectivo.
  72. p.scanMu.Lock()
  73. p.scanInterval = 10 * time.Minute
  74. p.lastScanAt = time.Now().Add(-(p.scanInterval + time.Minute))
  75. p.scanMu.Unlock()
  76. p.Build(ctx, "rt2", "test")
  77. if run.scanCalls != 2 {
  78. t.Fatalf("pasado scanInterval debería volver a escanear, scanCalls=%d", run.scanCalls)
  79. }
  80. }
  81. // #699: ScanDisabled no escanea NUNCA por intervalo (ni siquiera al
  82. // arrancar); solo ForceScan dispara el scan.
  83. func TestScanDisabledSoloOnDemand(t *testing.T) {
  84. p, run := newScanProber()
  85. p.scanMu.Lock()
  86. p.scanInterval = ScanDisabled
  87. p.scanMu.Unlock()
  88. ctx := context.Background()
  89. p.Build(ctx, "rt2", "test")
  90. if run.scanCalls != 0 {
  91. t.Fatalf("ScanDisabled no debería escanear al arrancar, scanCalls=%d", run.scanCalls)
  92. }
  93. p.ForceScan()
  94. p.Build(ctx, "rt2", "test")
  95. if run.scanCalls != 1 {
  96. t.Fatalf("ForceScan con ScanDisabled debería escanear, scanCalls=%d", run.scanCalls)
  97. }
  98. p.Build(ctx, "rt2", "test")
  99. if run.scanCalls != 1 {
  100. t.Fatalf("Build tras ForceScan con ScanDisabled no debería reescanear, scanCalls=%d", run.scanCalls)
  101. }
  102. }
  103. // #699: NewProber aplica DefaultScanInterval cuando Options.ScanInterval es
  104. // cero (compatibilidad con quien no configura nada).
  105. func TestScanIntervalDefaultCuandoCero(t *testing.T) {
  106. p := NewProber(&fakeRunner{}, Options{})
  107. p.scanMu.Lock()
  108. got := p.scanInterval
  109. p.scanMu.Unlock()
  110. if got != DefaultScanInterval {
  111. t.Fatalf("ScanInterval 0 debería resolver a DefaultScanInterval, got=%v", got)
  112. }
  113. p2 := NewProber(&fakeRunner{}, Options{ScanInterval: 15 * time.Minute})
  114. p2.scanMu.Lock()
  115. got2 := p2.scanInterval
  116. p2.scanMu.Unlock()
  117. if got2 != 15*time.Minute {
  118. t.Fatalf("ScanInterval configurado debería respetarse, got=%v", got2)
  119. }
  120. }