|
| 1 | +package backoff |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func TestRequestSizeBackOff(t *testing.T) { |
| 10 | + t.Run("max request size does not go above configured maximum", func(t *testing.T) { |
| 11 | + maxRequestSize := 1000 |
| 12 | + b := NewSizeLimitedBackOff(maxRequestSize) |
| 13 | + |
| 14 | + // Initial backoff value should be equal to the maxRequestSize |
| 15 | + assert.Equal(t, maxRequestSize, b.NextBackOff()) |
| 16 | + |
| 17 | + // After multiple successes, the backoff value should cap at maxRequestSize |
| 18 | + b.Success() |
| 19 | + assert.Equal(t, maxRequestSize, b.NextBackOff()) |
| 20 | + b.Success() |
| 21 | + assert.Equal(t, maxRequestSize, b.NextBackOff()) |
| 22 | + }) |
| 23 | + t.Run("min request size does not go below 1", func(t *testing.T) { |
| 24 | + // validate lower limit |
| 25 | + maxRequestSize := 5 |
| 26 | + b := NewSizeLimitedBackOff(maxRequestSize) |
| 27 | + assert.Equal(t, maxRequestSize, b.NextBackOff()) |
| 28 | + |
| 29 | + b.Failure() |
| 30 | + assert.Equal(t, 2, b.NextBackOff()) |
| 31 | + b.Failure() |
| 32 | + assert.Equal(t, 1, b.NextBackOff()) |
| 33 | + |
| 34 | + // backoff value should not go below 1 |
| 35 | + b.Failure() |
| 36 | + assert.Equal(t, 1, b.NextBackOff()) |
| 37 | + }) |
| 38 | + t.Run("backoff updates on Failure, Success and Reset", func(t *testing.T) { |
| 39 | + maxRequestSize := 1000 |
| 40 | + b := NewSizeLimitedBackOff(maxRequestSize) |
| 41 | + // After a failure, the backoff value should be halved |
| 42 | + b.Failure() |
| 43 | + assert.Equal(t, maxRequestSize/2, b.NextBackOff()) |
| 44 | + |
| 45 | + // After multiple failures, the backoff value should keep halving |
| 46 | + b.Failure() |
| 47 | + b.Failure() |
| 48 | + assert.Equal(t, maxRequestSize/8, b.NextBackOff()) |
| 49 | + |
| 50 | + // After success backoff value should keep doubling |
| 51 | + b.Success() |
| 52 | + assert.Equal(t, maxRequestSize/4, b.NextBackOff()) |
| 53 | + b.Success() |
| 54 | + assert.Equal(t, maxRequestSize/2, b.NextBackOff()) |
| 55 | + |
| 56 | + // Reset should set the backoff value back to the initial maxRequestSize |
| 57 | + b.Reset() |
| 58 | + assert.Equal(t, maxRequestSize, b.NextBackOff()) |
| 59 | + }) |
| 60 | +} |
0 commit comments