From 03ccac2230d3624da9b15c4c327974977826312e Mon Sep 17 00:00:00 2001 From: tian <11429339@qq.com> Date: Sun, 19 Apr 2026 14:02:07 +0800 Subject: [PATCH] Allow longer timeout for config apply --- internal/api/handlers_test.go | 43 ++++++++++++++++++++++++++++++++ internal/service/agent_client.go | 3 +++ 2 files changed, 46 insertions(+) diff --git a/internal/api/handlers_test.go b/internal/api/handlers_test.go index d1dc4fd..85e9ea0 100644 --- a/internal/api/handlers_test.go +++ b/internal/api/handlers_test.go @@ -12,6 +12,7 @@ import ( "strconv" "strings" "testing" + "time" "github.com/go-chi/chi/v5" ) @@ -225,3 +226,45 @@ func TestHandler_ProxyAgentMapsConfigCandidateApply(t *testing.T) { t.Fatalf("expected candidate apply response, got %s", rr.Body.String()) } } + +func TestHandler_ProxyAgentMapsLongRunningConfigCandidateApply(t *testing.T) { + agentServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + t.Fatalf("expected POST, got %s", r.Method) + } + if r.URL.Path != "/v1/config/candidate/apply" { + t.Fatalf("expected /v1/config/candidate/apply, got %s", r.URL.Path) + } + time.Sleep(3500 * time.Millisecond) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"ok":true,"status":{"metadata":{"config_id":"candidate"}}}`)) + })) + defer agentServer.Close() + + host, portText, err := net.SplitHostPort(strings.TrimPrefix(agentServer.URL, "http://")) + if err != nil { + t.Fatalf("parse test server address: %v", err) + } + port, err := strconv.Atoi(portText) + if err != nil { + t.Fatalf("parse test server port: %v", err) + } + + cfg := &config.Config{} + agent := service.NewAgentClient(cfg) + reg := service.NewRegistryService(cfg, agent) + reg.UpdateDevice(&models.Device{DeviceID: "edge-01", IP: host, AgentPort: port}) + h := NewHandler(nil, reg, agent, nil, nil) + + r := chi.NewRouter() + r.Post("/api/devices/{id}/config/candidate/apply", h.ProxyAgent) + rr := httptest.NewRecorder() + r.ServeHTTP(rr, httptest.NewRequest(http.MethodPost, "/api/devices/edge-01/config/candidate/apply", nil)) + + if rr.Code != http.StatusOK { + t.Fatalf("expected status 200, got %d: %s", rr.Code, rr.Body.String()) + } + if !strings.Contains(rr.Body.String(), "candidate") { + t.Fatalf("expected candidate apply response, got %s", rr.Body.String()) + } +} diff --git a/internal/service/agent_client.go b/internal/service/agent_client.go index 2f4b64d..97de352 100644 --- a/internal/service/agent_client.go +++ b/internal/service/agent_client.go @@ -101,6 +101,9 @@ func isLongOp(method, path string) bool { if method == http.MethodPut && path == "/v1/config" { return true } + if method == http.MethodPost && path == "/v1/config/candidate/apply" { + return true + } if strings.HasPrefix(path, "/v1/config/ui/") { return true }