diff --git a/internal/monitoring/alert_checks_shutdown_test.go b/internal/monitoring/alert_checks_shutdown_test.go index 8af7750c27..de494b21d6 100644 --- a/internal/monitoring/alert_checks_shutdown_test.go +++ b/internal/monitoring/alert_checks_shutdown_test.go @@ -2,8 +2,11 @@ package monitoring import ( "context" + "fmt" "os" "path/filepath" + "runtime" + "strings" "sync" "testing" "time" @@ -76,6 +79,52 @@ func blockFirstShutdownAlert(t *testing.T, monitor *Monitor) (<-chan struct{}, f return entered, unblock } +// Retain only fixed frame counts, never raw stacks, arguments or paths. These +// are process-wide observations at the failed boundary, not proof that a +// particular goroutine owns the tenant or caused the timeout. +var alertShutdownFrames = []string{ + "(*Monitor).Start(", + "(*Monitor).checkMockAlerts(", + "blockFirstShutdownAlert.func", + "(*Monitor).runPeriodicNotificationDeliveryChecks(", + "(*Monitor).runPeriodicConnectionAlertChecks(", + "(*Monitor).startMockMetricsSampler.func", + "(*Monitor).stopMockMetricsSampler(", + "(*Monitor).stopAlertProjectionCatchUp(", + "(*mockModeFence).advance(", +} + +func alertShutdownDiagnostics() string { + const stackBudget = 1 << 20 + buf := make([]byte, stackBudget) + n := runtime.Stack(buf, true) + return formatAlertShutdownDiagnostics(buf[:n], n == len(buf)) +} + +func formatAlertShutdownDiagnostics(stacks []byte, truncated bool) string { + var result strings.Builder + fmt.Fprintf(&result, "shutdown frame observations (process-wide, not ownership/causation): truncated=%t", truncated) + for _, frame := range alertShutdownFrames { + count := 0 + for _, stack := range strings.Split(string(stacks), "\n\n") { + if strings.Contains(stack, frame) { + count++ + } + } + fmt.Fprintf(&result, "; %s=%d", frame, count) + } + return result.String() +} + +func awaitAlertShutdown(t *testing.T, done <-chan struct{}, boundary string) { + t.Helper() + select { + case <-done: + case <-time.After(5 * time.Second): + t.Fatalf("%s: %s", boundary, alertShutdownDiagnostics()) + } +} + func awaitTenantRuntimeShutdown(t *testing.T, manager *MultiTenantMonitor, orgID string) { t.Helper() manager.mu.RLock() @@ -84,11 +133,7 @@ func awaitTenantRuntimeShutdown(t *testing.T, manager *MultiTenantMonitor, orgID if done == nil { t.Fatal("tenant has no owned runtime completion signal") } - select { - case <-done: - case <-time.After(5 * time.Second): - t.Fatal("tenant runtime did not join its released writer") - } + awaitAlertShutdown(t, done, "tenant runtime did not join its released writer") } func TestMonitorStartJoinsMockAlertChecksBeforeReturning(t *testing.T) { @@ -111,7 +156,7 @@ func TestMonitorStartJoinsMockAlertChecksBeforeReturning(t *testing.T) { case <-done: monitor.Stop() case <-time.After(5 * time.Second): - t.Error("monitor did not finish after releasing the alert pass") + t.Errorf("monitor did not finish after releasing the alert pass: %s", alertShutdownDiagnostics()) } }) select { @@ -126,11 +171,7 @@ func TestMonitorStartJoinsMockAlertChecksBeforeReturning(t *testing.T) { case <-time.After(100 * time.Millisecond): } unblock() - select { - case <-done: - case <-time.After(5 * time.Second): - t.Fatal("monitor failed to join its completed alert pass") - } + awaitAlertShutdown(t, done, "monitor failed to join its completed alert pass") if len(monitor.GetAlertManager().GetActiveAlerts()) == 0 { t.Fatal("shutdown silently dropped the admitted alert pass") } @@ -163,7 +204,7 @@ func TestMonitorStartJoinsConnectionAlertChecksBeforeReturning(t *testing.T) { case <-done: monitor.Stop() case <-time.After(5 * time.Second): - t.Error("monitor did not finish after releasing the connection lister") + t.Errorf("monitor did not finish after releasing the connection lister: %s", alertShutdownDiagnostics()) } }) select { @@ -178,11 +219,7 @@ func TestMonitorStartJoinsConnectionAlertChecksBeforeReturning(t *testing.T) { case <-time.After(100 * time.Millisecond): } unblock() - select { - case <-done: - case <-time.After(5 * time.Second): - t.Fatal("monitor failed to join its completed connection check") - } + awaitAlertShutdown(t, done, "monitor failed to join its completed connection check") } func TestTenantDeletionRetainsBlockedAlertPassThenRemovesCompletedRuntime(t *testing.T) { diff --git a/internal/monitoring/alert_shutdown_diagnostics_test.go b/internal/monitoring/alert_shutdown_diagnostics_test.go new file mode 100644 index 0000000000..08fbb4e4fa --- /dev/null +++ b/internal/monitoring/alert_shutdown_diagnostics_test.go @@ -0,0 +1,64 @@ +package monitoring + +import ( + "context" + "github.com/rcourtman/pulse-go-rewrite/internal/config" + "strings" + "testing" +) + +func TestAlertShutdownDiagnosticsBoundedAndContentFree(t *testing.T) { + // Multiple occurrences of a frame in one goroutine still count as one. + // Neither the header, argument values nor paths belong in CI diagnostics. + stacks := []byte("goroutine 981 [semacquire]:\nexample.(*Monitor).Start(0xPRIVATE)\n /private/fixture.go:42\nexample.(*Monitor).Start(0xPRIVATE)\n\n" + + "goroutine 982 [chan receive]:\nexample.(*Monitor).checkMockAlerts(0xPRIVATE)\nexample.blockFirstShutdownAlert.func1(0xPRIVATE)\n /private/fixture.go:99\n\n" + + "goroutine 983 [sleep]:\nother.unrelated(0xPRIVATE)\n") + for _, truncated := range []bool{false, true} { + got := formatAlertShutdownDiagnostics(stacks, truncated) + for _, want := range []string{"(*Monitor).Start(=1", "(*Monitor).checkMockAlerts(=1", "blockFirstShutdownAlert.func=1", "(*Monitor).stopMockMetricsSampler(=0"} { + if !strings.Contains(got, want) { + t.Errorf("missing %q in %s", want, got) + } + } + for _, withheld := range []string{"981", "982", "PRIVATE", "/private", "unrelated", "semacquire"} { + if strings.Contains(got, withheld) { + t.Errorf("raw stack detail %q escaped fixed-frame diagnostics", withheld) + } + } + if strings.Contains(got, "truncated=true") != truncated { + t.Error("stack truncation must be explicit") + } + if len(got) > 1024 { + t.Errorf("fixed diagnostic unexpectedly grew to %d bytes", len(got)) + } + } +} + +func TestAlertShutdownDiagnosticsObserveBlockedWriter(t *testing.T) { + configureAlertShutdownFixture(t) + monitor, err := New(&config.Config{DataPath: t.TempDir()}) + if err != nil { + t.Fatal(err) + } + entered, unblock := blockFirstShutdownAlert(t, monitor) + ctx, cancel := context.WithCancel(context.Background()) + done := make(chan struct{}) + go func() { defer close(done); monitor.Start(ctx, nil) }() + t.Cleanup(func() { + cancel() + unblock() + awaitAlertShutdown(t, done, "diagnostic fixture failed to drain") + monitor.Stop() + }) + awaitAlertShutdown(t, entered, "diagnostic fixture did not reach the writer") + got := alertShutdownDiagnostics() + for _, want := range []string{"(*Monitor).Start(=1", "(*Monitor).checkMockAlerts(=1", "blockFirstShutdownAlert.func=1"} { + if !strings.Contains(got, want) { + t.Errorf("live blocked writer missing %q in %s", want, got) + } + } + t.Log(got) + cancel() + unblock() + awaitAlertShutdown(t, done, "diagnostic fixture failed to join the released writer") +}