-
Notifications
You must be signed in to change notification settings - Fork 12
/
throttlers_test.go
1426 lines (1417 loc) · 34.3 KB
/
throttlers_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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gohalt
import (
"context"
"errors"
"fmt"
"regexp"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
)
const (
ms0_0 time.Duration = 0
ms0_9 time.Duration = time.Duration(0.9 * float64(1*time.Millisecond))
ms1_0 time.Duration = 1 * time.Millisecond
ms2_0 time.Duration = 2 * time.Millisecond
ms3_0 time.Duration = 3 * time.Millisecond
ms4_0 time.Duration = 4 * time.Millisecond
ms5_0 time.Duration = 5 * time.Millisecond
ms7_0 time.Duration = 7 * time.Millisecond
ms8_0 time.Duration = 8 * time.Millisecond
ms9_0 time.Duration = 9 * time.Millisecond
ms10_0 time.Duration = 10 * time.Millisecond
ms12_0 time.Duration = 12 * time.Millisecond
ms30_0 time.Duration = 30 * time.Millisecond
)
var trun Runner = NewRunnerSync(context.TODO(), NewThrottlerBuffered(1))
type tcase struct {
tms uint64 // number of sub runs inside one case
thr Throttler // throttler itself
acts []Runnable // actions that need to be throttled
pres []Runnable // actions that neeed to be run before throttle
tss []time.Duration // timestamps that needs to be applied to contexts set
ctxs []context.Context // contexts set for throttling
errs []error // expected throttler errors
durs []time.Duration // expected throttler durations
idx uint64 // carries seq number of sub run execution
over bool // if throttler needs to be over released
pass bool // if throttler doesn't need to be released
wait time.Duration // if next throttler run needs to be delayed in acq
}
func (t *tcase) run(index int) (dur time.Duration, err error) {
// get context with fallback
ctx := context.TODO()
if index < len(t.ctxs) {
ctx = t.ctxs[index]
}
// run additional pre action only if present
if index < len(t.pres) {
if pre := t.pres[index]; pre != nil {
_ = pre(ctx)
}
}
var ts time.Time
// try catch panic into error
func() {
defer atomicIncr(&t.idx)
defer func() {
if msg := recover(); msg != nil {
err = msg.(ErrorInternal)
}
}()
ts = time.Now()
// force strict acquire order
for index != int(atomicGet(&t.idx)) {
_ = sleep(ctx, time.Microsecond)
}
// set additional timestamp only if present
if index < len(t.tss) {
ctx = WithTimestamp(ctx, time.Now().Add(t.tss[index]))
}
err = t.thr.Acquire(ctx)
// in case of threshold error
if terr, ok := err.(ErrorThreshold); ok {
// check whether it's durations threshold
if durs, ok := terr.Threshold.(strdurations); ok {
// then round current values to milliseconds
durs.current = durs.current.Round(time.Millisecond)
terr.Threshold = durs
err = terr
}
}
// if next throttler run needs to be delayed sleep for wait
if t.wait > 0 {
_ = sleep(ctx, t.wait)
}
}()
dur = time.Since(ts)
// run additional action only if present
if index < len(t.acts) {
if act := t.acts[index]; act != nil {
_ = act(ctx)
}
}
limit := 1
if t.over && uint64(index+1) == t.tms { // imitate over releasing on last call
limit = index + 1
}
if t.pass {
limit = 0
}
for i := 0; i < limit; i++ {
if err := t.thr.Release(ctx); err != nil {
return dur, err
}
}
return
}
func (t *tcase) result(index int) (dur time.Duration, err error) {
if index < len(t.errs) {
err = t.errs[index]
}
if index < len(t.durs) {
dur = t.durs[index]
}
return
}
func TestThrottlers(t *testing.T) {
DefaultRetriedDuration = time.Millisecond
cctx, cancel := context.WithCancel(context.TODO())
cancel()
testerr := errors.New("test")
table := map[string]tcase{
"Throttler echo should not throttle on nil input": {
tms: 3,
thr: NewThrottlerEcho(nil),
},
"Throttler echo should throttle on not nil input": {
tms: 3,
thr: NewThrottlerEcho(testerr),
errs: []error{
testerr,
testerr,
testerr,
},
},
"Throttler wait should sleep for millisecond": {
tms: 3,
thr: NewThrottlerWait(ms1_0),
durs: []time.Duration{
ms0_9,
ms0_9,
ms0_9,
},
},
"Throttler square should sleep for correct time periods": {
tms: 3,
thr: NewThrottlerSquare(ms1_0, ms0_0, false),
durs: []time.Duration{
ms0_9,
ms0_9,
ms0_9,
},
},
"Throttler square should sleep for correct time periods with reset": {
tms: 5,
thr: NewThrottlerSquare(ms1_0, 20*ms1_0, true),
acts: []Runnable{
delayed(ms30_0, nope),
delayed(ms30_0, nope),
delayed(ms30_0, nope),
delayed(ms30_0, nope),
delayed(ms30_0, nope),
},
durs: []time.Duration{
ms0_9,
ms0_9 * 4,
ms0_9 * 9,
ms0_9 * 16,
ms0_9,
},
},
"Throttler jitter should sleep for correct time periods": {
tms: 3,
thr: NewThrottlerJitter(ms1_0, ms0_0, false, 0.0),
durs: []time.Duration{
ms0_9,
ms0_9,
ms0_9,
},
},
"Throttler jitter should sleep for random time periods": {
tms: 3,
thr: NewThrottlerJitter(ms1_0, ms0_0, false, 1.9),
durs: []time.Duration{
ms0_0,
ms0_0,
ms0_0,
},
pass: true,
},
"Throttler jitter should sleep for correct time periods with reset": {
tms: 5,
thr: NewThrottlerJitter(ms1_0, 20*ms1_0, true, 0.0),
acts: []Runnable{
delayed(ms30_0, nope),
delayed(ms30_0, nope),
delayed(ms30_0, nope),
delayed(ms30_0, nope),
delayed(ms30_0, nope),
},
durs: []time.Duration{
ms0_9,
ms0_9 * 4,
ms0_9 * 9,
ms0_9 * 16,
ms0_9,
},
},
"Throttler context should throttle on canceled context": {
tms: 3,
thr: NewThrottlerContext(),
ctxs: []context.Context{
cctx,
context.TODO(),
cctx,
},
errs: []error{
ErrorInternal{Throttler: "context", Message: cctx.Err().Error()},
nil,
ErrorInternal{Throttler: "context", Message: cctx.Err().Error()},
},
},
"Throttler panic should panic": {
tms: 3,
thr: NewThrottlerPanic(),
errs: []error{
ErrorInternal{Throttler: "panic"},
ErrorInternal{Throttler: "panic"},
ErrorInternal{Throttler: "panic"},
},
},
"Throttler each should throttle on threshold": {
tms: 6,
thr: NewThrottlerEach(3),
errs: []error{
nil,
nil,
ErrorThreshold{
Throttler: "each",
Threshold: strpair{current: 3, threshold: 3},
},
nil,
nil,
ErrorThreshold{
Throttler: "each",
Threshold: strpair{current: 6, threshold: 3},
},
},
},
"Throttler before should throttle before threshold": {
tms: 6,
thr: NewThrottlerBefore(4),
ctxs: []context.Context{
context.TODO(),
context.TODO(),
context.TODO(),
WithWeight(context.TODO(), 2),
context.TODO(),
context.TODO(),
},
errs: []error{
ErrorThreshold{
Throttler: "before",
Threshold: strpair{current: 1, threshold: 4},
},
ErrorThreshold{
Throttler: "before",
Threshold: strpair{current: 2, threshold: 4},
},
ErrorThreshold{
Throttler: "before",
Threshold: strpair{current: 3, threshold: 4},
},
nil,
nil,
nil,
},
},
"Throttler after should throttle after threshold": {
tms: 6,
thr: NewThrottlerAfter(3),
ctxs: []context.Context{
WithWeight(context.TODO(), 3),
WithWeight(context.TODO(), -1),
context.TODO(),
context.TODO(),
context.TODO(),
},
errs: []error{
nil,
nil,
nil,
ErrorThreshold{
Throttler: "after",
Threshold: strpair{current: 4, threshold: 3},
},
ErrorThreshold{
Throttler: "after",
Threshold: strpair{current: 5, threshold: 3},
},
ErrorThreshold{
Throttler: "after",
Threshold: strpair{current: 6, threshold: 3},
},
},
},
"Throttler past should throttle before time threshold": {
tms: 3,
thr: NewThrottlerPast(time.Date(2000, 1, 1, 10, 0, 0, 0, time.Local)),
ctxs: []context.Context{
WithTimestamp(context.TODO(), time.Date(1900, 1, 1, 10, 0, 0, 0, time.UTC)),
WithTimestamp(context.TODO(), time.Date(2000, 1, 1, 9, 59, 0, 0, time.Local)),
WithTimestamp(context.TODO(), time.Date(2000, 1, 1, 10, 59, 0, 0, time.Local)),
},
errs: []error{
ErrorThreshold{
Throttler: "past",
Threshold: strtimes{
current: time.Date(1900, 1, 1, 10, 0, 0, 0, time.UTC),
threshold: time.Date(2000, 1, 1, 10, 0, 0, 0, time.Local).UTC(),
},
},
ErrorThreshold{
Throttler: "past",
Threshold: strtimes{
current: time.Date(2000, 1, 1, 9, 59, 0, 0, time.Local).UTC(),
threshold: time.Date(2000, 1, 1, 10, 0, 0, 0, time.Local).UTC(),
},
},
nil,
},
},
"Throttler future should throttle before time threshold": {
tms: 3,
thr: NewThrottlerFuture(time.Date(2000, 1, 1, 10, 0, 0, 0, time.Local)),
ctxs: []context.Context{
WithTimestamp(context.TODO(), time.Date(1900, 1, 1, 10, 0, 0, 0, time.UTC)),
WithTimestamp(context.TODO(), time.Date(2000, 1, 1, 9, 59, 0, 0, time.Local)),
WithTimestamp(context.TODO(), time.Date(2000, 1, 1, 10, 59, 0, 0, time.Local)),
},
errs: []error{
nil,
nil,
ErrorThreshold{
Throttler: "future",
Threshold: strtimes{
current: time.Date(2000, 1, 1, 10, 59, 0, 0, time.Local).UTC(),
threshold: time.Date(2000, 1, 1, 10, 0, 0, 0, time.Local).UTC(),
},
},
},
},
"Throttler chance should throttle on 1": {
tms: 3,
thr: NewThrottlerChance(1),
errs: []error{
ErrorThreshold{
Throttler: "chance",
Threshold: strpercent(1.0),
},
ErrorThreshold{
Throttler: "chance",
Threshold: strpercent(1.0),
},
ErrorThreshold{
Throttler: "chance",
Threshold: strpercent(1.0),
},
},
},
"Throttler chance should throttle on >1": {
tms: 3,
thr: NewThrottlerChance(10.10),
errs: []error{
ErrorThreshold{
Throttler: "chance",
Threshold: strpercent(1.0),
},
ErrorThreshold{
Throttler: "chance",
Threshold: strpercent(1.0),
},
ErrorThreshold{
Throttler: "chance",
Threshold: strpercent(1.0),
},
},
},
"Throttler chance should not throttle on 0": {
tms: 3,
thr: NewThrottlerChance(0),
},
"Throttler running should throttle on threshold": {
tms: 3,
thr: NewThrottlerRunning(1),
acts: []Runnable{
delayed(ms2_0, nope),
delayed(ms2_0, nope),
delayed(ms2_0, nope),
},
errs: []error{
nil,
ErrorThreshold{
Throttler: "running",
Threshold: strpair{current: 2, threshold: 1},
},
ErrorThreshold{
Throttler: "running",
Threshold: strpair{current: 3, threshold: 1},
},
},
over: true,
},
"Throttler buffered should throttle on threshold": {
tms: 3,
thr: NewThrottlerBuffered(1),
acts: []Runnable{
delayed(ms1_0, nope),
delayed(ms1_0, nope),
delayed(ms1_0, nope),
},
durs: []time.Duration{
ms0_0,
ms0_9,
ms0_9,
},
over: true,
},
"Throttler priority should throttle on threshold": {
tms: 3,
thr: NewThrottlerPriority(1, 0),
acts: []Runnable{
delayed(ms1_0, nope),
delayed(ms1_0, nope),
delayed(ms1_0, nope),
},
durs: []time.Duration{
ms0_0,
ms0_9,
ms0_9,
},
over: true,
},
"Throttler priority should not throttle on priority": {
tms: 7,
thr: NewThrottlerPriority(5, 2),
acts: []Runnable{
delayed(ms3_0, nope),
delayed(ms3_0, nope),
delayed(ms3_0, nope),
delayed(ms3_0, nope),
delayed(ms3_0, nope),
delayed(ms3_0, nope),
delayed(ms3_0, nope),
},
ctxs: []context.Context{
WithPriority(context.TODO(), 1),
WithPriority(context.TODO(), 1),
WithPriority(context.TODO(), 1),
WithPriority(context.TODO(), 2),
WithPriority(context.TODO(), 2),
WithPriority(context.TODO(), 2),
WithPriority(context.TODO(), 2),
},
durs: []time.Duration{
ms0_0,
ms0_0,
ms2_0,
ms0_0,
ms0_0,
ms0_0,
ms2_0,
},
},
"Throttler timed should throttle after threshold": {
tms: 6,
thr: NewThrottlerTimed(
2,
ms2_0,
ms0_0,
),
pres: []Runnable{
nil,
nil,
nil,
nil,
delayed(ms3_0, nope),
delayed(ms3_0, nope),
},
errs: []error{
nil,
nil,
ErrorThreshold{
Throttler: "after",
Threshold: strpair{current: 3, threshold: 2},
},
ErrorThreshold{
Throttler: "after",
Threshold: strpair{current: 3, threshold: 2},
},
nil,
nil,
},
},
"Throttler timed should throttle after threshold with quantum": {
tms: 6,
thr: NewThrottlerTimed(
2,
ms8_0,
ms4_0,
),
pres: []Runnable{
nil,
nil,
nil,
delayed(ms5_0, nope),
nil,
delayed(ms10_0, nope),
},
errs: []error{
nil,
nil,
ErrorThreshold{
Throttler: "after",
Threshold: strpair{current: 3, threshold: 2},
},
nil,
ErrorThreshold{
Throttler: "after",
Threshold: strpair{current: 3, threshold: 2},
},
nil,
},
},
"Throttler latency should throttle on latency above threshold": {
tms: 3,
thr: NewThrottlerLatency(ms0_9, ms5_0),
tss: []time.Duration{
-ms5_0,
ms0_0,
ms0_0,
},
errs: []error{
nil,
ErrorThreshold{
Throttler: "latency",
Threshold: strdurations{current: ms5_0, threshold: ms0_9},
},
ErrorThreshold{
Throttler: "latency",
Threshold: strdurations{current: ms5_0, threshold: ms0_9},
},
},
},
"Throttler latency should not throttle on latency above threshold after retention": {
tms: 3,
thr: NewThrottlerLatency(ms0_9, ms3_0),
tss: []time.Duration{
-ms5_0,
ms0_0,
ms0_0,
},
pres: []Runnable{
nil,
nil,
delayed(ms9_0, nope),
},
errs: []error{
nil,
ErrorThreshold{
Throttler: "latency",
Threshold: strdurations{current: ms5_0, threshold: ms0_9},
},
nil,
},
},
"Throttler percentile should throttle on latency above threshold": {
tms: 5,
thr: NewThrottlerPercentile(ms3_0, 10, 0.5, ms7_0),
tss: []time.Duration{
ms0_0,
-ms5_0,
-ms5_0,
-ms1_0,
},
errs: []error{
nil,
nil,
ErrorThreshold{
Throttler: "percentile",
Threshold: strdurations{current: ms5_0, threshold: ms3_0},
},
ErrorThreshold{
Throttler: "percentile",
Threshold: strdurations{current: ms5_0, threshold: ms3_0},
},
ErrorThreshold{
Throttler: "percentile",
Threshold: strdurations{current: ms5_0, threshold: ms3_0},
},
},
},
"Throttler percentile should throttle on latency above threshold after retention": {
tms: 5,
thr: NewThrottlerPercentile(ms3_0, 10, 1.5, ms5_0),
tss: []time.Duration{
ms0_0,
-ms5_0,
-ms5_0,
-ms1_0,
},
pres: []Runnable{
nil,
nil,
nil,
nil,
delayed(ms7_0, nope),
},
errs: []error{
nil,
nil,
ErrorThreshold{
Throttler: "percentile",
Threshold: strdurations{current: ms5_0, threshold: ms3_0},
},
ErrorThreshold{
Throttler: "percentile",
Threshold: strdurations{current: ms5_0, threshold: ms3_0},
},
nil,
},
},
"Throttler percentile should throttle on latency above threshold with capacity": {
tms: 5,
thr: NewThrottlerPercentile(ms3_0, 1, 0.5, ms7_0),
tss: []time.Duration{
ms0_0,
-ms5_0,
-ms1_0,
-ms5_0,
},
errs: []error{
nil,
nil,
ErrorThreshold{
Throttler: "percentile",
Threshold: strdurations{current: ms5_0, threshold: ms3_0},
},
nil,
ErrorThreshold{
Throttler: "percentile",
Threshold: strdurations{current: ms5_0, threshold: ms3_0},
},
},
},
"Throttler monitor should throttle on internal stats error": {
tms: 3,
thr: NewThrottlerMonitor(
mntmock{err: testerr},
Stats{},
),
errs: []error{
ErrorInternal{Throttler: "monitor", Message: testerr.Error()},
ErrorInternal{Throttler: "monitor", Message: testerr.Error()},
ErrorInternal{Throttler: "monitor", Message: testerr.Error()},
ErrorInternal{Throttler: "monitor", Message: testerr.Error()},
},
},
"Throttler monitor should not throttle on stats below threshold": {
tms: 3,
thr: NewThrottlerMonitor(
mntmock{
stats: Stats{
MEMAlloc: 100,
MEMSystem: 1000,
CPUPause: 100,
CPUUsage: 0.1,
},
},
Stats{
MEMAlloc: 1000,
MEMSystem: 2000,
CPUPause: 500,
CPUUsage: 0.3,
},
),
},
"Throttler monitor should not throttle on stats above threshold nil stats": {
tms: 3,
thr: NewThrottlerMonitor(
mntmock{
stats: Stats{
MEMAlloc: 500,
MEMSystem: 5000,
CPUPause: 500,
CPUUsage: 0.1,
},
},
Stats{},
),
},
"Throttler monitor should throttle on stats above threshold": {
tms: 3,
thr: NewThrottlerMonitor(
mntmock{
stats: Stats{
MEMAlloc: 500,
MEMSystem: 5000,
CPUPause: 500,
CPUUsage: 0.1,
},
},
Stats{
MEMAlloc: 1000,
MEMSystem: 2000,
CPUPause: 500,
CPUUsage: 0.3,
},
),
errs: []error{
ErrorThreshold{
Throttler: "monitor",
Threshold: strstats{
current: Stats{
MEMAlloc: 500,
MEMSystem: 5000,
CPUPause: 500,
CPUUsage: 0.1,
},
threshold: Stats{
MEMAlloc: 1000,
MEMSystem: 2000,
CPUPause: 500,
CPUUsage: 0.3,
},
},
},
ErrorThreshold{
Throttler: "monitor",
Threshold: strstats{
current: Stats{
MEMAlloc: 500,
MEMSystem: 5000,
CPUPause: 500,
CPUUsage: 0.1,
},
threshold: Stats{
MEMAlloc: 1000,
MEMSystem: 2000,
CPUPause: 500,
CPUUsage: 0.3,
},
},
},
ErrorThreshold{
Throttler: "monitor",
Threshold: strstats{
current: Stats{
MEMAlloc: 500,
MEMSystem: 5000,
CPUPause: 500,
CPUUsage: 0.1,
},
threshold: Stats{
MEMAlloc: 1000,
MEMSystem: 2000,
CPUPause: 500,
CPUUsage: 0.3,
},
},
},
},
},
"Throttler metric should throttle on internal metric error": {
tms: 3,
thr: NewThrottlerMetric(mtcmock{err: testerr}),
errs: []error{
ErrorInternal{Throttler: "metric", Message: testerr.Error()},
ErrorInternal{Throttler: "metric", Message: testerr.Error()},
ErrorInternal{Throttler: "metric", Message: testerr.Error()},
ErrorInternal{Throttler: "metric", Message: testerr.Error()},
},
},
"Throttler metric should not throttle on metric below threshold": {
tms: 3,
thr: NewThrottlerMetric(mtcmock{metric: false}),
},
"Throttler metric should throttle on metric above threshold": {
tms: 3,
thr: NewThrottlerMetric(mtcmock{metric: true}),
errs: []error{
ErrorThreshold{Throttler: "metric", Threshold: strbool(true)},
ErrorThreshold{Throttler: "metric", Threshold: strbool(true)},
ErrorThreshold{Throttler: "metric", Threshold: strbool(true)},
ErrorThreshold{Throttler: "metric", Threshold: strbool(true)},
},
},
"Throttler enqueue should throttle on internal nil marshaler error": {
tms: 3,
thr: NewThrottlerEnqueue(enqmock{}),
ctxs: []context.Context{
WithMarshaler(context.TODO(), nil),
WithMarshaler(context.TODO(), nil),
WithMarshaler(context.TODO(), nil),
},
errs: []error{
ErrorInternal{Throttler: "enqueue", Message: "context doesn't contain required marshaler"},
ErrorInternal{Throttler: "enqueue", Message: "context doesn't contain required marshaler"},
ErrorInternal{Throttler: "enqueue", Message: "context doesn't contain required marshaler"},
},
},
"Throttler enqueue should throttle on internal message error": {
tms: 3,
thr: NewThrottlerEnqueue(enqmock{}),
errs: []error{
ErrorInternal{Throttler: "enqueue", Message: "context doesn't contain required message"},
ErrorInternal{Throttler: "enqueue", Message: "context doesn't contain required message"},
ErrorInternal{Throttler: "enqueue", Message: "context doesn't contain required message"},
},
},
"Throttler enqueue should throttle on internal marshaler error": {
tms: 3,
thr: NewThrottlerEnqueue(enqmock{}),
ctxs: []context.Context{
WithMarshaler(WithMessage(context.TODO(), "test"), marshal(testerr)),
WithMarshaler(WithMessage(context.TODO(), "test"), marshal(testerr)),
WithMarshaler(WithMessage(context.TODO(), "test"), marshal(testerr)),
},
errs: []error{
ErrorInternal{Throttler: "enqueue", Message: testerr.Error()},
ErrorInternal{Throttler: "enqueue", Message: testerr.Error()},
ErrorInternal{Throttler: "enqueue", Message: testerr.Error()},
},
},
"Throttler enqueue should throttle on internal enqueuer error": {
tms: 3,
thr: NewThrottlerEnqueue(enqmock{err: testerr}),
ctxs: []context.Context{
WithMessage(context.TODO(), "test"),
WithMessage(context.TODO(), "test"),
WithMessage(context.TODO(), "test"),
},
errs: []error{
ErrorInternal{Throttler: "enqueue", Message: testerr.Error()},
ErrorInternal{Throttler: "enqueue", Message: testerr.Error()},
ErrorInternal{Throttler: "enqueue", Message: testerr.Error()},
},
},
"Throttler enqueue should not throttle on enqueuer success": {
tms: 3,
thr: NewThrottlerEnqueue(enqmock{}),
ctxs: []context.Context{
WithMarshaler(WithMessage(context.TODO(), "test"), marshal(nil)),
WithMessage(context.TODO(), "test"),
WithMessage(context.TODO(), "test"),
},
},
"Throttler adaptive should throttle on throttling adoptee": {
tms: 3,
thr: NewThrottlerAdaptive(
7,
ms2_0,
ms1_0,
2,
NewThrottlerEcho(testerr),
),
errs: []error{
nil,
ErrorThreshold{
Throttler: "after",
Threshold: strpair{current: 2, threshold: 0},
},
ErrorThreshold{
Throttler: "after",
Threshold: strpair{current: 1, threshold: 0},
},
},
},
"Throttler adaptive should not throttle on non throttling adoptee": {
tms: 3,
thr: NewThrottlerAdaptive(
0,
ms2_0,
ms1_0,
1,
NewThrottlerEcho(nil),
),
},
"Throttler pattern should throttle on internal key error": {
tms: 3,
thr: NewThrottlerPattern(),
ctxs: []context.Context{
context.TODO(),
WithKey(context.TODO(), ""),
WithKey(context.TODO(), "test"),
},
errs: []error{
ErrorInternal{Throttler: "pattern", Message: "known key is not found"},
ErrorInternal{Throttler: "pattern", Message: "known key is not found"},
ErrorInternal{Throttler: "pattern", Message: "known key is not found"},
},
},
"Throttler pattern should throttle on matching throttler pattern": {
tms: 5,
thr: NewThrottlerPattern(
Pattern{
Pattern: regexp.MustCompile("nontest"),
Throttler: NewThrottlerEcho(nil),
},
Pattern{
Pattern: regexp.MustCompile("test"),
Throttler: NewThrottlerEcho(testerr),
},
),
ctxs: []context.Context{
context.TODO(),
WithKey(context.TODO(), "125"),
WithKey(context.TODO(), "test"),
WithKey(context.TODO(), "nontest"),
WithKey(context.TODO(), "non"),
},
errs: []error{
ErrorInternal{Throttler: "pattern", Message: "known key is not found"},
ErrorInternal{Throttler: "pattern", Message: "known key is not found"},
testerr,
nil,
ErrorInternal{Throttler: "pattern", Message: "known key is not found"},
},
},
"Throttler ring should throttle on internal index error": {
tms: 3,
thr: NewThrottlerRing(),
errs: []error{
ErrorInternal{Throttler: "ring", Message: "known index is not found"},
ErrorInternal{Throttler: "ring", Message: "known index is not found"},
ErrorInternal{Throttler: "ring", Message: "known index is not found"},
},
},
"Throttler ring should throttle on matching throttler index": {
tms: 5,
thr: NewThrottlerRing(
NewThrottlerEcho(nil),
NewThrottlerEcho(testerr),
),
errs: []error{
nil,
testerr,
nil,
testerr,
nil,
},
},
"Throttler all should not throttle on empty list": {
tms: 3,
thr: NewThrottlerAll(),
},
"Throttler all should not throttle on non internal errors": {
tms: 3,
thr: NewThrottlerAll(
NewThrottlerEcho(nil),
NewThrottlerEcho(nil),
NewThrottlerEcho(nil),
),
},
"Throttler all should not throttle on some internal errors": {
tms: 3,
thr: NewThrottlerAll(
NewThrottlerEcho(testerr),
NewThrottlerEcho(nil),
NewThrottlerEcho(testerr),
),
},
"Throttler all should throttle on all internal errors": {
tms: 3,
thr: NewThrottlerAll(
NewThrottlerEcho(testerr),
NewThrottlerEcho(testerr),
NewThrottlerEcho(testerr),
),
errs: []error{
ErrorInternal{Throttler: "all", Message: testerr.Error()},
ErrorInternal{Throttler: "all", Message: testerr.Error()},
ErrorInternal{Throttler: "all", Message: testerr.Error()},
},
},
"Throttler any should not throttle on empty list": {
tms: 3,
thr: NewThrottlerAny(),
},
"Throttler any should not throttle on non internal errors": {
tms: 3,