Add scripts/integration.sh, which starts the real FastAPI and Go API, trains small and forecast through POST /api/train, checks SSE scenario propagation, heartbeat, reconnect, then kills FastAPI to verify snapshot and last-good forecast fallback before restarting it for recovery. Add scripts/sse-load, a standard-library load generator that holds 120 concurrent SSE streams for five minutes, posts scenario changes, records p50/p95/p99 event latency, drops and heartbeat cadence, and exits non-zero when a contract threshold is missed. Add unit tests, make targets and README documentation.
125 lines
3.3 KiB
Go
125 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"math"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestPercentileNearestRank(t *testing.T) {
|
|
values := []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
|
cases := []struct {
|
|
p float64
|
|
want float64
|
|
}{
|
|
{50, 5},
|
|
{95, 10},
|
|
{99, 10},
|
|
{0, 1},
|
|
{100, 10},
|
|
}
|
|
for _, tc := range cases {
|
|
if got := percentile(values, tc.p); got != tc.want {
|
|
t.Fatalf("percentile(%v, %v)=%v, want %v", values, tc.p, got, tc.want)
|
|
}
|
|
}
|
|
if got := percentile(nil, 95); got != 0 {
|
|
t.Fatalf("empty percentile=%v, want 0", got)
|
|
}
|
|
|
|
p50, p95, p99, max := percentiles(values)
|
|
if p50 != 5 || p95 != 10 || p99 != 10 || max != 10 {
|
|
t.Fatalf("percentiles=(%v,%v,%v,%v)", p50, p95, p99, max)
|
|
}
|
|
}
|
|
|
|
func TestPercentileDoesNotMutateInput(t *testing.T) {
|
|
values := []float64{3, 1, 2}
|
|
_ = percentile(values, 50)
|
|
if values[0] != 3 || values[1] != 1 || values[2] != 2 {
|
|
t.Fatalf("input was reordered: %v", values)
|
|
}
|
|
}
|
|
|
|
func TestBuildPingSummary(t *testing.T) {
|
|
start := time.Date(2026, 11, 14, 10, 0, 0, 0, time.UTC)
|
|
var pings []time.Time
|
|
for i := 0; i < 5; i++ {
|
|
pings = append(pings, start.Add(time.Duration(i)*15*time.Second))
|
|
}
|
|
clients := []*client{
|
|
{pings: pings[:3]},
|
|
{pings: pings[:4]},
|
|
{pings: pings},
|
|
}
|
|
got := buildPingSummary(pings, clients)
|
|
if got.Unique != 5 {
|
|
t.Fatalf("unique=%d, want 5", got.Unique)
|
|
}
|
|
if math.Abs(got.MedianIntervalMS-15000) > 0.1 {
|
|
t.Fatalf("median interval=%v, want 15000", got.MedianIntervalMS)
|
|
}
|
|
if got.MaxIntervalMS != 15000 || got.MinIntervalMS != 15000 {
|
|
t.Fatalf("interval range=[%v,%v], want [15000,15000]", got.MinIntervalMS, got.MaxIntervalMS)
|
|
}
|
|
if got.PerClientMin != 3 || got.PerClientMedian != 4 || got.PerClientMax != 5 {
|
|
t.Fatalf("per-client=(%d,%d,%d), want (3,4,5)",
|
|
got.PerClientMin, got.PerClientMedian, got.PerClientMax)
|
|
}
|
|
}
|
|
|
|
func TestSummaryPassAndFail(t *testing.T) {
|
|
started := time.Now()
|
|
makeRecorder := func(peak, drops, postErrs int) *recorder {
|
|
return &recorder{
|
|
peak: peak,
|
|
drops: drops,
|
|
postErrs: postErrs,
|
|
pingTimes: make(map[time.Time]struct{}),
|
|
}
|
|
}
|
|
changes := []changeResult{{
|
|
Version: 2,
|
|
Connected: 120,
|
|
Received: 120,
|
|
LatenciesMS: []float64{4, 6, 8},
|
|
}}
|
|
pass := buildSummary(makeRecorder(120, 0, 0), changes, "http://example", 120, 120, started, 5*time.Minute)
|
|
if !pass.Passed {
|
|
t.Fatalf("expected pass, reasons=%v", pass.FailureReasons)
|
|
}
|
|
|
|
fail := buildSummary(makeRecorder(119, 1, 1), changes, "http://example", 120, 120, started, 5*time.Minute)
|
|
if fail.Passed {
|
|
t.Fatal("expected failure for low peak and drops")
|
|
}
|
|
if len(fail.FailureReasons) < 3 {
|
|
t.Fatalf("failure reasons=%v, want at least 3", fail.FailureReasons)
|
|
}
|
|
}
|
|
|
|
func TestSlowLatencyFailsThreshold(t *testing.T) {
|
|
rec := &recorder{peak: 120, pingTimes: make(map[time.Time]struct{})}
|
|
changes := []changeResult{{
|
|
Version: 3,
|
|
Connected: 120,
|
|
Received: 120,
|
|
LatenciesMS: []float64{2500},
|
|
}}
|
|
sum := buildSummary(rec, changes, "http://example", 120, 120, time.Now(), 5*time.Minute)
|
|
if sum.Passed {
|
|
t.Fatal("expected p95 latency over 2s to fail")
|
|
}
|
|
}
|
|
|
|
func TestScenarioReceiptKeepsFirst(t *testing.T) {
|
|
c := &client{}
|
|
first := time.Unix(100, 0)
|
|
c.recordScenario(4, first)
|
|
c.recordScenario(4, first.Add(time.Second))
|
|
got, ok := c.receipt(4)
|
|
if !ok || !got.Equal(first) {
|
|
t.Fatalf("receipt=%v ok=%v, want %v", got, ok, first)
|
|
}
|
|
}
|