Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix shadowing in retry library #13

Merged
merged 1 commit into from
Aug 7, 2024
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
10 changes: 6 additions & 4 deletions types/retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func isExpectedErr(err error) bool {
}

// Do executes a func with retry
// TODO: check if this is needed, because is not being used.
// TODO: Remove this function, and make our programs to depend on retires based
// on some standard retry library
func Do(sleep time.Duration, maxSleepTime time.Duration, retryableFunc func() error) error {
if err := retryableFunc(); err != nil {
if isUnrecoverableErr(err) {
Expand All @@ -66,9 +67,10 @@ func Do(sleep time.Duration, maxSleepTime time.Duration, retryableFunc func() er
}

// Add some randomness to prevent thrashing
jitter, err := randDuration(int64(sleep))
if err != nil {
return err
// TODO: This duration should be passed by the caller
jitter, randomnessErr := randDuration(int64(sleep))
if randomnessErr != nil {
return randomnessErr
}
sleep = sleep + jitter/2

Expand Down
14 changes: 13 additions & 1 deletion types/retry/retry_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package retry

import (
"github.com/stretchr/testify/require"
"errors"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestUnrecoverableError(t *testing.T) {
Expand All @@ -19,3 +21,13 @@ func TestExpectedError(t *testing.T) {
})
require.NoError(t, err)
}

func TestDoNotShadowAnError(t *testing.T) {
var expectedError = errors.New("expected error")

err := Do(1*time.Second, 1*time.Second, func() error {
return expectedError
})
require.Error(t, err)
require.ErrorIs(t, err, expectedError)
}
Loading