-
Notifications
You must be signed in to change notification settings - Fork 28
/
retry_test.go
159 lines (116 loc) · 2.77 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package retry
import (
"errors"
"testing"
)
func TestRetry(t *testing.T) {
action := func(attempt uint) error {
return nil
}
err := Retry(action)
if err != nil {
t.Error("expected a nil error")
}
}
func TestRetryAttemptNumberIsAccurate(t *testing.T) {
var strategyAttemptNumber uint
var actionAttemptNumber uint
strategy := func(attempt uint) bool {
strategyAttemptNumber = attempt
return true
}
action := func(attempt uint) error {
actionAttemptNumber = attempt
return nil
}
err := Retry(action, strategy)
if err != nil {
t.Error("expected a nil error")
}
if strategyAttemptNumber != 0 {
t.Errorf(
"expected strategy to receive 0, received %v instead",
strategyAttemptNumber,
)
}
if actionAttemptNumber != 1 {
t.Errorf(
"expected action to receive 1, received %v instead",
actionAttemptNumber,
)
}
}
func TestRetryRetriesUntilNoErrorReturned(t *testing.T) {
const errorUntilAttemptNumber = 5
var attemptsMade uint
action := func(attempt uint) error {
attemptsMade = attempt
if errorUntilAttemptNumber == attempt {
return nil
}
return errors.New("erroring")
}
err := Retry(action)
if err != nil {
t.Error("expected a nil error")
}
if errorUntilAttemptNumber != attemptsMade {
t.Errorf(
"expected %d attempts to be made, but %d were made instead",
errorUntilAttemptNumber,
attemptsMade,
)
}
}
func TestShouldAttempt(t *testing.T) {
shouldAttempt := shouldAttempt(1)
if !shouldAttempt {
t.Error("expected to return true")
}
}
func TestShouldAttemptWithStrategy(t *testing.T) {
const attemptNumberShouldReturnFalse = 7
strategy := func(attempt uint) bool {
return (attemptNumberShouldReturnFalse != attempt)
}
should := shouldAttempt(1, strategy)
if !should {
t.Error("expected to return true")
}
should = shouldAttempt(1+attemptNumberShouldReturnFalse, strategy)
if !should {
t.Error("expected to return true")
}
should = shouldAttempt(attemptNumberShouldReturnFalse, strategy)
if should {
t.Error("expected to return false")
}
}
func TestShouldAttemptWithMultipleStrategies(t *testing.T) {
trueStrategy := func(attempt uint) bool {
return true
}
falseStrategy := func(attempt uint) bool {
return false
}
should := shouldAttempt(1, trueStrategy)
if !should {
t.Error("expected to return true")
}
should = shouldAttempt(1, falseStrategy)
if should {
t.Error("expected to return false")
}
should = shouldAttempt(1, trueStrategy, trueStrategy, trueStrategy)
if !should {
t.Error("expected to return true")
}
should = shouldAttempt(1, falseStrategy, falseStrategy, falseStrategy)
if should {
t.Error("expected to return false")
}
should = shouldAttempt(1, trueStrategy, trueStrategy, falseStrategy)
if should {
t.Error("expected to return false")
}
}