mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-21 06:31:07 +02:00
I have an upcoming PR for event notifications that needs similar exponential backoff logic, and I prefer the API and logic in the auto-auth exponential backoff rather than that of github.com/cenkalti/backoff/v3. This does have a small behavior change: the auto-auth min backoff will now be randomly reduced by up to 25% on the first call. This is a desirable property to avoid thundering herd problems, where a bunch of agents won't all try have the same retry timeout.
53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package backoff
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// TestBackoff_Basic tests that basic exponential backoff works as expected up to a max of 3 times.
|
|
func TestBackoff_Basic(t *testing.T) {
|
|
for i := 0; i < 100; i++ {
|
|
b := NewBackoff(3, 1*time.Millisecond, 10*time.Millisecond)
|
|
x, err := b.Next()
|
|
assert.Nil(t, err)
|
|
assert.LessOrEqual(t, x, 1*time.Millisecond)
|
|
assert.GreaterOrEqual(t, x, 750*time.Microsecond)
|
|
|
|
x2, err := b.Next()
|
|
assert.Nil(t, err)
|
|
assert.LessOrEqual(t, x2, x*2)
|
|
assert.GreaterOrEqual(t, x2, x*3/4)
|
|
|
|
x3, err := b.Next()
|
|
assert.Nil(t, err)
|
|
assert.LessOrEqual(t, x3, x2*2)
|
|
assert.GreaterOrEqual(t, x3, x2*3/4)
|
|
|
|
_, err = b.Next()
|
|
assert.NotNil(t, err)
|
|
}
|
|
}
|
|
|
|
// TestBackoff_ZeroRetriesAlwaysFails checks that if retries is set to zero, then an error is returned immediately.
|
|
func TestBackoff_ZeroRetriesAlwaysFails(t *testing.T) {
|
|
b := NewBackoff(0, 1*time.Millisecond, 10*time.Millisecond)
|
|
_, err := b.Next()
|
|
assert.NotNil(t, err)
|
|
}
|
|
|
|
// TestBackoff_MaxIsEnforced checks that the maximum backoff is enforced.
|
|
func TestBackoff_MaxIsEnforced(t *testing.T) {
|
|
b := NewBackoff(1001, 1*time.Millisecond, 2*time.Millisecond)
|
|
for i := 0; i < 1000; i++ {
|
|
x, err := b.Next()
|
|
assert.LessOrEqual(t, x, 2*time.Millisecond)
|
|
assert.Nil(t, err)
|
|
}
|
|
}
|