merged from gramsrv upstream

This commit is contained in:
onysd 2026-09-01 12:06:31 +03:00
parent 79c64ee916
commit 21a0856587
651 changed files with 54774 additions and 4590 deletions

View file

@ -5,6 +5,8 @@ import (
"errors"
"fmt"
"net"
"path/filepath"
"sync/atomic"
"testing"
"time"
@ -12,6 +14,43 @@ import (
tdrpc "github.com/iamxvbaba/td/rpc"
)
func TestMarkClientReadyAvoidsDuplicateInitialCatchUp(t *testing.T) {
var everReady atomic.Bool
var firstClient atomic.Bool
if markClientReady(&everReady, &firstClient) {
t.Fatal("first Ready transition requested a catch-up")
}
if !markClientReady(&everReady, &firstClient) {
t.Fatal("transport reconnect did not request a catch-up")
}
var replacementClient atomic.Bool
if markClientReady(&everReady, &replacementClient) {
t.Fatal("replacement Client duplicated its callback-owned initial catch-up")
}
if !markClientReady(&everReady, &replacementClient) {
t.Fatal("replacement Client transport reconnect did not request a catch-up")
}
}
func TestEventWriterCapsConnectionDetailsWithoutDroppingSamples(t *testing.T) {
w, err := newEventWriter(filepath.Join(t.TempDir(), "events.ndjson"))
if err != nil {
t.Fatal(err)
}
for i := 0; i < maxConnectionDeadEventLines+1; i++ {
w.write(map[string]any{"type": "connection_dead", "class": "connection"})
}
w.write(map[string]any{"type": "sample", "ready": 0})
written, dropped := w.counts()
if written != maxConnectionDeadEventLines+1 || dropped != 1 {
t.Fatalf("event counts = %d/%d", written, dropped)
}
if err := w.close(); err != nil {
t.Fatal(err)
}
}
func TestOperationMetricsUsesBoundedHistogramAndFixedErrorClasses(t *testing.T) {
metrics := &operationMetrics{}
metrics.observe(time.Now().Add(-20*time.Millisecond), nil)
@ -33,6 +72,7 @@ func TestClassifyErrorReasonUsesFiniteRedactedVocabulary(t *testing.T) {
{errors.New("dial tcp 10.0.0.1:2398: socket: too many open files"), "file_descriptor_limit"},
{errors.New("read: temporary auth key not found: pfs reconnect required"), "pfs_reconnect"},
{errors.New("read tcp: EOF auth_key_id=secret"), "eof"},
{errors.New("startup session ended before business readiness"), "business_readiness_incomplete"},
}
for _, test := range tests {
if got := classifyErrorReason(test.err); got != test.want {
@ -64,6 +104,34 @@ func TestOperationMetricsSeparatesHarnessCancellation(t *testing.T) {
}
}
func TestMetricSetFreezeExcludesCoordinatedTeardown(t *testing.T) {
metrics := newMetricSet("ping")
metrics.observe("ping", time.Now(), nil)
cut := metrics.freeze()
metrics.observe("ping", time.Now(), fmt.Errorf("engine forcibly closed: %w", context.Canceled))
if got := cut["ping"]; got.Count != 1 || got.Errors != 0 || got.Canceled != 0 {
t.Fatalf("workload cut = %#v", got)
}
if got := metrics.report()["ping"]; got != cut["ping"] {
t.Fatalf("post-freeze metrics changed: got %#v want %#v", got, cut["ping"])
}
}
func TestEvaluateReportRejectsFixedRateDeliveryLoss(t *testing.T) {
report := &RunReport{
ExpectedSessions: 1, PeakReadySessions: 1,
SteadySamples: 1, SteadyReadyRatio: 1, MinSteadyReadySessions: 1,
MessageScheduled: 2, MessageEnqueued: 2, MessageCompleted: 2,
Delivery: DeliveryReport{Expected: 2, Delivered: 1, Missing: 1},
Operations: map[string]OperationReport{},
}
evaluateReport(report, RunConfig{MinimumReadyRatio: 1, MessageRate: 1})
if report.Pass {
t.Fatal("fixed-rate report with a missing recipient delivery passed")
}
}
func TestEvaluateReportAllowsOnlyConnectionErrorsForExpectedRestart(t *testing.T) {
report := &RunReport{
ExpectedSessions: 2, PeakReadySessions: 2, Reconnects: 2,
@ -84,6 +152,24 @@ func TestEvaluateReportAllowsOnlyConnectionErrorsForExpectedRestart(t *testing.T
}
}
func TestEvaluateReportRejectsServerDeliveryAndDatabaseErrors(t *testing.T) {
report := &RunReport{
ExpectedSessions: 1, PeakReadySessions: 1,
SteadySamples: 1, SteadyReadyRatio: 1, MinSteadyReadySessions: 1,
Operations: map[string]OperationReport{},
RPCDeliveryOutcomes: map[string]map[string]uint64{
"updates.getState": {"ok": 1, "edge_overload": 2},
},
DatabaseWork: map[string]StartupDatabaseWork{
"messages.getDialogs": {Errors: 3},
},
}
evaluateReport(report, RunConfig{MinimumReadyRatio: 1})
if report.Pass || len(report.Failures) != 2 {
t.Fatalf("report = %#v", report)
}
}
func TestEvaluateReportRequiresReclamationAndNoFloodWait(t *testing.T) {
report := &RunReport{
ExpectedSessions: 10, PeakReadySessions: 10, ServerMetricsScrapes: 1,
@ -97,6 +183,7 @@ func TestEvaluateReportRequiresReclamationAndNoFloodWait(t *testing.T) {
"telesrv_mtproto_raw_connections": 2,
"telesrv_mtproto_logical_outbox_bytes": 4,
},
WorkloadEndServerMetrics: map[string]float64{},
}
evaluateReport(report, RunConfig{MinimumReadyRatio: 1, RecoveryDuration: time.Minute, ServerMetricsURL: "http://metrics"})
if report.Pass || len(report.Failures) != 1 {
@ -119,6 +206,7 @@ func TestEvaluateReportAcceptsReturnToNonZeroSharedServerBaseline(t *testing.T)
"telesrv_mtproto_logical_sessions": 2,
"telesrv_mtproto_logical_outbox_bytes": 1024,
},
WorkloadEndServerMetrics: map[string]float64{},
}
evaluateReport(report, RunConfig{MinimumReadyRatio: 1, RecoveryDuration: time.Minute, ServerMetricsURL: "http://metrics"})
if !report.Pass || len(report.Failures) != 0 {