Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/requesthelper/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func WithRetries(el *logging.ExtensionLogger, rm *RequestManager, sf SleepFunc)
func isTransientHTTPStatusCode(statusCode int) bool {
switch statusCode {
case
http.StatusNotFound, // 404. This will occur if there is a timing issue with the NodeService CCF cache
http.StatusRequestTimeout, // 408
http.StatusTooManyRequests, // 429
http.StatusInternalServerError, // 500
Expand Down
42 changes: 36 additions & 6 deletions internal/requesthelper/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,32 @@ func TestWithRetries_failingBadStatusCode_validateSleeps(t *testing.T) {
}

func TestWithRetries_healingServer(t *testing.T) {
srv := httptest.NewServer(new(healingServer))
h := &healingServer{
HealAfter: 4,
FailCode: 500,
SuccessCode: 200,
}
Comment on lines +151 to +155
Copy link

Copilot AI Jan 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use http.Status... constants instead of raw status-code integers (e.g., http.StatusInternalServerError, http.StatusOK) to avoid magic numbers and make intent clearer.

Copilot uses AI. Check for mistakes.
srv := httptest.NewServer(h)
defer srv.Close()

d := NewTestURLRequest(srv.URL)
rm := requesthelper.GetRequestManager(d, testRequestTimeout)
sr := new(sleepRecorder)
resp, err := requesthelper.WithRetries(nopLog(), rm, sr.Sleep)
defer resp.Body.Close()
require.Nil(t, err, "should eventually succeed")
require.NotNil(t, resp.Body, "response body exists")

require.Equal(t, sleepSchedule[:3], []time.Duration(*sr))
}

func TestWithRetries_retry404(t *testing.T) {
h := &healingServer{
HealAfter: 4,
FailCode: 404,
SuccessCode: 200,
}
srv := httptest.NewServer(h)
Comment on lines +170 to +176
Copy link

Copilot AI Jan 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this new test, the response body is closed via defer resp.Body.Close() before asserting err == nil / resp != nil (same pattern as the healing-server test). If the retry logic changes and the request doesn’t succeed, the test will panic with a nil-pointer dereference and mask the real failure. Prefer deferring the close only after confirming resp/resp.Body are non-nil (or guard the defer).

Copilot uses AI. Check for mistakes.
defer srv.Close()

d := NewTestURLRequest(srv.URL)
Expand All @@ -173,14 +198,19 @@ func (s *sleepRecorder) Sleep(d time.Duration) {
}

// healingServer returns HTTP 500 until 4th call, then HTTP 200 afterwards
type healingServer int
type healingServer struct {
calls int
HealAfter int // number of calls that must happen before we start succeeding
FailCode int
SuccessCode int
}

func (h *healingServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
*h++
if *h < 4 {
w.WriteHeader(http.StatusInternalServerError)
h.calls++
if h.calls < h.HealAfter {
w.WriteHeader(h.FailCode)
} else {
w.WriteHeader(http.StatusOK)
w.WriteHeader(h.SuccessCode)
}
}

Expand Down
Loading