-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretry_test.go
175 lines (141 loc) · 5.01 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package retry
import (
"context"
"errors"
"fmt"
"os"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestRetry_Run(t *testing.T) {
// fake error
var testErr = errors.New("test error")
t.Run("r.Run returns nothing with error", func(t *testing.T) {
tries := 0
start := time.Now()
var last time.Time
retry := NewRetry(5, 50*time.Millisecond, 50*time.Millisecond)
res, err := retry.Run(func() (interface{}, error) {
tries++
last = time.Now()
return nil, testErr
})
assert.Equal(t, tries, 5, fmt.Sprintf("expected 5 tries, got %d", tries))
assert.Equal(t, err, testErr, fmt.Sprintf("err should equal testErr, got: %v", err))
assert.Equal(t, res, nil)
// CI environments and VMs are unreliable when it comes to firing
// time.Timer, so this assertion is skipped on CI, because it was
// causing random failures.
if _, present := os.LookupEnv("CI"); present {
max := 5 * (50 + 50) * time.Millisecond
assert.Less(
t, last.Sub(start).Milliseconds(), max.Milliseconds(),
fmt.Sprintf("should have taken less than %v, took %v", max.Milliseconds(), last.Sub(start).Milliseconds()),
)
}
})
t.Run("r.Run returns something after three retries", func(t *testing.T) {
tries := 0
retry := NewRetry(5, 50*time.Millisecond, 50*time.Millisecond)
res, err := retry.Run(func() (interface{}, error) {
if tries == 3 {
return tries, nil
}
tries++
return nil, testErr
})
assert.Equal(t, tries, 3, fmt.Sprintf("expected 3 tries, got %d", tries))
assert.Equal(t, err, nil, fmt.Sprintf("err should equal testErr, got: %v", err))
assert.Equal(t, res, 3)
})
}
func TestRetry_RunWithContext(t *testing.T) {
// fake error
var testErr = errors.New("test error")
t.Run("r.RunWithContext proper timeout", func(t *testing.T) {
tries := 0
start := time.Now()
var last time.Time
retry := NewRetry(5, 50*time.Millisecond, 50*time.Millisecond)
ctx, cancel := context.WithTimeout(context.Background(), 350*time.Millisecond)
defer cancel()
res, err := retry.RunWithContext(ctx, func(ctx context.Context) (interface{}, error) {
tries++
last = time.Now()
return nil, testErr
})
assert.Equal(t, tries, 5, fmt.Sprintf("expected 5 tries, got %d", tries))
assert.Equal(t, err, testErr, fmt.Sprintf("err should equal errTest, got: %v", err))
assert.Equal(t, res, nil)
// CI environments and VMs are unreliable when it comes to firing
// time.Timer, so this assertion is skipped on CI, because it was
// causing random failures.
if _, present := os.LookupEnv("CI"); present {
max := 5 * (50 + 50) * time.Millisecond
assert.Less(
t, last.Sub(start).Milliseconds(), max.Milliseconds(),
fmt.Sprintf("should have taken less than %v, took %v", max.Milliseconds(), last.Sub(start).Milliseconds()),
)
}
})
t.Run("r.RunWithContext small timeout", func(t *testing.T) {
tries := 0
start := time.Now()
var last time.Time
retry := NewRetry(5, 50*time.Millisecond, 50*time.Millisecond)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
res, err := retry.RunWithContext(ctx, func(ctx context.Context) (interface{}, error) {
tries++
last = time.Now()
return nil, testErr
})
assert.Less(t, tries, 5, fmt.Sprintf("expected less than 5 tries, got %d", tries))
assert.Equal(t, err, testErr, fmt.Sprintf("err should equal errTest, got: %v", err))
assert.Equal(t, res, nil)
// CI environments and VMs are unreliable when it comes to firing
// time.Timer, so this assertion is skipped on CI, because it was
// causing random failures.
if _, present := os.LookupEnv("CI"); present {
max := 5 * (50 + 50) * time.Millisecond
assert.Less(
t, last.Sub(start).Milliseconds(), max.Milliseconds(),
fmt.Sprintf("should have taken less than %v, took %v", max.Milliseconds(), last.Sub(start).Milliseconds()),
)
}
})
t.Run("exit due to context cancellation", func(t *testing.T) {
var wg sync.WaitGroup
var err error
tries := 0
retry := NewRetry(5, 50*time.Millisecond, 50*time.Millisecond)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
wg.Add(1)
go func() {
_, err = retry.RunWithContext(ctx, func(ctx context.Context) (interface{}, error) {
tries++
return nil, testErr
})
wg.Done()
}()
time.Sleep(200 * time.Millisecond)
cancel()
wg.Wait()
assert.GreaterOrEqual(t, tries, 1, fmt.Sprintf("expected at least 1, got %d", tries))
assert.Equal(t, err, testErr, fmt.Sprintf("err should equal Test error, got: %v", err))
})
t.Run("less than zero durations", func(t *testing.T) {
var tries int
retry := NewRetry(-1, -1, -1)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
_, err := retry.RunWithContext(ctx, func(ctx context.Context) (interface{}, error) {
tries++
return nil, testErr
})
assert.GreaterOrEqual(t, tries, 1, fmt.Sprintf("expected at less than 3, got %d", tries))
assert.Equal(t, err, testErr, fmt.Sprintf("err should equal Test error, got: %v", err))
})
}