mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-10-26 13:39:47 +00:00
d268963536
The startup-watchdog race repair only converted the capture in service_health_test.go, but startup_watchdog_test.go still polls a plain bytes.Buffer while the watchdog goroutine writes the same buffer through zerolog. go test -race therefore still reports a read/write data race in TestStartupWatchdogFiresAndNamesLastPhase and the rest-0 backend shard stays red. Capture those logs through the same mutex-guarded syncBuffer so the poll and the watchdog write are serialized. Change-source: pulse-maintainer
68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package server
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
func TestStartupPhaseMarkAndCurrent(t *testing.T) {
|
|
var nilPhase *startupPhase
|
|
if got := nilPhase.current(); got != "unknown" {
|
|
t.Fatalf("nil phase current = %q, want unknown", got)
|
|
}
|
|
nilPhase.mark("ignored") // must not panic
|
|
|
|
phase := &startupPhase{}
|
|
if got := phase.current(); got != "unknown" {
|
|
t.Fatalf("empty phase current = %q, want unknown", got)
|
|
}
|
|
phase.mark("API router initialized")
|
|
if got := phase.current(); got != "API router initialized" {
|
|
t.Fatalf("phase current = %q, want last marked phase", got)
|
|
}
|
|
}
|
|
|
|
func TestStartupWatchdogFiresAndNamesLastPhase(t *testing.T) {
|
|
var buf syncBuffer
|
|
logger := zerolog.New(&buf)
|
|
|
|
phase := &startupPhase{}
|
|
phase.mark("AI services started")
|
|
|
|
stop := startStartupWatchdog(logger, phase, 20*time.Millisecond)
|
|
defer stop()
|
|
|
|
deadline := time.Now().Add(2 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
if strings.Contains(buf.String(), "startup is stalled") {
|
|
break
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
|
|
out := buf.String()
|
|
if !strings.Contains(out, "startup is stalled") {
|
|
t.Fatalf("watchdog did not fire; log=%q", out)
|
|
}
|
|
if !strings.Contains(out, "AI services started") {
|
|
t.Fatalf("watchdog did not name the last startup phase; log=%q", out)
|
|
}
|
|
}
|
|
|
|
func TestStartupWatchdogStopIsIdempotentAndPreventsFire(t *testing.T) {
|
|
var buf syncBuffer
|
|
logger := zerolog.New(&buf)
|
|
|
|
stop := startStartupWatchdog(logger, &startupPhase{}, 15*time.Millisecond)
|
|
stop()
|
|
stop() // must not panic
|
|
|
|
time.Sleep(80 * time.Millisecond)
|
|
if strings.Contains(buf.String(), "startup is stalled") {
|
|
t.Fatalf("watchdog fired after stop; log=%q", buf.String())
|
|
}
|
|
}
|