-
Notifications
You must be signed in to change notification settings - Fork 4
/
throttler_test.go
331 lines (317 loc) · 7.52 KB
/
throttler_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package alertnotification
import (
"errors"
"fmt"
"os"
"reflect"
"testing"
"time"
"github.com/GitbookIO/diskache"
)
func TestNewThrottler(t *testing.T) {
tests := []struct {
name string
want Throttler
}{
{
name: "default",
want: Throttler{
CacheOpt: fmt.Sprintf("/tmp/cache/%v_throttler_disk_cache", os.Getenv("APP_NAME")),
ThrottleDuration: 5,
GraceDuration: 0,
},
},
{
name: "change duration",
want: Throttler{
CacheOpt: fmt.Sprintf("/tmp/cache/%v_throttler_disk_cache", os.Getenv("APP_NAME")),
ThrottleDuration: 7,
GraceDuration: 5,
},
},
{
name: "change both",
want: Throttler{
CacheOpt: "new_cache_dir",
ThrottleDuration: 8,
GraceDuration: 0,
},
},
}
for _, tt := range tests {
if tt.name == "change duration" {
os.Setenv("THROTTLE_DURATION", "7")
os.Setenv("THROTTLE_GRACE_SECONDS", "5")
} else if tt.name == "change both" {
os.Setenv("THROTTLE_DURATION", "8")
os.Setenv("THROTTLE_GRACE_SECONDS", "0")
os.Setenv("THROTTLE_DISKCACHE_DIR", "new_cache_dir")
} else if tt.name == "default" {
os.Setenv("THROTTLE_GRACE_SECONDS", "")
}
t.Run(tt.name, func(t *testing.T) {
got := NewThrottler()
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewThrottler() = %v, want %v", got, tt.want)
}
})
}
}
func TestThrottler_IsThrottledOrGraced(t *testing.T) {
type fields struct {
CacheOpt string
ThrottleDuration int
GraceDuration int
}
type args struct {
ocError error
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{
name: "default",
fields: fields{
CacheOpt: fmt.Sprintf("/tmp/cache/%v_throttler_disk_cache", os.Getenv("APP_NAME")),
ThrottleDuration: 5,
GraceDuration: 0,
},
args: args{
ocError: errors.New("test_throttling"),
},
want: false,
},
{
name: "throttled_true",
fields: fields{
CacheOpt: fmt.Sprintf("/tmp/cache/%v_throttler_disk_cache", os.Getenv("APP_NAME")),
ThrottleDuration: 5,
GraceDuration: 0,
},
args: args{
ocError: errors.New("test_throttling"),
},
want: true,
},
{
name: "graced_true",
fields: fields{
CacheOpt: fmt.Sprintf("/tmp/cache/%v_throttler_disk_cache", os.Getenv("APP_NAME")),
ThrottleDuration: 5,
GraceDuration: 25,
},
args: args{
ocError: errors.New("test_throttling"),
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := &Throttler{
CacheOpt: tt.fields.CacheOpt,
ThrottleDuration: tt.fields.ThrottleDuration,
GraceDuration: tt.fields.GraceDuration,
}
if tt.name == "throttled_true" {
if err := th.ThrottleError(tt.args.ocError); err != nil {
t.Errorf("testing failed : %+v", err)
}
}
if got := th.IsThrottledOrGraced(tt.args.ocError); got != tt.want {
t.Errorf("Throttler.IsThrottled() = %v, want %v", got, tt.want)
}
err := th.CleanThrottlingCache()
if err != nil {
t.Errorf("Cannot clean after test. %+v", err)
}
})
}
}
func TestThrottler_ThrottleError(t *testing.T) {
type fields struct {
CacheOpt string
ThrottleDuration int
GraceDuration int
}
type args struct {
errObj error
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "default",
fields: fields{
CacheOpt: fmt.Sprintf("/tmp/cache/%v_throttler_disk_cache", os.Getenv("APP_NAME")),
ThrottleDuration: 5,
GraceDuration: 0,
},
args: args{
errObj: errors.New("test_throttling"),
},
wantErr: false,
},
{
name: "test_error",
fields: fields{
CacheOpt: "/no_permission_dir",
ThrottleDuration: 5,
GraceDuration: 0,
},
args: args{
errObj: errors.New("test_throttling"),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := &Throttler{
CacheOpt: tt.fields.CacheOpt,
ThrottleDuration: tt.fields.ThrottleDuration,
}
if err := th.ThrottleError(tt.args.errObj); (err != nil) != tt.wantErr {
t.Errorf("Throttler.ThrottleError() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.name == "default" && !th.IsThrottledOrGraced(tt.args.errObj) {
t.Errorf("Throttler.ThrottleError() error = %v, wantErr %v", errors.New("throttling failed"), tt.wantErr)
}
if !tt.wantErr {
err := th.CleanThrottlingCache()
if err != nil {
t.Errorf("Cannot clean after test. %+v", err)
}
}
})
}
}
func TestThrottler_getDiskCache(t *testing.T) {
type fields struct {
CacheOpt string
ThrottleDuration int
}
cachePart := fmt.Sprintf("/tmp/cache/%v_throttler_disk_cache", os.Getenv("APP_NAME"))
opts := diskache.Opts{
Directory: cachePart,
}
dc, err := diskache.New(&opts)
if err != nil {
t.Errorf("Throttler.getDiskCache() error = %v", err)
return
}
tests := []struct {
name string
fields fields
want *diskache.Diskache
wantErr bool
}{
{
name: "TestThrottler_getDiskCache_success",
fields: fields{
CacheOpt: cachePart,
ThrottleDuration: 5,
},
want: dc,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := &Throttler{
CacheOpt: tt.fields.CacheOpt,
ThrottleDuration: tt.fields.ThrottleDuration,
}
got, err := th.getDiskCache()
if (err != nil) != tt.wantErr {
t.Errorf("Throttler.getDiskCache() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Throttler.getDiskCache() = %v, want %v", got, tt.want)
}
})
}
}
func Test_isOverThrottleDuration(t *testing.T) {
type args struct {
cachedTime string
throttleDuration int
graceDuration int
}
tests := []struct {
name string
args args
want bool
}{
{
name: "Test_isOverThrottleDuration_true",
args: args{
cachedTime: time.Now().Add(-3 * time.Minute).Format(time.RFC3339), // -3 minutes => pass 2 minutes durations
throttleDuration: 2,
graceDuration: 0,
},
want: true,
},
{
name: "Test_isOverThrottleDuration_false",
args: args{
cachedTime: time.Now().Add(1 * time.Minute).Format(time.RFC3339), // 1 minute ahead of current < throtte duration 2
throttleDuration: 2,
graceDuration: 0,
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isOverThrottleDuration(tt.args.cachedTime, tt.args.throttleDuration); got != tt.want {
t.Errorf("isOverThrottleDuration() = %v, want %v", got, tt.want)
}
})
}
}
func Test_isOverGraceDuration(t *testing.T) {
type args struct {
cachedTime string
throttleDuration int
graceDuration int
}
tests := []struct {
name string
args args
want bool
}{
{
name: "Test_isOverGraceDuration_true",
args: args{
cachedTime: time.Now().Add(-5 * time.Second).Format(time.RFC3339), // 2 sec after grace duration is over
throttleDuration: 0,
graceDuration: 3,
},
want: true,
},
{
name: "Test_isOverGraceDuration_false",
args: args{
cachedTime: time.Now().Add(2 * time.Second).Format(time.RFC3339), // still 8 sec left for grace duration
throttleDuration: 0,
graceDuration: 10,
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isOverGraceDuration(tt.args.cachedTime, tt.args.graceDuration); got != tt.want {
t.Errorf("isOverGraceDuration() = %v, want %v", got, tt.want)
}
})
}
}