From fa233297694ada00545c7da00977943c09fcd3f0 Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 29 Oct 2026 18:27:21 +0800 Subject: [PATCH] gateway tests: an unrouted 504 test waits for its held report before it ends MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TestUnroutedUnknownHoldAnsweredSooner failed on mote (10-29) with "TempDir RemoveAll cleanup: unlinkat …/magpie/held: directory not empty". The 504 is answered first and the held report is written after it (unrouted.go), so a test that returns on the 504 alone races the write against its TempDir's removal. TestUnroutedHeldReportSaved already waited for the route's Held; the three others that end on an unrouted 504 (UnknownHoldAnsweredSooner, RequestShownThenAnswered, MostLeavesOutMasking) now wait too, through heldSaved, for any probe/text route naming its report (once let go, the request may begin a route of its own beside the one shown). Run (mote): the four unrouted/held tests -count=3 with a 300ms sleep injected before writeHeld: ok; full ./internal/gateway in the previous commit's record. Broken by hand: not reproduced red. With the injected sleep the old tests still pass, since the late write lands after RemoveAll and recreates the folder rather than racing it; the cause is from the log and the code, not a forced red. At this head (both commits rebased on origin/main adc618bbf), on mote: go vet, GOOS=windows go vet and GOOS=linux go vet -tags nogui of ./internal/gateway ./internal/provider clean; go test -count=1 ./internal/provider ok; ./internal/gateway -run 'Factory|Refus|Policy|Unrouted|Held' ok. The full ./internal/gateway run (ok, 768s) was at the earlier base 96a703a59. Rebased once more onto fc43c69e5 (Codex effort change: codex_backend, decide, effort_update, gateway, quiet, responses; none of the files here) after that run; not run again on it. --- internal/gateway/unrouted_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/internal/gateway/unrouted_test.go b/internal/gateway/unrouted_test.go index 88e504268..d9b9f4b3c 100644 --- a/internal/gateway/unrouted_test.go +++ b/internal/gateway/unrouted_test.go @@ -136,6 +136,28 @@ func TestUnroutedUnknownHoldAnsweredSooner(t *testing.T) { if r.err != nil || r.code != http.StatusGatewayTimeout || !strings.Contains(r.body, "without routing it to any provider") { t.Fatalf("held request answered %d %q %v", r.code, r.body, r.err) } + heldSaved(t, s) +} + +// heldSaved waits for the report of the request s answered 504 for +// being held before routing: the 504 goes first and the report is saved +// after it, so a test that returns on the 504 alone has its HOME removed +// under the write ("TempDir RemoveAll cleanup: … magpie/held: directory +// not empty" on mote, 10-29). +func heldSaved(t *testing.T, s *Server) { + t.Helper() + for deadline := time.Now().Add(10 * time.Second); ; time.Sleep(10 * time.Millisecond) { + // any route: once let go, the request may have begun one of + // its own beside the one shown while it was held + for _, r := range routesOf(s, "probe/text") { + if r.Held != "" { + return + } + } + if time.Now().After(deadline) { + t.Fatalf("the held request's report was never saved: %+v", routesOf(s, "probe/text")) + } + } } // A body still arriving (an image over a slow link) is a known wait: a @@ -369,6 +391,7 @@ func TestUnroutedRequestShownThenAnswered(t *testing.T) { if len(rs) != 1 || !rs[0].Done || rs[0].Status != http.StatusGatewayTimeout || !strings.Contains(rs[0].Error, "without routing") { t.Fatalf("the held request's route: %+v", rs) } + heldSaved(t, s) } // One shown while it waited gives way to its own route once routed: the @@ -460,6 +483,7 @@ func TestUnroutedMostLeavesOutMasking(t *testing.T) { case <-time.After(20 * time.Second): t.Fatal("a request held after masking was never answered") } + heldSaved(t, s) } // A request another magpie passed on is shown, while it waits to be