|
| 1 | +package csplugin |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/cenkalti/backoff/v5" |
| 11 | + "github.com/sirupsen/logrus" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + errFail = errors.New("fail") |
| 18 | + errAlwaysFails = errors.New("always fails") |
| 19 | +) |
| 20 | + |
| 21 | +type fakeBackoff struct { |
| 22 | + retries []time.Duration |
| 23 | + idx int |
| 24 | +} |
| 25 | + |
| 26 | +func (f *fakeBackoff) NextBackOff() time.Duration { |
| 27 | + if f.idx >= len(f.retries) { |
| 28 | + return backoff.Stop |
| 29 | + } |
| 30 | + d := f.retries[f.idx] |
| 31 | + f.idx++ |
| 32 | + return d |
| 33 | +} |
| 34 | + |
| 35 | +func (f *fakeBackoff) Reset() { |
| 36 | + f.idx = 0 |
| 37 | +} |
| 38 | + |
| 39 | +func newFakeBackoff(durations ...time.Duration) backoffFactory { |
| 40 | + return func() backoff.BackOff { |
| 41 | + return &fakeBackoff{retries: durations} |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestRetryWithBackoff_SuccessAfterRetries(t *testing.T) { |
| 46 | + discard := logrus.New() |
| 47 | + discard.Out = io.Discard |
| 48 | + |
| 49 | + ctx := t.Context() |
| 50 | + |
| 51 | + var calls int |
| 52 | + |
| 53 | + fn := func(_ context.Context) error { |
| 54 | + calls++ |
| 55 | + if calls < 3 { |
| 56 | + return errFail |
| 57 | + } |
| 58 | + |
| 59 | + return nil |
| 60 | + } |
| 61 | + |
| 62 | + cfg := PluginConfig{TimeOut: 50 * time.Millisecond, MaxRetry: 5} |
| 63 | + err := retryWithBackoff(ctx, cfg, discard, fn, newFakeBackoff( |
| 64 | + 0, 0, 0, |
| 65 | + )) |
| 66 | + require.NoError(t, err) |
| 67 | + assert.Equal(t, 3, calls) |
| 68 | +} |
| 69 | + |
| 70 | +func TestRetryWithBackoff_ExhaustsRetries(t *testing.T) { |
| 71 | + discard := logrus.New() |
| 72 | + discard.Out = io.Discard |
| 73 | + |
| 74 | + ctx := t.Context() |
| 75 | + |
| 76 | + fn := func(_ context.Context) error { |
| 77 | + return errAlwaysFails |
| 78 | + } |
| 79 | + |
| 80 | + cfg := PluginConfig{TimeOut: 50 * time.Millisecond, MaxRetry: 2} |
| 81 | + err := retryWithBackoff(ctx, cfg, discard, fn, newFakeBackoff( |
| 82 | + 0, 0, 0, |
| 83 | + )) |
| 84 | + require.ErrorIs(t, err, errAlwaysFails) |
| 85 | +} |
| 86 | + |
| 87 | +func TestRetryWithBackoff_ContextCanceled(t *testing.T) { |
| 88 | + discard := logrus.New() |
| 89 | + discard.Out = io.Discard |
| 90 | + |
| 91 | + ctx, cancel := context.WithCancel(t.Context()) |
| 92 | + cancel() |
| 93 | + |
| 94 | + var calls int |
| 95 | + fn := func(ctx context.Context) error { |
| 96 | + calls++ |
| 97 | + select { |
| 98 | + case <-ctx.Done(): |
| 99 | + return ctx.Err() |
| 100 | + default: |
| 101 | + return nil |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + cfg := PluginConfig{TimeOut: 50 * time.Millisecond, MaxRetry: 3} |
| 106 | + err := retryWithBackoff(ctx, cfg, discard, fn, newFakeBackoff(0,0,0)) |
| 107 | + require.ErrorIs(t, err, context.Canceled) |
| 108 | + assert.Zero(t, calls) |
| 109 | + |
| 110 | + // XXX: do we attempt to notify if ctx is canceled? |
| 111 | + // assert.Equal(t, 1, calls, "fn should be called once and fail with canceled") |
| 112 | +} |
| 113 | + |
| 114 | +func TestRetryWithBackoff_PerAttemptTimeout(t *testing.T) { |
| 115 | + discard := logrus.New() |
| 116 | + discard.Out = io.Discard |
| 117 | + |
| 118 | + fn := func(ctx context.Context) error { |
| 119 | + <-ctx.Done() |
| 120 | + return ctx.Err() |
| 121 | + } |
| 122 | + |
| 123 | + cfg := PluginConfig{TimeOut: 10 * time.Millisecond, MaxRetry: 1} |
| 124 | + err := retryWithBackoff(t.Context(), cfg, discard, fn, newFakeBackoff(0)) |
| 125 | + require.ErrorIs(t, err, context.DeadlineExceeded) |
| 126 | +} |
| 127 | + |
| 128 | +func TestRetryWithBackoff_MaxElapsedTime(t *testing.T) { |
| 129 | + discard := logrus.New() |
| 130 | + discard.Out = io.Discard |
| 131 | + |
| 132 | + ctx := t.Context() |
| 133 | + |
| 134 | + fn := func(_ context.Context) error { |
| 135 | + return errAlwaysFails |
| 136 | + } |
| 137 | + |
| 138 | + // MaxRetry = 0 -> unlimited retries |
| 139 | + // but we override elapsed time to a very short value |
| 140 | + cfg := PluginConfig{TimeOut: 10 * time.Millisecond, MaxRetry: 0} |
| 141 | + |
| 142 | + err := retryWithBackoff(ctx, cfg, discard, fn, newFakeBackoff(0, 0, 0)) |
| 143 | + require.Error(t, err) |
| 144 | + assert.ErrorIs(t, err, errAlwaysFails) |
| 145 | +} |
| 146 | + |
| 147 | +func TestRetryWithBackoff_SuccessFirstTry(t *testing.T) { |
| 148 | + discard := logrus.New() |
| 149 | + discard.Out = io.Discard |
| 150 | + |
| 151 | + ctx := t.Context() |
| 152 | + |
| 153 | + fn := func(_ context.Context) error { |
| 154 | + return nil |
| 155 | + } |
| 156 | + |
| 157 | + cfg := PluginConfig{TimeOut: 50 * time.Millisecond, MaxRetry: 3} |
| 158 | + err := retryWithBackoff(ctx, cfg, discard, fn, newFakeBackoff(0, 0, 0)) |
| 159 | + require.NoError(t, err) |
| 160 | +} |
| 161 | + |
0 commit comments