mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-02 10:09:02 +01:00
No Router built by the internal/api tests was ever freed. At the end of a run
all 1,358 were still reachable, with 26,000 goroutines and 2.86 GiB live, so
peak memory grew with every new test until the release correctness job ran out
of memory on its 7 GiB runner.
Most test sites never stopped their Router's workers. The few that did were
pinned anyway, because the setup-token cleanup worker in ConfigHandlers had no
way to stop and holds callbacks back into the Router. That worker now joins
the Router's shutdown, and package tests build Routers through newTestRouter,
which stops them with the test. Two new tests fail if a Router stays reachable
after shutdown or a package test calls NewRouter directly.
Default command policies now share one compiled pattern set. A private copy
cost 230 KiB and 1.4 ms per call and is now 2.2 KiB and 1.6 us. It accounted
for a quarter of the live test heap. In production each organization's AI
service holds one, so this also saves about 230 KiB per organization.
Measured on pulse-dev at 88c2a21f65 with the same go test -count=1 -p 1 run:
peak RSS of the largest child 6.41 GiB before, 2.44 GiB after. That largest
child is now the compiler: a cold test build alone peaks at 2.34 GiB and the
test binary alone at 1.48 GiB. All package tests pass. CPU time fell about 5%;
wall clock followed the host's load and is not comparable between these runs.
Contract-Neutral: Stops test Router workers and shares compiled policy patterns; no API, policy decision or storage change.
87 lines
2.7 KiB
Go
87 lines
2.7 KiB
Go
package api
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
"weak"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/config"
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/monitoring"
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/websocket"
|
|
)
|
|
|
|
// newTestRouter builds a Router and stops what it owns when the test ends.
|
|
// NewRouter starts rate limiters, pollers and store workers whose goroutines
|
|
// keep the whole Router reachable until they are stopped, so a test that drops
|
|
// its Router still holds it for the rest of the package run.
|
|
//
|
|
// Register t.TempDir and t.Setenv before calling this: cleanups run last-in
|
|
// first-out, and the workers may still write to the data directory as they stop.
|
|
func newTestRouter(t testing.TB, cfg *config.Config, monitor *monitoring.Monitor, mtMonitor *monitoring.MultiTenantMonitor, wsHub *websocket.Hub, reloadFunc func() error, serverVersion string) *Router {
|
|
t.Helper()
|
|
router := NewRouter(cfg, monitor, mtMonitor, wsHub, reloadFunc, serverVersion)
|
|
t.Cleanup(func() {
|
|
router.ShutdownBackgroundWorkers()
|
|
router.ShutdownResourceStores()
|
|
router.ShutdownRBAC()
|
|
})
|
|
return router
|
|
}
|
|
|
|
// A worker that outlives its Router pins everything the Router built. This is
|
|
// the production shutdown path too, so a new worker has to join it.
|
|
func TestStoppedRouterBecomesUnreachable(t *testing.T) {
|
|
var built weak.Pointer[Router]
|
|
t.Run("build", func(t *testing.T) {
|
|
built = weak.Make(newTestRouter(t, &config.Config{DataPath: t.TempDir()}, nil, nil, nil, nil, "test"))
|
|
})
|
|
|
|
deadline := time.Now().Add(10 * time.Second)
|
|
for built.Value() != nil {
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("Router stayed reachable after its workers stopped")
|
|
}
|
|
runtime.GC()
|
|
time.Sleep(20 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
// A direct NewRouter call in this package's tests leaks its Router until the
|
|
// test binary exits, which is what grew the package's peak memory past the
|
|
// release runner's limit.
|
|
func TestPackageTestsBuildRoutersThroughLifecycleHelper(t *testing.T) {
|
|
const helperFile = "test_router_lifecycle_test.go"
|
|
direct := regexp.MustCompile(`(^|[^.\w])NewRouter\(`)
|
|
|
|
paths, err := filepath.Glob("*_test.go")
|
|
if err != nil {
|
|
t.Fatalf("list test files: %v", err)
|
|
}
|
|
for _, path := range paths {
|
|
if path == helperFile {
|
|
continue
|
|
}
|
|
src, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatalf("read %s: %v", path, err)
|
|
}
|
|
if packageClause(string(src)) == "package api" && direct.Match(src) {
|
|
t.Errorf("%s calls NewRouter directly; use newTestRouter so the Router stops with the test", path)
|
|
}
|
|
}
|
|
}
|
|
|
|
func packageClause(src string) string {
|
|
for _, line := range strings.Split(src, "\n") {
|
|
if strings.HasPrefix(line, "package ") {
|
|
return line
|
|
}
|
|
}
|
|
return ""
|
|
}
|