gateway tests: TestConcurrentRedactedCountTokens' children run the stand-in CLIs their parent already ran, and stop at the parent's deadline rather than a fixed 20s

The test re-executes its own binary once per mode so the redaction key
cache is cold. Each child went through TestMain again, which wrote all
eight stand-in CLIs (security, claude, codex, cursor-agent, devin, ...)
into a new temp home. On macOS a newly written program waits for the
system's first-run check before it runs. The child's fixture,
provider.Save, calls Accounts(), which runs those stand-ins (cursor and
devin identity asks, each waited for up to 3s, and `security` for the
Claude credential). So the child spent its budget on account discovery
it doesn't test. The masked count_tokens work under test takes ~6ms.

Evidence (stacks from the child's own timeout panic): every timed-out
child sat in provider.Save -> Accounts -> cliIdentity.get /
askDevinStatus / askCursorStatus / readClaudeCredential, waiting on a
stand-in's exec. None was in redact or count_tokens. Timing provider.Save
in the child under scratchpad load.sh (CPU burners plus new-script exec
loops), 6 interleaved runs each: 5.7-9.4s before, 3.9-5.1s after (one
8.1s). Idle: 4-6s before, 1.7-2.2s after. Under `taskpolicy -c
background` the old children timed out at 20s in 2 of 4 runs and in 2 of
6 earlier ones.

What remains after that is CPU starvation. Under background QoS at load
average ~150, children still went past 20s. Their stacks were runnable in
json encoding, or in `security` exec'd from a stand-in that had already
run, with nothing blocked. So the child's -test.timeout is now 90% of
the parent's remaining deadline instead of a fixed 20s. A child that
really hangs still prints its stacks before the parent's run ends.

Children now take the parent's stand-in folder from
MAGPIE_GATEWAY_TEST_STAND_INS. A child still gets its own temp HOME and
XDG folders.

Verified: -race -count=10 of the test; go test -race ./internal/gateway
(ok, 739s on a loaded machine; TestLargeRequestEndpoints alone took 272s
under -race, so the default 10m -timeout is too short there); go vet
./...; full go test -tags nogui ./... has gateway ok (298s).
This commit is contained in:
yetone
2026-10-07 06:10:06 +08:00
parent ee7ab461c5
commit e13f151302
2 changed files with 30 additions and 8 deletions
+21 -7
View File
@@ -10,6 +10,10 @@ import (
"github.com/yetone/magpie/internal/testenv"
)
// standInsEnv names the folder of a test binary's stand-in CLIs, for a
// test binary it starts again.
const standInsEnv = "MAGPIE_GATEWAY_TEST_STAND_INS"
func TestMain(m *testing.M) {
code, err := isolatedTests(m)
if err != nil {
@@ -45,17 +49,27 @@ func isolatedTests(m *testing.M) (int, error) {
return 1, err
}
}
bin := filepath.Join(home, "bin")
if err := os.Mkdir(bin, 0o755); err != nil {
return 1, err
}
// Finding an inert CLI also prevents discovery from falling back to
// one installed at an absolute system path. Tests can prepend their
// own fakes, as fakeClaude does, and still use ordinary shell tools.
if err := testenv.StandIns(bin, []string{"security", "secret-tool", "claude", "codex", "cursor-agent", "devin", "grok", "kiro-cli"}); err != nil {
return 1, err
// A test binary this one starts again (the cold-start redaction tests)
// runs the stand-ins its parent made: on macOS a newly written program
// waits for the system's first-run check, and under a full suite's load
// a child's account discovery spent its whole 20s on new stand-ins.
bin := os.Getenv(standInsEnv)
if bin == "" {
bin = filepath.Join(home, "bin")
if err := os.Mkdir(bin, 0o755); err != nil {
return 1, err
}
if err := testenv.StandIns(bin, []string{"security", "secret-tool", "claude", "codex", "cursor-agent", "devin", "grok", "kiro-cli"}); err != nil {
return 1, err
}
defer testenv.RemovePrograms()
if err := os.Setenv(standInsEnv, bin); err != nil {
return 1, err
}
}
defer testenv.RemovePrograms()
if err := os.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH")); err != nil {
return 1, err
}
+9 -1
View File
@@ -12,6 +12,7 @@ import (
"strings"
"sync"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/redact"
@@ -28,10 +29,17 @@ func TestConcurrentRedactedCountTokens(t *testing.T) {
if err != nil {
t.Fatal(err)
}
// a child that hangs prints its stacks before this run's own deadline
// does, rather than at a fixed 20s a loaded machine can spend on
// starting it
timeout := "0"
if d, ok := t.Deadline(); ok {
timeout = (time.Until(d) * 9 / 10).Round(time.Second).String()
}
for _, mode := range []string{"shared", "new_per_request"} {
t.Run(mode, func(t *testing.T) {
t.Parallel() // independent processes, each with a cold key cache
cmd := exec.Command(exe, "-test.run=^TestConcurrentRedactedCountTokens$", "-test.timeout=20s")
cmd := exec.Command(exe, "-test.run=^TestConcurrentRedactedCountTokens$", "-test.timeout="+timeout)
cmd.Env = append(os.Environ(), modeEnv+"="+mode)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("concurrent first requests: %v\n%s", err, out)