GetLocalHTTPResponseWithBackoff calls GetLocalHTTPResponse with an external retry/backoff strategy. It takes a number of attempts and an initialDelay duration. params follow the same conventions as GetLocalHTTPResponse (HTTPRequestOpts or int timeout). The inner call has MaxRetries forced to 1 to av
(t *testing.T, rawurl string, attempts int, initialDelay time.Duration, params ...any)
| 544 | // (HTTPRequestOpts or int timeout). The inner call has MaxRetries forced to 1 |
| 545 | // to avoid nested retries. |
| 546 | func GetLocalHTTPResponseWithBackoff(t *testing.T, rawurl string, attempts int, initialDelay time.Duration, params ...any) (string, *http.Response, error) { |
| 547 | if attempts < 1 { |
| 548 | attempts = 1 |
| 549 | } |
| 550 | // Build options from params but force inner MaxRetries to 1 to avoid nested retries. |
| 551 | innerOpts := parseHTTPRequestOpts(60, params...) |
| 552 | innerOpts.MaxRetries = 1 |
| 553 | |
| 554 | var lastBody string |
| 555 | var lastResp *http.Response |
| 556 | var lastErr error |
| 557 | delay := initialDelay |
| 558 | |
| 559 | for attempt := 1; attempt <= attempts; attempt++ { |
| 560 | body, resp, err := GetLocalHTTPResponse(t, rawurl, innerOpts) |
| 561 | if err == nil { |
| 562 | return body, resp, nil |
| 563 | } |
| 564 | lastBody = body |
| 565 | lastResp = resp |
| 566 | lastErr = err |
| 567 | |
| 568 | if attempt < attempts { |
| 569 | // Log and sleep with exponential backoff |
| 570 | t.Logf("GetLocalHTTPResponseWithBackoff attempt %d/%d failed for %s: %v; retrying after %s", attempt, attempts, rawurl, err, delay) |
| 571 | time.Sleep(delay) |
| 572 | // Double the delay for next attempt |
| 573 | delay *= 2 |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | return lastBody, lastResp, lastErr |
| 578 | } |
| 579 | |
| 580 | // EnsureLocalHTTPContent will verify a URL responds with a 200, expected content string, and optional parameters. |
| 581 | // Parameters can be either: |