refactor(http): extract retry delay unit to variable
Extract hardcoded retry delay unit to a variable for better testability and flexibility. Update tests to use milliseconds for faster execution while maintaining the same behavior.
This commit is contained in:
parent
1aa556829d
commit
57ad26aee8
2 changed files with 11 additions and 2 deletions
|
|
@ -9,6 +9,8 @@ import (
|
|||
|
||||
const maxRetries = 3
|
||||
|
||||
var retryDelayUnit = time.Second
|
||||
|
||||
func shouldRetry(statusCode int) bool {
|
||||
return statusCode == http.StatusTooManyRequests ||
|
||||
statusCode >= 500
|
||||
|
|
@ -34,7 +36,7 @@ func DoRequestWithRetry(client *http.Client, req *http.Request) (*http.Response,
|
|||
}
|
||||
|
||||
if i < maxRetries-1 {
|
||||
if err = sleepWithCtx(req.Context(), time.Second*time.Duration(i+1)); err != nil {
|
||||
if err = sleepWithCtx(req.Context(), retryDelayUnit*time.Duration(i+1)); err != nil {
|
||||
return nil, fmt.Errorf("failed to sleep: %w", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,9 @@ import (
|
|||
)
|
||||
|
||||
func TestDoRequestWithRetry(t *testing.T) {
|
||||
retryDelayUnit = time.Millisecond
|
||||
t.Cleanup(func() { retryDelayUnit = time.Second })
|
||||
|
||||
testcases := []struct {
|
||||
name string
|
||||
serverBehavior func(*httptest.Server) int
|
||||
|
|
@ -53,6 +56,7 @@ func TestDoRequestWithRetry(t *testing.T) {
|
|||
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
attempts := 0
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
attempts++
|
||||
|
|
@ -91,6 +95,9 @@ func TestDoRequestWithRetry(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestDoRequestWithRetry_Delay(t *testing.T) {
|
||||
retryDelayUnit = time.Millisecond
|
||||
t.Cleanup(func() { retryDelayUnit = time.Second })
|
||||
|
||||
var start time.Time
|
||||
delays := []time.Duration{}
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -124,5 +131,5 @@ func TestDoRequestWithRetry_Delay(t *testing.T) {
|
|||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
resp.Body.Close()
|
||||
|
||||
assert.GreaterOrEqual(t, delays[2], time.Second)
|
||||
assert.GreaterOrEqual(t, delays[2], time.Millisecond)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue