forked from rafaeljesus/retry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retry_test.go
113 lines (96 loc) · 2.16 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package retry
import (
"errors"
"net/http"
"testing"
"time"
)
var (
errFail = errors.New("fail")
)
func TestRetry(t *testing.T) {
t.Parallel()
tests := []struct {
scenario string
function func(*testing.T)
}{
{
scenario: "do retry",
function: testDoRetry,
},
{
scenario: "do retry with fail",
function: testDoRetryWithFail,
},
{
scenario: "do http retry",
function: testDoHTTPRetry,
},
{
scenario: "do http retry with fail",
function: testDoHTTPRetryWithFail,
},
}
for _, test := range tests {
t.Run(test.scenario, func(t *testing.T) {
test.function(t)
})
}
}
func testDoRetry(t *testing.T) {
attemptsCount := 0
fn := func() error {
attemptsCount++
return nil
}
err := Do(fn, 2, time.Second)
if err != nil {
t.Errorf("retry.Do returned wrong err value: got %v want %v", err, nil)
}
if attemptsCount != 1 {
t.Errorf("attemptsCount returned wrong count value: got %v want %v", attemptsCount, 1)
}
}
func testDoRetryWithFail(t *testing.T) {
errFail := errors.New("fail")
attemptsCount := 0
fail := func() error {
attemptsCount++
return errFail
}
err := Do(fail, 2, time.Second)
if err == nil {
t.Errorf("retry.Do returned wrong err value: got %v want %v", err, errFail)
}
if attemptsCount != 2 {
t.Errorf("attemptsCount returned wrong count value: got %v want %v", attemptsCount, 2)
}
}
func testDoHTTPRetry(t *testing.T) {
attemptsCount := 0
fn := func() (*http.Response, error) {
attemptsCount++
return &http.Response{}, nil
}
_, err := DoHTTP(fn, 2, time.Second)
if err != nil {
t.Errorf("retry.DoHTTP returned wrong err value: got %v want %v", err, nil)
}
if attemptsCount != 1 {
t.Errorf("attemptsCount returned wrong count value: got %v want %v", attemptsCount, 1)
}
}
func testDoHTTPRetryWithFail(t *testing.T) {
attemptsCount := 0
fn := func() (*http.Response, error) {
attemptsCount++
return &http.Response{}, errFail
}
_, err := DoHTTP(fn, 2, time.Second)
if err == nil {
t.Errorf("retry.DoHTTP returned wrong err value: got %v want %v", err, nil)
}
if attemptsCount != 2 {
t.Errorf("attemptsCount returned wrong count value: got %v want %v", attemptsCount, 2)
}
}