diff --git a/options.go b/options.go index 5577ee7..f79fa98 100644 --- a/options.go +++ b/options.go @@ -124,6 +124,8 @@ func BackOffDelay(n uint, _ error, config *Config) time.Duration { config.maxBackOffN = max - uint(math.Floor(math.Log2(float64(config.delay)))) } + n-- + if n > config.maxBackOffN { n = config.maxBackOffN } diff --git a/retry_test.go b/retry_test.go index 1ee3739..2a1ec11 100644 --- a/retry_test.go +++ b/retry_test.go @@ -294,7 +294,7 @@ func TestBackOffDelay(t *testing.T) { delay: -1, expectedMaxN: 62, n: 2, - expectedDelay: 4, + expectedDelay: 2, }, { label: "zero-delay", @@ -310,6 +310,13 @@ func TestBackOffDelay(t *testing.T) { n: 62, expectedDelay: time.Second << 33, }, + { + label: "one-second-n", + delay: time.Second, + expectedMaxN: 33, + n: 1, + expectedDelay: time.Second, + }, } { t.Run( c.label, @@ -489,7 +496,7 @@ func TestContext(t *testing.T) { }) t.Run("timed out on retry infinte attempts - wraps context error with last retried function error", func(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*500) + ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*200) defer cancel() retrySum := 0