-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretryer_test.go
146 lines (112 loc) · 2.54 KB
/
retryer_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
package retryer
import (
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestRetry_WithBoolean_CallAsRequested(t *testing.T) {
counter := 0
Retry(10).
ExecuteBool(func() bool {
counter++
if counter == 100 {
return true
}
return false
})
assert.Equal(t, counter, 10)
}
func TestRetry_WithBoolean_CallLessIfTrue(t *testing.T) {
counter := 0
Retry(10).
ExecuteBool(func() bool {
counter++
if counter == 3 {
return true
}
return false
})
assert.Equal(t, counter, 3)
}
func TestRetry_WithError_CallAsRequested(t *testing.T) {
counter := 0
Retry(10).
ExecuteError(func() error {
counter++
if counter == 100 {
return nil
}
return errors.New("Error")
})
assert.Equal(t, counter, 10)
}
func TestRetry_WithError_CallLessIfTrue(t *testing.T) {
counter := 0
Retry(10).
ExecuteError(func() error {
counter++
if counter == 3 {
return nil
}
return errors.New("Error")
})
assert.Equal(t, counter, 3)
}
func TestRetryForever_WithBoolean_CallAsRequested(t *testing.T) {
counter := 0
RetryForever().
ExecuteBool(func() bool {
counter++
if counter == 100 {
return true
}
return false
})
assert.Equal(t, counter, 100)
}
func TestRetryAndWait_WithBoolean_CallAsRequested(t *testing.T) {
counter := 0
t0 := time.Now()
RetryAndWait([]time.Duration{1000 * time.Millisecond, 1000 * time.Millisecond}).
ExecuteBool(func() bool {
counter++
if counter == 10 {
return true
}
return false
})
t1 := time.Now()
diff := t1.Sub(t0)
assert.GreaterOrEqual(t, int64(diff), int64(2000*time.Millisecond))
}
func TestRetryAndWait_WithBoolean_CallLessIfTrue(t *testing.T) {
counter := 0
t0 := time.Now()
RetryAndWait([]time.Duration{1000 * time.Millisecond, 1000 * time.Millisecond}).
ExecuteBool(func() bool {
counter++
if counter == 1 {
return true
}
return false
})
t1 := time.Now()
diff := t1.Sub(t0)
assert.GreaterOrEqual(t, int64(diff), int64(0))
}
func TestRetryAndWaitForever_WithBoolean_CallAsRequested(t *testing.T) {
counter := 0
t0 := time.Now()
RetryAndWaitForever(func(attempt int) time.Duration { return time.Duration(attempt*100) * time.Millisecond }).
ExecuteBool(func() bool {
counter++
if counter == 3 {
return true
}
return false
})
t1 := time.Now()
diff := t1.Sub(t0)
assert.GreaterOrEqual(t, int64(diff), int64(0))
}