2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-10-01 23:02:32 +00:00
|
|
|
package retry
|
2019-04-26 17:38:39 +00:00
|
|
|
|
|
|
|
import (
|
2020-10-01 05:14:21 +00:00
|
|
|
"context"
|
2022-10-17 21:57:48 +00:00
|
|
|
"fmt"
|
2020-10-01 05:14:21 +00:00
|
|
|
"math"
|
2019-04-26 17:38:39 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
func TestJitter(t *testing.T) {
|
|
|
|
repeat(t, "0 percent", func(t *testing.T) {
|
|
|
|
jitter := NewJitter(0)
|
2019-04-26 17:38:39 +00:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
baseTime := time.Duration(i) * time.Second
|
2020-10-01 05:14:21 +00:00
|
|
|
require.Equal(t, baseTime, jitter(baseTime))
|
2019-04-26 17:38:39 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
repeat(t, "10 percent", func(t *testing.T) {
|
|
|
|
jitter := NewJitter(10)
|
|
|
|
baseTime := 5000 * time.Millisecond
|
|
|
|
maxTime := 5500 * time.Millisecond
|
|
|
|
newTime := jitter(baseTime)
|
|
|
|
require.True(t, newTime > baseTime)
|
|
|
|
require.True(t, newTime <= maxTime)
|
2019-04-26 17:38:39 +00:00
|
|
|
})
|
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
repeat(t, "100 percent", func(t *testing.T) {
|
|
|
|
jitter := NewJitter(100)
|
|
|
|
baseTime := 1234 * time.Millisecond
|
|
|
|
maxTime := 2468 * time.Millisecond
|
|
|
|
newTime := jitter(baseTime)
|
|
|
|
require.True(t, newTime > baseTime)
|
|
|
|
require.True(t, newTime <= maxTime)
|
2019-04-26 17:38:39 +00:00
|
|
|
})
|
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
repeat(t, "overflow", func(t *testing.T) {
|
|
|
|
jitter := NewJitter(100)
|
|
|
|
baseTime := time.Duration(math.MaxInt64) - 2*time.Hour
|
|
|
|
newTime := jitter(baseTime)
|
|
|
|
require.Equal(t, baseTime, newTime)
|
2019-04-26 17:38:39 +00:00
|
|
|
})
|
2020-10-01 05:14:21 +00:00
|
|
|
}
|
2019-04-26 17:38:39 +00:00
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
func repeat(t *testing.T, name string, fn func(t *testing.T)) {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
fn(t)
|
|
|
|
}
|
2019-04-26 17:38:39 +00:00
|
|
|
})
|
2020-10-01 05:14:21 +00:00
|
|
|
}
|
2019-04-26 17:38:39 +00:00
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
func TestWaiter_Delay(t *testing.T) {
|
|
|
|
t.Run("zero value", func(t *testing.T) {
|
|
|
|
w := &Waiter{}
|
|
|
|
for i, expected := range []time.Duration{0, 1, 2, 4, 8, 16, 32, 64, 128} {
|
|
|
|
w.failures = uint(i)
|
|
|
|
require.Equal(t, expected*time.Second, w.delay(), "failure count: %d", i)
|
|
|
|
}
|
2019-04-26 17:38:39 +00:00
|
|
|
})
|
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
t.Run("with minimum wait", func(t *testing.T) {
|
|
|
|
w := &Waiter{MinWait: 5 * time.Second}
|
|
|
|
for i, expected := range []time.Duration{5, 5, 5, 5, 8, 16, 32, 64, 128} {
|
|
|
|
w.failures = uint(i)
|
|
|
|
require.Equal(t, expected*time.Second, w.delay(), "failure count: %d", i)
|
|
|
|
}
|
2019-04-26 17:38:39 +00:00
|
|
|
})
|
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
t.Run("with maximum wait", func(t *testing.T) {
|
|
|
|
w := &Waiter{MaxWait: 20 * time.Second}
|
|
|
|
for i, expected := range []time.Duration{0, 1, 2, 4, 8, 16, 20, 20, 20} {
|
|
|
|
w.failures = uint(i)
|
|
|
|
require.Equal(t, expected*time.Second, w.delay(), "failure count: %d", i)
|
2019-04-26 17:38:39 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
t.Run("with minimum failures", func(t *testing.T) {
|
|
|
|
w := &Waiter{MinFailures: 4}
|
|
|
|
for i, expected := range []time.Duration{0, 0, 0, 0, 0, 1, 2, 4, 8, 16} {
|
|
|
|
w.failures = uint(i)
|
|
|
|
require.Equal(t, expected*time.Second, w.delay(), "failure count: %d", i)
|
2019-04-26 17:38:39 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
t.Run("with factor", func(t *testing.T) {
|
|
|
|
w := &Waiter{Factor: time.Millisecond}
|
|
|
|
for i, expected := range []time.Duration{0, 1, 2, 4, 8, 16, 32, 64, 128} {
|
|
|
|
w.failures = uint(i)
|
|
|
|
require.Equal(t, expected*time.Millisecond, w.delay(), "failure count: %d", i)
|
2019-04-26 17:38:39 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
t.Run("with all settings", func(t *testing.T) {
|
|
|
|
w := &Waiter{
|
|
|
|
MinFailures: 2,
|
|
|
|
MinWait: 4 * time.Millisecond,
|
|
|
|
MaxWait: 20 * time.Millisecond,
|
|
|
|
Factor: time.Millisecond,
|
|
|
|
}
|
|
|
|
for i, expected := range []time.Duration{4, 4, 4, 4, 4, 4, 8, 16, 20, 20, 20} {
|
|
|
|
w.failures = uint(i)
|
|
|
|
require.Equal(t, expected*time.Millisecond, w.delay(), "failure count: %d", i)
|
2019-04-26 17:38:39 +00:00
|
|
|
}
|
|
|
|
})
|
2021-04-07 22:33:11 +00:00
|
|
|
|
|
|
|
t.Run("jitter can exceed MaxWait", func(t *testing.T) {
|
|
|
|
w := &Waiter{
|
|
|
|
MaxWait: 20 * time.Second,
|
|
|
|
Jitter: NewJitter(300),
|
|
|
|
|
|
|
|
failures: 30,
|
|
|
|
}
|
|
|
|
|
|
|
|
delay := w.delay()
|
|
|
|
require.True(t, delay > 20*time.Second, "expected delay %v to be greater than MaxWait %v", delay, w.MaxWait)
|
|
|
|
})
|
2020-10-01 05:14:21 +00:00
|
|
|
}
|
2019-04-26 17:38:39 +00:00
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
func TestWaiter_Wait(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
ctx := context.Background()
|
2019-04-26 17:38:39 +00:00
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
t.Run("first failure", func(t *testing.T) {
|
|
|
|
w := &Waiter{MinWait: time.Millisecond, Factor: 1}
|
|
|
|
elapsed, err := runWait(ctx, w)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assertApproximateDuration(t, elapsed, time.Millisecond)
|
|
|
|
require.Equal(t, w.failures, uint(1))
|
|
|
|
})
|
2019-04-26 17:38:39 +00:00
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
t.Run("max failures", func(t *testing.T) {
|
|
|
|
w := &Waiter{
|
|
|
|
MaxWait: 100 * time.Millisecond,
|
|
|
|
failures: 200,
|
2019-04-26 17:38:39 +00:00
|
|
|
}
|
2020-10-01 05:14:21 +00:00
|
|
|
elapsed, err := runWait(ctx, w)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assertApproximateDuration(t, elapsed, 100*time.Millisecond)
|
|
|
|
require.Equal(t, w.failures, uint(201))
|
2019-04-26 17:38:39 +00:00
|
|
|
})
|
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
t.Run("context deadline", func(t *testing.T) {
|
|
|
|
w := &Waiter{failures: 200, MinWait: time.Second}
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Millisecond)
|
|
|
|
t.Cleanup(cancel)
|
2019-04-26 17:38:39 +00:00
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
elapsed, err := runWait(ctx, w)
|
|
|
|
require.Equal(t, err, context.DeadlineExceeded)
|
|
|
|
assertApproximateDuration(t, elapsed, 5*time.Millisecond)
|
|
|
|
require.Equal(t, w.failures, uint(201))
|
2019-04-26 17:38:39 +00:00
|
|
|
})
|
|
|
|
}
|
2020-10-01 05:14:21 +00:00
|
|
|
|
2022-10-17 21:57:48 +00:00
|
|
|
func TestWaiter_RetryLoop(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
// Change the default factor so that we retry faster.
|
|
|
|
w := &Waiter{Factor: 1 * time.Millisecond}
|
|
|
|
|
|
|
|
t.Run("exits if operation is successful after a few reties", func(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
numRetries := 0
|
|
|
|
err := w.RetryLoop(ctx, func() error {
|
|
|
|
if numRetries < 2 {
|
|
|
|
numRetries++
|
|
|
|
return fmt.Errorf("operation not successful")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("errors if operation is never successful", func(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
err := w.RetryLoop(ctx, func() error {
|
|
|
|
return fmt.Errorf("operation not successful")
|
|
|
|
})
|
|
|
|
require.NotNil(t, err)
|
|
|
|
require.EqualError(t, err, "could not retry operation: operation not successful")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-01 05:14:21 +00:00
|
|
|
func runWait(ctx context.Context, w *Waiter) (time.Duration, error) {
|
|
|
|
before := time.Now()
|
|
|
|
err := w.Wait(ctx)
|
|
|
|
return time.Since(before), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertApproximateDuration(t *testing.T, actual time.Duration, expected time.Duration) {
|
|
|
|
t.Helper()
|
|
|
|
delta := 20 * time.Millisecond
|
|
|
|
min, max := expected-delta, expected+delta
|
|
|
|
if min < 0 {
|
|
|
|
min = 0
|
|
|
|
}
|
|
|
|
if actual < min || actual > max {
|
|
|
|
t.Fatalf("expected %v to be between %v and %v", actual, min, max)
|
|
|
|
}
|
|
|
|
}
|