-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify_internal_test.go
179 lines (152 loc) · 4.4 KB
/
spotify_internal_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
176
177
178
179
package main
import (
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const (
testMaxTries = 5
// Set the testDelay to something tiny so we don't wait forever for these tests.
// I think more ideally we'd use a mocked clock or whatever, but like... whatever.
testDelay = 10 * time.Millisecond
)
func TestWrapWithRetry(t *testing.T) {
testErr := errors.New("foo")
errorsNever := func() error {
return nil
}
errorsAlways := func() error {
return testErr
}
makeNErrorFunc := func(maxErrs int) func() error {
numErrs := 0
return func() error {
if numErrs < maxErrs {
numErrs++
return testErr
}
return nil
}
}
testMaxTries := 5
// Sanity check the max tries we're testing against.
if testMaxTries < 2 {
t.Fatalf("testMaxTries should be set to a number greater than 2")
}
errorsOnce := makeNErrorFunc(1)
errorsTwice := makeNErrorFunc(2)
errorsMaxTimesExactly := makeNErrorFunc(testMaxTries)
errorsOnceMoreThanMax := makeNErrorFunc(testMaxTries + 1)
errorsOneLessThanMax := makeNErrorFunc(testMaxTries - 1)
testCases := []struct {
name string
fun func() error
expectedErr error
}{
{
name: "never erroring function",
fun: errorsNever,
expectedErr: nil,
},
{
name: "always erroring function",
fun: errorsAlways,
expectedErr: testErr,
},
{
name: "once erroring function",
fun: errorsOnce,
expectedErr: nil,
},
{
name: "twice erroring function",
fun: errorsTwice,
expectedErr: nil,
},
{
name: "ones less than max erroring function",
fun: errorsOneLessThanMax,
expectedErr: nil,
},
{
name: "exactly max times erroring function",
fun: errorsMaxTimesExactly,
expectedErr: nil,
},
{
name: "one less than max erroring function",
fun: errorsOnceMoreThanMax,
expectedErr: testErr,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
actualErr := wrapInRetry(tc.fun, uint(testMaxTries), testDelay)
if tc.expectedErr != nil {
assert.ErrorIs(t, actualErr, testErr)
} else {
assert.NoError(t, actualErr)
}
})
}
}
func TestDelayIsHonored(t *testing.T) {
testMaxTries := uint(2)
retryDelay := 100 * time.Millisecond
start := time.Now()
err := wrapInRetry(func() error {
return errors.New("blah")
}, testMaxTries, retryDelay)
end := time.Now()
assert.Error(t, err)
actualElapsed := end.Sub(start)
// Since we have a maximum retry of 2, we would expect a delay after
// the first failure, then another delay after the second failure,
// and an immediate exit after the third. So, two delays:
expectedElapsed := 2 * retryDelay
// Now of course, there may be some delta here, since we can't have perfect timers:
assert.InDelta(t, expectedElapsed.Milliseconds(), actualElapsed.Milliseconds(), 20)
}
func TestWrapInRetryWithRet(t *testing.T) {
// Since wrapInRetryWithRet just wraps (no pun intended)
// wrapWithRetry, we aren't going to try testing anything too crazy.
testErr := errors.New("hi")
numErrs := 0
maxErrs := 2
expectedRet := 42
erroringFunc := func() (int, error) {
if numErrs < maxErrs {
numErrs++
return 0, testErr
}
return expectedRet, nil
}
actualRet, err := wrapInRetryWithRet(erroringFunc, 5, 10*time.Millisecond)
// We should not error.
assert.NoError(t, err)
// We should propagate the return value.
assert.Equal(t, actualRet, expectedRet)
// The function should have only been called maxErrs times.
assert.Equal(t, numErrs, maxErrs)
}
func TestErrorAllowList(t *testing.T) {
unallowedErr := errors.New("bad")
allowedErr := errors.New("good")
unallowedErrFunc := func() error {
return unallowedErr
}
allowedErrFunc := func() error {
return allowedErr
}
// Calling this for either the allowed or unallowed functions will
// both fail, because no error is considered to be allowed.
assert.ErrorIs(t, wrapInRetry(unallowedErrFunc, testMaxTries, testDelay), unallowedErr)
assert.ErrorIs(t, wrapInRetry(allowedErrFunc, testMaxTries, testDelay), allowedErr)
// However, calling it with an allow list that contains the
// allowedErr will only fail the unallowedErrFunc:
assert.ErrorIs(t, wrapInRetry(unallowedErrFunc, testMaxTries, testDelay, allowedErr), unallowedErr)
assert.ErrorIs(t, wrapInRetry(allowedErrFunc, testMaxTries, testDelay, allowedErr), allowedErr)
}