mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 08:55:24 +00:00
e9a464e0cf
#16594 - [X] Changes file added for user-visible changes in `changes/` or `orbit/changes/`. See [Changes files](https://fleetdm.com/docs/contributing/committing-changes#changes-files) for more information. - [X] Added/updated tests - [X] Manual QA for all new/changed functionality - For Orbit and Fleet Desktop changes: - [X] Manual QA must be performed in the three main OSs, macOS, Windows and Linux. - [X] Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).
72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
package retry
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
var errTest = errors.New("test error")
|
|
|
|
func TestRetryDo(t *testing.T) {
|
|
t.Run("WithMaxAttempts only performs the operation the configured number of times", func(t *testing.T) {
|
|
count := 0
|
|
max := 3
|
|
|
|
err := Do(func() error {
|
|
count++
|
|
return errTest
|
|
}, WithMaxAttempts(max), WithInterval(1*time.Millisecond))
|
|
|
|
require.ErrorIs(t, errTest, err)
|
|
require.Equal(t, max, count)
|
|
})
|
|
|
|
t.Run("operations are run an unlimited number of times by default", func(t *testing.T) {
|
|
count := 0
|
|
max := 10
|
|
|
|
err := Do(func() error {
|
|
if count++; count != max {
|
|
return errTest
|
|
}
|
|
return nil
|
|
}, WithInterval(1*time.Millisecond))
|
|
|
|
require.NoError(t, err)
|
|
require.Equal(t, max, count)
|
|
})
|
|
|
|
t.Run("with backoff", func(t *testing.T) {
|
|
count := 0
|
|
max := 4
|
|
start := time.Now()
|
|
err := Do(func() error {
|
|
switch count {
|
|
case 0:
|
|
require.WithinDuration(t, start, time.Now(), 1*time.Millisecond)
|
|
case 1:
|
|
require.WithinDuration(t, start.Add(50*time.Millisecond), time.Now(), 10*time.Millisecond)
|
|
case 2:
|
|
require.WithinDuration(t, start.Add((50+100)*time.Millisecond), time.Now(), 10*time.Millisecond)
|
|
case 3:
|
|
require.WithinDuration(t, start.Add((50+100+200)*time.Millisecond), time.Now(), 10*time.Millisecond)
|
|
}
|
|
count++
|
|
if count != max {
|
|
return errTest
|
|
}
|
|
return nil
|
|
},
|
|
WithInterval(50*time.Millisecond),
|
|
WithBackoffMultiplier(2),
|
|
WithMaxAttempts(4),
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
require.Equal(t, max, count)
|
|
})
|
|
}
|