60 lines
2.0 KiB
Go
60 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestRunMergeComputesArrivalAndPersistenceGate(t *testing.T) {
|
|
dir := t.TempDir()
|
|
a := AgentSummary{RunID: "run1", Scenario: "p2p", Machine: "a", ReadyConnections: 1000,
|
|
PublishAttempted: 10, PublishAccepted: 10, ExpectedPeer: 10, ReceivedPeer: 9,
|
|
RealtimeLost: 1, HistoryChecked: 10, HistoryFound: 10}
|
|
b := AgentSummary{RunID: "run1", Scenario: "p2p", Machine: "b", ReadyConnections: 1000,
|
|
PublishAttempted: 10, PublishAccepted: 10, ExpectedPeer: 10, ReceivedPeer: 10,
|
|
HistoryChecked: 10, HistoryFound: 9, PersistenceLost: 1}
|
|
aPath, bPath, reportPath := filepath.Join(dir, "a.json"), filepath.Join(dir, "b.json"), filepath.Join(dir, "report.json")
|
|
writeSummaryForTest(t, aPath, a)
|
|
writeSummaryForTest(t, bPath, b)
|
|
if err := runMerge(aPath, bPath, reportPath); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var report CombinedReport
|
|
raw, err := os.ReadFile(reportPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = json.Unmarshal(raw, &report); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if report.ExpectedDeliveries != 20 || report.ReceivedDeliveries != 19 || report.ArrivalRate != 0.95 || report.LossRate != 0.05 {
|
|
t.Fatalf("unexpected delivery totals: %+v", report)
|
|
}
|
|
if report.Passed || report.PersistenceLost != 1 {
|
|
t.Fatalf("correctness gate did not fail: %+v", report)
|
|
}
|
|
}
|
|
|
|
func TestMergedHistogramP95(t *testing.T) {
|
|
agents := []AgentSummary{
|
|
{AckLatency: HistogramSnapshot{Count: 10, MaxMS: 900, Buckets: map[string]int64{"200ms": 9, "1s": 1}}},
|
|
{AckLatency: HistogramSnapshot{Count: 10, MaxMS: 300, Buckets: map[string]int64{"200ms": 10}}},
|
|
}
|
|
if got := mergedHistogramP95(agents, func(summary AgentSummary) HistogramSnapshot { return summary.AckLatency }); got != 200 {
|
|
t.Fatalf("p95=%v", got)
|
|
}
|
|
}
|
|
|
|
func writeSummaryForTest(t *testing.T, path string, summary AgentSummary) {
|
|
t.Helper()
|
|
raw, err := json.Marshal(summary)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = os.WriteFile(path, raw, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|