mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
Add custom backoff setter (#176)
Users should have the capability to customize the backoff pattern and accordingly adjust it within the retry mechanism.
This commit is contained in:
@@ -137,6 +137,34 @@ func TestRetryOneShotSucceeded(t *testing.T) {
|
||||
assert.Equal(1, number)
|
||||
}
|
||||
|
||||
func TestRetryWitCustomBackoffOneShotSucceeded(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestRetryWitCustomBackoffOneShotSucceeded")
|
||||
|
||||
var number int
|
||||
increaseNumber := func() error {
|
||||
number++
|
||||
if number == DefaultRetryTimes {
|
||||
return nil
|
||||
}
|
||||
return errors.New("error occurs")
|
||||
}
|
||||
|
||||
err := Retry(increaseNumber, RetryWithCustomBackoff(&TestCustomBackoffStrategy{interval: time.Microsecond * 50}))
|
||||
|
||||
assert.IsNil(err)
|
||||
assert.Equal(5, number)
|
||||
}
|
||||
|
||||
type TestCustomBackoffStrategy struct {
|
||||
interval time.Duration
|
||||
}
|
||||
|
||||
func (c *TestCustomBackoffStrategy) CalculateInterval() time.Duration {
|
||||
return c.interval + 1
|
||||
}
|
||||
|
||||
func TestRetryWithExponentialWithJitterBackoffShiftOneShotSucceeded(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user