From 20a6b86a4a66ca3408dfd7fd0e4a8bbe9c01052c Mon Sep 17 00:00:00 2001 From: Anankke Date: Fri, 20 Nov 2026 16:57:53 -0800 Subject: [PATCH] test(gateway): measure signed-history allocations in a child process TestFilterThinkingBlocksSignedHistorySkipsDecode asserts testing.AllocsPerRun < 16, and AllocsPerRun reads the process-wide malloc counter, so allocations by background goroutines that earlier tests in the package leave running are counted against it and the test fails intermittently in full package runs. Run the measurement in a child test process that runs only this test, as TestOpenAIResponsesOrphanPrecheckLargeEscapedContent already does. The assertions and the threshold are unchanged. --- .../gateway_thinking_filter_precheck_test.go | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/backend/internal/service/gateway_thinking_filter_precheck_test.go b/backend/internal/service/gateway_thinking_filter_precheck_test.go index b7cb36c4d9..32b173ce25 100644 --- a/backend/internal/service/gateway_thinking_filter_precheck_test.go +++ b/backend/internal/service/gateway_thinking_filter_precheck_test.go @@ -2,9 +2,14 @@ package service import ( "bytes" + "context" "encoding/json" + "os" + "os/exec" + "regexp" "strings" "testing" + "time" "github.com/Wei-Shaw/sub2api/internal/pkg/antigravity" "github.com/stretchr/testify/require" @@ -75,6 +80,21 @@ func signedThinkingHistoryBody() []byte { } func TestFilterThinkingBlocksSignedHistorySkipsDecode(t *testing.T) { + // AllocsPerRun counts allocations across the process, including background + // goroutines left by other service tests. Measure only this case in a child. + const childEnv = "SUB2API_THINKING_FILTER_ALLOC_TEST_CHILD" + if os.Getenv(childEnv) != "1" { + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + executable, err := os.Executable() + require.NoError(t, err) + cmd := exec.CommandContext(ctx, executable, "-test.run=^"+regexp.QuoteMeta(t.Name())+"$", "-test.count=1") + cmd.Env = append(os.Environ(), childEnv+"=1") + output, err := cmd.CombinedOutput() + require.NoError(t, err, "isolated allocation test failed:\n%s", output) + return + } + body := signedThinkingHistoryBody() allocs := testing.AllocsPerRun(5, func() { got := filterThinkingBlocksInternal(body, false)