From d0ccbbebc6f786004b0d3f72a1643a1e34d07c0e Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 15 Oct 2026 11:12:15 +0800 Subject: [PATCH] gateway: the proxy-down tests take a dead port no test server holds (ubuntu CI red at 707fbb249) TestPluginProxyDownDoesNotRest/http failed on ubuntu CI with "request 0: 200 ... from a": the test closed a listener to get a "down" proxy port, then started its httptest server, and Linux handed that just-closed port to the server, so the down proxy answered. TestProxyDownDoesNotRest picks its port the same way. Both now take the dead port through deadAddr after their servers are up, and it is never one of the servers' addresses. Run: gofmt -l; go vet -tags nogui ./internal/gateway/ for darwin, linux and windows; go test -tags nogui -count=3 -run 'TestPluginProxyDownDoesNotRest|TestProxyDownDoesNotRest' ./internal/gateway/ (ok); the whole internal/gateway package (ok, 446s). Tests only, so no other package is affected. --- internal/gateway/plugin_proxy_down_test.go | 8 +----- internal/gateway/proxy_down_test.go | 29 +++++++++++++++++----- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/internal/gateway/plugin_proxy_down_test.go b/internal/gateway/plugin_proxy_down_test.go index 3a7add575..d47e7a515 100644 --- a/internal/gateway/plugin_proxy_down_test.go +++ b/internal/gateway/plugin_proxy_down_test.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "io" - "net" "net/http" "net/http/httptest" "path/filepath" @@ -30,12 +29,6 @@ func TestPluginProxyDownDoesNotRest(t *testing.T) { } t.Setenv("MAGPIE_BUN", bun) t.Cleanup(plugin.Settle) - l, err := net.Listen("tcp", "127.0.0.1:0") - if err != nil { - t.Fatal(err) - } - down := l.Addr().String() - l.Close() // the proxy b goes through, which answers as the vendor good := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { io.ReadAll(r.Body) @@ -45,6 +38,7 @@ func TestPluginProxyDownDoesNotRest(t *testing.T) { fmt.Fprint(w, `data: {"id":"c","object":"chat.completion.chunk","model":"fake-1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}`+"\n\ndata: [DONE]\n\n") })) defer good.Close() + down := deadAddr(t, good.Listener.Addr().String()) t.Setenv("FAKE_BASE", "http://vendor.invalid/v1") ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) diff --git a/internal/gateway/proxy_down_test.go b/internal/gateway/proxy_down_test.go index 5c3ce1a13..27c29b765 100644 --- a/internal/gateway/proxy_down_test.go +++ b/internal/gateway/proxy_down_test.go @@ -5,6 +5,7 @@ import ( "net" "net/http" "net/http/httptest" + "slices" "strings" "testing" @@ -23,18 +24,13 @@ func TestProxyDownDoesNotRest(t *testing.T) { for _, k := range []string{"HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "ALL_PROXY", "all_proxy", "NO_PROXY", "no_proxy"} { t.Setenv(k, "") } - l, err := net.Listen("tcp", "127.0.0.1:0") - if err != nil { - t.Fatal(err) - } - down := l.Addr().String() - l.Close() // nothing listens there now up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { io.ReadAll(r.Body) w.Header().Set("Content-Type", "application/json") io.WriteString(w, `{"id":"c","object":"chat.completion","choices":[{"index":0,"message":{"role":"assistant","content":"from b"},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1}}`) })) t.Cleanup(up.Close) + down := deadAddr(t, up.Listener.Addr().String()) for _, p := range []provider.Provider{ {ID: "a", Name: "A", Key: "ka", Models: []string{"m"}, Chat: "http://a.test/v1", Proxy: scheme + "://" + down}, {ID: "b", Name: "B", Key: "kb", Models: []string{"m"}, Chat: up.URL + "/v1"}, @@ -69,3 +65,24 @@ func TestProxyDownDoesNotRest(t *testing.T) { }) } } + +// deadAddr is a loopback address nothing listens on, for a proxy that is +// down. It is taken after the test's own servers are up and is never one of +// theirs: a port just let go can be handed out again at once (Linux CI gave +// the closed port to the next httptest server, so the "down" proxy answered). +func deadAddr(t *testing.T, taken ...string) string { + t.Helper() + for range 20 { + l, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + a := l.Addr().String() + l.Close() + if !slices.Contains(taken, a) { + return a + } + } + t.Fatal("no free loopback port apart from the test's servers") + return "" +}