gateway: TestPluginToldWhereTheAgentWorks counts a case's tries, not one request (#3347)

Cause: the test said a case's request reached the vendor exactly once.
A lone account whose try fails with a 5xx is asked again after a moment
by design (TestLastOneLeftIsTriedAgain), so one failed first try gives
the vendor two requests and the client a 200. That is what CI hit at
84d902ce2 (run 39906814999, race shard 3): "codex: the vendor got 2
requests". The product sends no duplicate: every request had a try of
its own. Which failure the first try met on CI isn't known, because the
test threw the route away. No gateway log line names it.

Reproduced: the fake vendor answered the codex case's first request
with a 502. The old test failed with the same message, "codex: the vendor
got 2 requests", and the route had try 1 at 502 with again=1 and try 2
at 200. Locally the natural failure never came: -race -count=150 (cpu
1,4), 6 parallel processes x 80, CI's shard 3 three times, and all 4
race shards at once (GOMAXPROCS=2) were all green.

Fix: the requests the case is about are its tries. The test fails when
the vendor got none, or got more requests than the route has tries (a
duplicate), or when a try that answered 200 was tried again. Every
request is checked for the headers the plugin was told and the vendor
never sees. Each retry is logged with its status and error, so the next
CI hit names the failure.
- Broken by hand: an injected 502 now passes and logs "codex: tried
  again after 502 FakeCo: 502 Bad Gateway". An injected duplicate (one
  request counted twice) fails with "codex: the vendor got 2 requests in
  1 tries".

Run at 84d902ce2 plus this change (macOS):
- gofmt -l: clean.
- go vet: darwin, GOOS=linux -tags nogui and GOOS=windows ok.
- go test -tags nogui -race -count=20 -run TestPluginToldWhereTheAgentWorks
  ./internal/gateway: ok.
- go test -tags nogui -timeout 45m ./internal/gateway: ok (358s), log
  kept whole.

After rebasing onto bac5be067 (plugin renewal lock), go vet and the
-race -count=20 run above: ok again. The package run is from before the
rebase.

Not tried: a Linux run, so the first try's CI failure stays unnamed.

Fixes #3347
This commit is contained in:
yetone
2026-10-23 14:43:37 +08:00
parent bac5be0677
commit 65e80405b8
+37 -18
View File
@@ -7,6 +7,7 @@ import (
"net/url"
"os"
"path/filepath"
"slices"
"sync"
"testing"
)
@@ -88,29 +89,47 @@ func TestPluginToldWhereTheAgentWorks(t *testing.T) {
mu.Lock()
got = nil
mu.Unlock()
postProto(t, New(), tc.path, tc.body)
rt := postProto(t, New(), tc.path, tc.body)
mu.Lock()
if len(got) != 1 {
mu.Unlock()
t.Fatalf("%s: the vendor got %d requests", tc.name, len(got))
}
h := got[0]
sent := slices.Clone(got)
mu.Unlock()
for _, k := range []string{"X-Magpie-Work-Dir", "X-Magpie-Work-Git"} {
if v := h.Values(k); len(v) > 0 {
t.Errorf("%s: the vendor got %s: %q", tc.name, k, v)
// the requests this case is about are its tries: one, or more when
// a try failed and the lone account was asked again after a moment
// (TestLastOneLeftIsTriedAgain), each told the same. A request with
// no try of its own would be a duplicate, and fails (#3347)
for _, x := range rt.Tries[:max(len(rt.Tries)-1, 0)] {
if x.Status == http.StatusOK {
t.Fatalf("%s: a try that answered was tried again: %+v", tc.name, rt.Tries)
}
t.Logf("%s: tried again after %d %s", tc.name, x.Status, x.Error)
}
dir := h.Get("X-Saw-Work-Dir")
if dir != "" {
d, err := url.PathUnescape(dir)
if err != nil {
t.Fatalf("%s: %q isn't percent-encoded: %v", tc.name, dir, err)
}
dir = d
if len(sent) == 0 || len(sent) > len(rt.Tries) {
t.Fatalf("%s: the vendor got %d requests in %d tries: %+v", tc.name, len(sent), len(rt.Tries), rt.Tries)
}
if dir != tc.dir || h.Get("X-Saw-Work-Git") != tc.git {
t.Errorf("%s: the plugin was told %q, git %q; want %q, %q", tc.name, dir, h.Get("X-Saw-Work-Git"), tc.dir, tc.git)
for _, h := range sent {
sawWorkPlace(t, tc.name, h, tc.dir, tc.git)
}
}
}
// sawWorkPlace checks what one request told the plugin (X-Saw-Work-*, as
// the fake plugin echoes it) and that none of it reached the vendor.
func sawWorkPlace(t *testing.T, name string, h http.Header, wantDir, wantGit string) {
t.Helper()
for _, k := range []string{"X-Magpie-Work-Dir", "X-Magpie-Work-Git"} {
if v := h.Values(k); len(v) > 0 {
t.Errorf("%s: the vendor got %s: %q", name, k, v)
}
}
dir := h.Get("X-Saw-Work-Dir")
if dir != "" {
d, err := url.PathUnescape(dir)
if err != nil {
t.Fatalf("%s: %q isn't percent-encoded: %v", name, dir, err)
}
dir = d
}
if dir != wantDir || h.Get("X-Saw-Work-Git") != wantGit {
t.Errorf("%s: the plugin was told %q, git %q; want %q, %q", name, dir, h.Get("X-Saw-Work-Git"), wantDir, wantGit)
}
}