-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretry_test.go
66 lines (44 loc) · 1.33 KB
/
retry_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package retry_test
import (
stderrors "errors"
"testing"
"github.com/IQ-tech/go-errors"
"github.com/IQ-tech/go-retry"
"github.com/stretchr/testify/assert"
)
func TestFunc(t *testing.T) {
t.Parallel()
t.Run("returns error if attempts are exceeded", func(t *testing.T) {
t.Parallel()
err := stderrors.New("message")
function := func() (interface{}, error) {
return nil, err
}
_, retryErr := retry.Func(retry.Options{Attempts: 3, InitialTimeBetweenRetries: 1}, function)
assert.Equal(t, errors.GetOriginalError(retryErr).Error(), err.Error())
})
t.Run("doesn't return error if function succeeds before attempts are exceeded", func(t *testing.T) {
t.Parallel()
err := stderrors.New("message")
attempts := 0
function := func() (interface{}, error) {
attempts++
if attempts == 2 {
return 1, nil
}
return nil, err
}
result, err := retry.Func(retry.Options{Attempts: 3, InitialTimeBetweenRetries: 1}, function)
assert.NoError(t, err)
assert.Equal(t, 1, result)
})
t.Run("doesn't return error if function succeeds in the first try", func(t *testing.T) {
t.Parallel()
function := func() (interface{}, error) {
return 2, nil
}
result, err := retry.Func(retry.Options{Attempts: 5, InitialTimeBetweenRetries: 5}, function)
assert.Nil(t, err)
assert.Equal(t, 2, result)
})
}