| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package adapters
- import (
- "fmt"
- "strings"
- "testing"
- "time"
- "github.com/gnacho/netpulse/server-go/internal/logonce"
- )
- // TestRecordClientBwSamplesWarnsOncePerInvalidMAC: una muestra con MAC que no
- // lo es se descarta y se avisa una sola vez por valor; la MAC vacía no avisa.
- func TestRecordClientBwSamplesWarnsOncePerInvalidMAC(t *testing.T) {
- var lines []string
- old := bwWarn
- bwWarn = logonce.NewWithLogf("[netpulse:clientbw] ", func(f string, a ...any) {
- lines = append(lines, fmt.Sprintf(f, a...))
- })
- t.Cleanup(func() { bwWarn = old })
- d := openLiveTestDB(t)
- l := &Live{db: d}
- t0 := time.Now()
- for i := 0; i < 3; i++ {
- bytes := uint64(100 * (i + 1))
- samples := []clientBwSample{
- {mac: "xyz", rx: bytes, tx: bytes},
- {mac: "", rx: bytes, tx: bytes},
- {mac: "AA:BB:CC:DD:EE:01", rx: bytes, tx: bytes},
- }
- l.recordClientBwSamples("gw", t0.Add(time.Duration(i)*time.Second), samples, "hostapd")
- }
- if len(lines) != 1 || !strings.HasPrefix(lines[0], "[netpulse:clientbw] ") ||
- !strings.Contains(lines[0], `"xyz"`) || !strings.Contains(lines[0], "router=gw") {
- t.Fatalf("avisos: %q (want uno con prefijo, el valor y el router)", lines)
- }
- if got := l.lastClientBw["gw"]; len(got) != 1 || got["AA:BB:CC:DD:EE:01"] == nil {
- t.Fatalf("lastClientBw[gw] = %+v (want solo la MAC válida)", got)
- }
- if n := countRawClientBW(t, d); n != 2 {
- t.Fatalf("filas totales en client_bw_raw = %d, esperaba 2 (solo la MAC válida)", n)
- }
- var badRows int
- if err := d.QueryRow("SELECT COUNT(*) FROM client_bw_raw WHERE mac != ?", "AA:BB:CC:DD:EE:01").Scan(&badRows); err != nil {
- t.Fatalf("consulta client_bw_raw: %v", err)
- }
- if badRows != 0 {
- t.Fatalf("filas con MAC inválida en client_bw_raw = %d, esperaba 0", badRows)
- }
- }
|