go-zero/core/fx/retry_test.go

167 lines
3.9 KiB
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package fx
import (
"context"
2020-07-26 17:09:05 +08:00
"errors"
"testing"
"time"
2020-07-26 17:09:05 +08:00
"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")
}))
times1 := 0
2021-02-19 17:49:39 +08:00
assert.Nil(t, DoWithRetry(func() error {
times1++
if times1 == defaultRetryTimes {
2020-07-26 17:09:05 +08:00
return nil
}
return errors.New("any")
}))
times2 := 0
2021-02-19 17:49:39 +08:00
assert.NotNil(t, DoWithRetry(func() error {
times2++
if times2 == defaultRetryTimes+1 {
2020-07-26 17:09:05 +08:00
return nil
}
return errors.New("any")
}))
2021-04-15 19:49:17 +08:00
total := 2 * defaultRetryTimes
times3 := 0
2021-02-19 17:49:39 +08:00
assert.Nil(t, DoWithRetry(func() error {
times3++
if times3 == total {
2020-07-26 17:09:05 +08:00
return nil
}
return errors.New("any")
2021-02-19 17:49:39 +08:00
}, WithRetry(total)))
2020-07-26 17:09:05 +08:00
}
func TestRetryWithTimeout(t *testing.T) {
assert.Nil(t, DoWithRetry(func() error {
return nil
2023-05-28 12:11:55 +08:00
}, WithTimeout(time.Millisecond*500)))
times1 := 0
assert.Nil(t, DoWithRetry(func() error {
times1++
if times1 == 1 {
return errors.New("any ")
}
2023-05-28 12:11:55 +08:00
time.Sleep(time.Millisecond * 150)
return nil
2023-05-28 12:11:55 +08:00
}, WithTimeout(time.Millisecond*250)))
total := defaultRetryTimes
times2 := 0
assert.Nil(t, DoWithRetry(func() error {
times2++
if times2 == total {
return nil
}
2023-05-28 12:11:55 +08:00
time.Sleep(time.Millisecond * 50)
return errors.New("any")
2023-05-28 12:11:55 +08:00
}, WithTimeout(time.Millisecond*50*(time.Duration(total)+2))))
assert.NotNil(t, DoWithRetry(func() error {
return errors.New("any")
2023-05-28 12:11:55 +08:00
}, WithTimeout(time.Millisecond*250)))
}
func TestRetryWithInterval(t *testing.T) {
times1 := 0
assert.NotNil(t, DoWithRetry(func() error {
times1++
if times1 == 1 {
return errors.New("any")
}
2023-05-28 12:11:55 +08:00
time.Sleep(time.Millisecond * 150)
return nil
2023-05-28 12:11:55 +08:00
}, WithTimeout(time.Millisecond*250), WithInterval(time.Millisecond*150)))
times2 := 0
assert.NotNil(t, DoWithRetry(func() error {
times2++
if times2 == 2 {
return nil
}
2023-05-28 12:11:55 +08:00
time.Sleep(time.Millisecond * 150)
return errors.New("any ")
2023-05-28 12:11:55 +08:00
}, WithTimeout(time.Millisecond*250), WithInterval(time.Millisecond*150)))
}
func TestRetryWithWithIgnoreErrors(t *testing.T) {
ignoreErr1 := errors.New("ignore error1")
ignoreErr2 := errors.New("ignore error2")
ignoreErrs := []error{ignoreErr1, ignoreErr2}
assert.Nil(t, DoWithRetry(func() error {
return ignoreErr1
}, WithIgnoreErrors(ignoreErrs)))
assert.Nil(t, DoWithRetry(func() error {
return ignoreErr2
}, WithIgnoreErrors(ignoreErrs)))
assert.NotNil(t, DoWithRetry(func() error {
return errors.New("any")
}))
}
func TestRetryCtx(t *testing.T) {
2023-10-15 21:39:44 +08:00
t.Run("with timeout", func(t *testing.T) {
assert.NotNil(t, DoWithRetryCtx(context.Background(), func(ctx context.Context, retryCount int) error {
if retryCount == 0 {
return errors.New("any")
}
time.Sleep(time.Millisecond * 150)
return nil
}, WithTimeout(time.Millisecond*250), WithInterval(time.Millisecond*150)))
assert.NotNil(t, DoWithRetryCtx(context.Background(), func(ctx context.Context, retryCount int) error {
if retryCount == 1 {
return nil
}
time.Sleep(time.Millisecond * 150)
return errors.New("any ")
}, WithTimeout(time.Millisecond*250), WithInterval(time.Millisecond*150)))
})
t.Run("with deadline exceeded", func(t *testing.T) {
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Millisecond*250))
defer cancel()
var times int
assert.Error(t, DoWithRetryCtx(ctx, func(ctx context.Context, retryCount int) error {
times++
time.Sleep(time.Millisecond * 150)
return errors.New("any")
2023-10-15 21:39:44 +08:00
}, WithInterval(time.Millisecond*150)))
assert.Equal(t, 1, times)
})
2023-10-15 21:39:44 +08:00
t.Run("with deadline not exceeded", func(t *testing.T) {
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Millisecond*250))
defer cancel()
var times int
assert.NoError(t, DoWithRetryCtx(ctx, func(ctx context.Context, retryCount int) error {
times++
if times == defaultRetryTimes {
return nil
}
time.Sleep(time.Millisecond * 50)
return errors.New("any")
}))
assert.Equal(t, defaultRetryTimes, times)
})
}