|
| 1 | +package defaults_test |
| 2 | + |
| 3 | +// Verifies the retry attempt headers added to volcengine/corehandlers: |
| 4 | +// X-Sdk-Invocation-Id must stay the same across every retry attempt of a single |
| 5 | +// logical request, and X-Sdk-Request must report the current/max attempt count. |
| 6 | + |
| 7 | +import ( |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/volcengine/volcengine-go-sdk/volcengine/client" |
| 13 | + "github.com/volcengine/volcengine-go-sdk/volcengine/client/metadata" |
| 14 | + "github.com/volcengine/volcengine-go-sdk/volcengine/defaults" |
| 15 | + "github.com/volcengine/volcengine-go-sdk/volcengine/request" |
| 16 | +) |
| 17 | + |
| 18 | +func TestAddRetryInfoHeaderAcrossRetries(t *testing.T) { |
| 19 | + var invocationIDs []string |
| 20 | + var attemptHeaders []string |
| 21 | + callCount := 0 |
| 22 | + |
| 23 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 24 | + callCount++ |
| 25 | + invocationIDs = append(invocationIDs, r.Header.Get("X-Sdk-Invocation-Id")) |
| 26 | + attemptHeaders = append(attemptHeaders, r.Header.Get("X-Sdk-Request")) |
| 27 | + if callCount < 3 { |
| 28 | + w.WriteHeader(http.StatusInternalServerError) |
| 29 | + return |
| 30 | + } |
| 31 | + w.WriteHeader(http.StatusOK) |
| 32 | + })) |
| 33 | + defer server.Close() |
| 34 | + |
| 35 | + cfg := defaults.Config().WithHTTPClient(server.Client()).WithRegion("test-region") |
| 36 | + handlers := defaults.Handlers() |
| 37 | + |
| 38 | + c := client.New(*cfg, metadata.ClientInfo{Endpoint: server.URL, SigningRegion: "test-region"}, handlers) |
| 39 | + c.Retryer = client.DefaultRetryer{NumMaxRetries: 2} |
| 40 | + |
| 41 | + req := c.NewRequest(&request.Operation{Name: "Test", HTTPMethod: "POST", HTTPPath: "/"}, &struct{}{}, &struct{}{}) |
| 42 | + if err := req.Send(); err != nil { |
| 43 | + t.Fatalf("expected success after retries, got error: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + if callCount != 3 { |
| 47 | + t.Fatalf("expected 3 attempts (1 initial + 2 retries), got %d", callCount) |
| 48 | + } |
| 49 | + |
| 50 | + for i, id := range invocationIDs { |
| 51 | + if id == "" { |
| 52 | + t.Fatalf("attempt %d: X-Sdk-Invocation-Id header missing", i+1) |
| 53 | + } |
| 54 | + if id != invocationIDs[0] { |
| 55 | + t.Fatalf("attempt %d: X-Sdk-Invocation-Id changed across retries: %q vs %q", i+1, id, invocationIDs[0]) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + wantAttempts := []string{"attempt=1; max=3", "attempt=2; max=3", "attempt=3; max=3"} |
| 60 | + for i, want := range wantAttempts { |
| 61 | + if attemptHeaders[i] != want { |
| 62 | + t.Errorf("attempt %d: X-Sdk-Request = %q, want %q", i+1, attemptHeaders[i], want) |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments