diff --git a/internal/api/http_metrics_test.go b/internal/api/http_metrics_test.go index 8a32be88b4..6fb06bdca9 100644 --- a/internal/api/http_metrics_test.go +++ b/internal/api/http_metrics_test.go @@ -1,6 +1,7 @@ package api import ( + "os" "strconv" "testing" "time" @@ -198,20 +199,38 @@ func TestNormalizeSegment(t *testing.T) { // could copy a short segment into a stack buffer and hide the allocation. var normalizeSegmentSink string +// allocIsolationEnv names the test that a child process re-runs on its own. +const allocIsolationEnv = "PULSE_TEST_ALLOC_ISOLATION" + +// measureAllocsInIsolation keeps the upstream test entry point while using the +// bounded, measurement-marked child launcher shared with the background-fixture +// control. The child flag and upstream environment marker both select the actual +// measurement; the parent must observe that measurement, not just a passing test. +func measureAllocsInIsolation(t *testing.T) bool { + t.Helper() + if *routeAllocationChild || os.Getenv(allocIsolationEnv) == t.Name() { + t.Cleanup(func() { t.Log("ROUTE_ALLOCATION_MEASURED " + t.Name()) }) + return true + } + runRouteAllocationTest(t, t.Name()) + return false +} + func TestNormalizeSegment_DoesNotAllocate(t *testing.T) { - isolatedRouteAllocationCheck(t, func() { - for _, seg := range []string{ - "12345", - "550e8400-e29b-41d4-a716-446655440000", - "abcdefghijklmnopqrstuvwxyz1234567890abcdef", - "resources", - "metrics-store", - } { - if allocs := testing.AllocsPerRun(100, func() { normalizeSegmentSink = normalizeSegment(seg) }); allocs != 0 { - t.Errorf("normalizeSegment(%q) allocated %v times per call, want 0", seg, allocs) - } + if !measureAllocsInIsolation(t) { + return + } + for _, seg := range []string{ + "12345", + "550e8400-e29b-41d4-a716-446655440000", + "abcdefghijklmnopqrstuvwxyz1234567890abcdef", + "resources", + "metrics-store", + } { + if allocs := testing.AllocsPerRun(100, func() { normalizeSegmentSink = normalizeSegment(seg) }); allocs != 0 { + t.Errorf("normalizeSegment(%q) allocated %v times per call, want 0", seg, allocs) } - }) + } } func TestNormalizeRoute(t *testing.T) { @@ -273,13 +292,14 @@ func TestNormalizeRoute(t *testing.T) { var normalizeRouteSink string func TestNormalizeRoute_RootFastPathDoesNotAllocate(t *testing.T) { - isolatedRouteAllocationCheck(t, func() { - for _, path := range []string{"/", ""} { - if allocs := testing.AllocsPerRun(100, func() { normalizeRouteSink = normalizeRoute(path) }); allocs != 0 { - t.Errorf("normalizeRoute(%q) allocated %v times per call, want 0", path, allocs) - } + if !measureAllocsInIsolation(t) { + return + } + for _, path := range []string{"/", ""} { + if allocs := testing.AllocsPerRun(100, func() { normalizeRouteSink = normalizeRoute(path) }); allocs != 0 { + t.Errorf("normalizeRoute(%q) allocated %v times per call, want 0", path, allocs) } - }) + } } // ---------------------------------------------------------------------------