test(openai): isolate orphan precheck allocation test

This commit is contained in:
lkon committed 2026-11-08 19:33:33 +08:00
1 parent 6b4b37db43
commit 2273a5d63f
1 file changed
+20
@@ -1,8 +1,13 @@
package service
import (
"context"
"os"
"os/exec"
"regexp"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
@@ -126,6 +131,21 @@ func TestOpenAIResponsesOrphanPrecheckCompatibilityErrors(t *testing.T) {
}
func TestOpenAIResponsesOrphanPrecheckLargeEscapedContent(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_OPENAI_ORPHAN_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 := []byte(`{"vendor":"` + strings.Repeat(`\n\"`, 1<<16) + `","input":[{"content":"` + strings.Repeat(`\n\"`, 1<<16) + `","type":"message"}]}`)
require.False(t, openAIResponsesInputMayNeedOrphanCleanup(body))
allocations := testing.AllocsPerRun(5, func() {