Allow longer timeout for config apply

This commit is contained in:
tian 2026-04-19 14:02:07 +08:00
parent 1731a30c5c
commit 03ccac2230
2 changed files with 46 additions and 0 deletions

View File

@ -12,6 +12,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
"time"
"github.com/go-chi/chi/v5" "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()) 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())
}
}

View File

@ -101,6 +101,9 @@ func isLongOp(method, path string) bool {
if method == http.MethodPut && path == "/v1/config" { if method == http.MethodPut && path == "/v1/config" {
return true return true
} }
if method == http.MethodPost && path == "/v1/config/candidate/apply" {
return true
}
if strings.HasPrefix(path, "/v1/config/ui/") { if strings.HasPrefix(path, "/v1/config/ui/") {
return true return true
} }