dnstt_exporter/internal/dnstt/collector_test.go

62 lines
1.8 KiB
Go
Raw Permalink Normal View History

2026-05-05 13:43:02 +02:00
package dnstt
import (
"testing"
"time"
)
func TestCollectorTracksActiveAndPeakClients(t *testing.T) {
now := time.Unix(1000, 0)
c := NewCollector([]string{"tunnel.example.com"}, WithNow(func() time.Time { return now }))
c.RecordQuery("tunnel.example.com", "client-a", 120)
c.RecordQuery("tunnel.example.com", "client-b", 80)
c.RecordResponse("tunnel.example.com", 200)
snapshot := c.Snapshot()
tunnel := snapshot.Tunnels["tunnel.example.com"]
if tunnel.ActiveClients != 2 {
t.Fatalf("active clients = %d, want 2", tunnel.ActiveClients)
}
if tunnel.PeakClients != 2 {
t.Fatalf("peak clients = %d, want 2", tunnel.PeakClients)
}
if tunnel.TotalSessions != 2 {
t.Fatalf("total sessions = %d, want 2", tunnel.TotalSessions)
}
if tunnel.TotalQueries != 2 {
t.Fatalf("queries = %d, want 2", tunnel.TotalQueries)
}
if tunnel.BytesIn != 200 {
t.Fatalf("bytes in = %d, want 200", tunnel.BytesIn)
}
if tunnel.BytesOut != 200 {
t.Fatalf("bytes out = %d, want 200", tunnel.BytesOut)
}
now = now.Add(ClientTimeout + time.Second)
snapshot = c.Snapshot()
tunnel = snapshot.Tunnels["tunnel.example.com"]
if tunnel.ActiveClients != 0 {
t.Fatalf("active clients after timeout = %d, want 0", tunnel.ActiveClients)
}
if tunnel.PeakClients != 2 {
t.Fatalf("peak clients after timeout = %d, want 2", tunnel.PeakClients)
}
}
func TestCollectorMatchesSubdomainsToRegisteredTunnel(t *testing.T) {
now := time.Unix(1000, 0)
c := NewCollector([]string{"tunnel.example.com"}, WithNow(func() time.Time { return now }))
c.RecordQuery("abcd.tunnel.example.com", "client-a", 120)
tunnel := c.Snapshot().Tunnels["tunnel.example.com"]
if tunnel.TotalQueries != 1 {
t.Fatalf("queries = %d, want 1", tunnel.TotalQueries)
}
if tunnel.ActiveClients != 1 {
t.Fatalf("active clients = %d, want 1", tunnel.ActiveClients)
}
}