Files
Mateusz Gruszczyński de13d851a8
Go quality gates / test (push) Waiting to run
v0.1.1
2026-11-30 14:44:28 +01:00

93 lines
3.4 KiB
Go

package telemetry
import (
"encoding/json"
"math"
"strings"
"testing"
"time"
"git.linuxiarz.pl/gru/EasyDNS-Web/internal/easydns"
)
func fleetStats(queries, hits, misses uint64) easydns.Stats {
return easydns.Stats{
QueriesTotal: queries,
Responses: map[string]uint64{"noerror": queries / 2, "nxdomain": queries / 4},
Blocked: queries / 10,
Cache: easydns.Cache{Hits: hits, Misses: misses},
LatencyP95S: .021,
}
}
func TestFleetAggregateSumsRatesAndWeightsCacheCounts(t *testing.T) {
base := time.Date(2026, 11, 30, 10, 0, 2, 0, time.UTC)
inputs := []struct {
name string
off time.Duration
first, last easydns.Stats
}{
{"primary", 0, fleetStats(100, 10, 10), fleetStats(130, 30, 10)},
{"secondary-1", 2 * time.Second, fleetStats(200, 0, 0), fleetStats(260, 0, 60)},
{"secondary-2", 5 * time.Second, fleetStats(50, 0, 0), fleetStats(95, 10, 10)},
}
var nodes []FleetNode
for i, tc := range inputs {
h := New()
h.Record(base.Add(tc.off), tc.first)
h.Record(base.Add(tc.off+15*time.Second), tc.last)
samples, _, _ := h.Snapshot()
nodes = append(nodes, FleetNode{ID: string(rune('a' + i)), Name: tc.name, Samples: samples})
}
points := AggregateFleet(nodes)
if len(points) != 2 {
t.Fatalf("expected grid points at 10:00:15 and 10:00:30, got %d", len(points))
}
last := points[len(points)-1]
if last.Contributors != 3 || last.QueryRate == nil || math.Abs(*last.QueryRate-9) > .0001 {
t.Fatalf("3-node sum must be (30+60+45)/15 = 9 QPS: %+v", last)
}
// 20 hits + 0 hits + 10 hits vs 0 + 60 + 10 misses = 30%.
if last.CacheHitRate == nil || math.Abs(*last.CacheHitRate-30) > .0001 {
t.Fatalf("fleet cache ratio must be weighted 30%%, not avg of percentages: %+v", last)
}
if last.NoErrorRate == nil || *last.NoErrorRate <= 0 || last.BlockedRate == nil {
t.Fatalf("additive response/blocked rates missing: %+v", last)
}
body, err := json.Marshal(last)
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(body), "p95") || strings.Contains(string(body), "p50") {
t.Fatal("aggregated latency percentiles would be invalid")
}
}
func TestFleetAggregateNeverTreatsMissingNodesAsZero(t *testing.T) {
base := time.Date(2026, 11, 30, 10, 0, 2, 0, time.UTC)
a, b := New(), New()
a.Record(base, fleetStats(100, 10, 10))
a.Record(base.Add(15*time.Second), fleetStats(145, 30, 10))
b.Record(base.Add(3*time.Second), fleetStats(500, 20, 20)) // no second sample: rate unknown
points := AggregateFleet([]FleetNode{{ID: "a", Samples: historySamples(a)}, {ID: "b", Samples: historySamples(b)}})
last := points[len(points)-1]
if last.QueryRate != nil || last.CacheHitRate != nil || last.Contributors != 1 {
t.Fatalf("partial fleet must produce gap, not 3 QPS presented as total: %+v", last)
}
// Counter reset on one node must also introduce a null, not a negative QPS.
b.Record(base.Add(18*time.Second), fleetStats(0, 0, 0))
points = AggregateFleet([]FleetNode{{ID: "a", Samples: historySamples(a)}, {ID: "b", Samples: historySamples(b)}})
last = points[len(points)-1]
if last.QueryRate != nil || last.Contributors != 1 {
t.Fatalf("reset must not show a partial aggregate as full: %+v", last)
}
}
func TestFleetEmptyHistory(t *testing.T) {
if AggregateFleet(nil) != nil || AggregateFleet([]FleetNode{{Name: "no readings"}}) != nil {
t.Fatal("fleet with no readings must stay empty")
}
}
func historySamples(h *History) []Sample { s, _, _ := h.Snapshot(); return s }