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.
This commit is contained in:
Anankke committed 2026-11-20 16:57:53 -08:00
1 parent 328db26f45
commit 20a6b86a4a
1 file changed
+20
@@ -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)