2020-07-26 17:09:05 +08:00
|
|
|
package fx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRetry(t *testing.T) {
|
2021-02-19 17:49:39 +08:00
|
|
|
assert.NotNil(t, DoWithRetry(func() error {
|
2020-07-26 17:09:05 +08:00
|
|
|
return errors.New("any")
|
|
|
|
}))
|
|
|
|
|
|
|
|
var times int
|
2021-02-19 17:49:39 +08:00
|
|
|
assert.Nil(t, DoWithRetry(func() error {
|
2020-07-26 17:09:05 +08:00
|
|
|
times++
|
|
|
|
if times == defaultRetryTimes {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("any")
|
|
|
|
}))
|
|
|
|
|
|
|
|
times = 0
|
2021-02-19 17:49:39 +08:00
|
|
|
assert.NotNil(t, DoWithRetry(func() error {
|
2020-07-26 17:09:05 +08:00
|
|
|
times++
|
|
|
|
if times == defaultRetryTimes+1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("any")
|
|
|
|
}))
|
|
|
|
|
2021-04-15 19:49:17 +08:00
|
|
|
total := 2 * defaultRetryTimes
|
2020-07-26 17:09:05 +08:00
|
|
|
times = 0
|
2021-02-19 17:49:39 +08:00
|
|
|
assert.Nil(t, DoWithRetry(func() error {
|
2020-07-26 17:09:05 +08:00
|
|
|
times++
|
|
|
|
if times == total {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("any")
|
2021-02-19 17:49:39 +08:00
|
|
|
}, WithRetry(total)))
|
2020-07-26 17:09:05 +08:00
|
|
|
}
|