v0.1.2
Go quality gates / test (push) Waiting to run

This commit is contained in:
Mateusz Gruszczyński committed 2026-12-01 00:16:15 +01:00
1 parent de13d851a8
commit a28362b82c
34 files changed
+1541 -341

No files matched your search

+86
View File
@@ -0,0 +1,86 @@
package telemetry
import (
"math"
"testing"
"time"
"git.linuxiarz.pl/gru/EasyDNS-Web/internal/easydns"
)
func TestWindowParsingAndBoundedSampling(t *testing.T) {
now := time.Date(2026, 11, 30, 18, 0, 0, 0, time.UTC)
cases := []struct {
span, step string
seconds int
}{
{"15m", "auto", 15}, {"1h", "auto", 15}, {"6h", "auto", 60}, {"12h", "auto", 300}, {"24h", "auto", 300}, {"24h", "1h", 3600}, {"invalid", "invalid", 300},
}
for _, tc := range cases {
w := ParseWindow(tc.span, tc.step, now)
if w.StepSeconds != tc.seconds || w.Span > 24*time.Hour || w.Span < 15*time.Minute || w.From.After(w.To) {
t.Errorf("bad window %+v", w)
}
}
// An old telemetry consumer without step= keeps the original measured rows.
if v := ParseWindow("", "", now); v.StepSeconds != 15 {
t.Fatalf("legacy endpoint changed resolution: %+v", v)
}
h := New()
stats := easydns.Stats{Responses: map[string]uint64{}, Sources: map[string]uint64{}}
for i := 0; i < maxSamples+12; i++ {
stats.QueriesTotal += 15
h.Record(now.Add(time.Duration(i)*PollInterval), stats)
}
samples, _, _ := h.Snapshot()
if len(samples) != maxSamples {
t.Fatalf("bounded history retained %d not %d", len(samples), maxSamples)
}
}
func TestDownsampleOnlyMeasuredBucketsAndWeightedCache(t *testing.T) {
h := New()
start := time.Date(2026, 11, 30, 10, 0, 0, 0, time.UTC)
mk := func(q, hits, miss uint64, p95 float64) easydns.Stats {
return easydns.Stats{QueriesTotal: q, Responses: map[string]uint64{"noerror": q}, Sources: map[string]uint64{"recursive": q / 2}, Cache: easydns.Cache{Hits: hits, Misses: miss, Entries: 12}, LatencyP95S: p95}
}
h.Record(start.Add(-15*time.Second), mk(100, 0, 0, .005))
h.Record(start, mk(115, 15, 0, .015)) // 1 QPS, cache 100%
h.Record(start.Add(15*time.Second), mk(145, 15, 30, .030)) // 2 QPS, cache 0%
h.Record(start.Add(61*time.Second), mk(177, 15, 62, .015)) // measured later than 30s
w := ParseWindow("1h", "1m", start.Add(2*time.Minute))
pts := WindowSamples(historySamples(h), w)
if len(pts) != 3 {
t.Fatalf("expected three observed buckets, got %d: %+v", len(pts), pts)
}
p := pts[1]
if p.QueryRate == nil || math.Abs(*p.QueryRate-1.5) > 1e-8 {
t.Fatalf("downsample must average actual rates, got %+v", p.QueryRate)
}
if p.CacheHitRate == nil || math.Abs(*p.CacheHitRate-100.0/3) > 1e-8 {
t.Fatalf("cache bucket must use weighted hits/misses, got %+v", p.CacheHitRate)
}
if p.P95MS != 30 {
t.Fatalf("peak p95 within bucket lost, got %v", p.P95MS)
}
if p.Time != start.Add(15*time.Second) {
t.Fatalf("bucket fabricated a timestamp: %v", p.Time)
}
// A counter reset marks its bucket as missing, not negative or zero.
h.Record(start.Add(2*time.Minute), mk(0, 0, 0, .005))
pts = WindowSamples(historySamples(h), w)
for _, pt := range pts {
if pt.Time == start.Add(2*time.Minute) && pt.QueryRate != nil {
t.Fatal("counter reset was turned into a rate")
}
}
}
func TestFleetDownsamplingPreservesIncompleteBuckets(t *testing.T) {
t0 := time.Date(2026, 11, 30, 10, 0, 0, 0, time.UTC)
x := 1.0
points := []FleetPoint{{Time: t0, QueryRate: &x, Contributors: 2}, {Time: t0.Add(15 * time.Second), QueryRate: nil, Contributors: 1}}
w := ParseWindow("1h", "1m", t0.Add(time.Minute))
reduced := WindowFleet(points, w)
if len(reduced) != 1 || reduced[0].QueryRate != nil || reduced[0].Contributors != 1 {
t.Fatalf("missing node hidden by downsample: %+v", reduced)
}
}