From a320356ca6cf2eca25ae620e056e1ed2ec6c7531 Mon Sep 17 00:00:00 2001 From: KonradStaniec Date: Wed, 7 Aug 2024 12:44:19 +0200 Subject: [PATCH] Fix regression in retries --- types/retry/retry.go | 10 ++++++---- types/retry/retry_test.go | 14 +++++++++++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/types/retry/retry.go b/types/retry/retry.go index e193a83a9..c26a5200b 100644 --- a/types/retry/retry.go +++ b/types/retry/retry.go @@ -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) { @@ -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 diff --git a/types/retry/retry_test.go b/types/retry/retry_test.go index af036dbab..2b048c971 100644 --- a/types/retry/retry_test.go +++ b/types/retry/retry_test.go @@ -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) { @@ -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) +}