generated from devnw/oss-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscaler_test.go
617 lines (533 loc) · 13.1 KB
/
scaler_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
package stream
import (
"context"
"fmt"
"sync"
"testing"
"time"
"go.devnw.com/gen"
)
var emptyFn = func(context.Context, any) (any, bool) { return 0, true }
var nosendFn = func(context.Context, any) (any, bool) { return 0, false }
func ScalerTest[U ~[]T, T comparable](
t *testing.T,
name string,
data []U,
) {
Tst(
t,
name,
data,
func(t *testing.T, data []T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
testdata := gen.Slice[T](data)
integers := testdata.Map()
s := Scaler[T, T]{
Fn: func(_ context.Context, in T) (T, bool) {
return in, true
},
}
// Test that the scaler can be used with a nil context.
//nolint:staticcheck // nil context on purpose
out, err := s.Exec(nil, testdata.Chan(ctx))
if err != nil {
t.Errorf("expected no error, got %v", err)
}
tloop:
for {
select {
case <-ctx.Done():
t.Fatal("context closed")
case v, ok := <-out:
if !ok {
break tloop
}
integers[v] = true
}
}
for k, v := range integers {
seen, ok := v.(bool)
if !ok {
t.Errorf("expected bool, got %T", v)
}
if !seen {
t.Errorf("expected %v, got %v for %v", true, v, k)
}
}
})
}
func Test_Scaler_Exec(t *testing.T) {
ScalerTest(t, "int8", IntTests[int8](10, 100))
ScalerTest(t, "uint8", IntTests[uint8](10, 100))
ScalerTest(t, "uint8", IntTests[uint8](10, 100))
ScalerTest(t, "uint16", IntTests[uint16](10, 100))
ScalerTest(t, "int32", IntTests[int32](10, 100))
ScalerTest(t, "uint32", IntTests[uint32](10, 100))
ScalerTest(t, "int64", IntTests[int64](10, 100))
ScalerTest(t, "uint64", IntTests[uint64](10, 100))
ScalerTest(t, "float32", FloatTests[float32](10, 100))
ScalerTest(t, "float64", FloatTests[float64](10, 100))
}
func Test_Scaler_NilFn(t *testing.T) {
s := Scaler[any, any]{}
//nolint:staticcheck // nil context on purpose
_, err := s.Exec(nil, nil)
if err == nil {
t.Error("Expected error, got nil")
}
}
func Test_Scaler_NilCtx(t *testing.T) {
s := Scaler[any, any]{
Fn: emptyFn,
}
// Overwrite the default context with a cancelable context.
var cancel context.CancelFunc
defaultCtx, cancel = context.WithCancel(context.Background())
// Fix the default context after the test completes
t.Cleanup(func() {
defaultCtx = context.Background()
})
cancel()
// Test that the scaler can be used with a nil context.
//nolint:staticcheck // nil context on purpose
out, err := s.Exec(nil, nil)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
select {
case <-time.After(time.Second):
t.Errorf("expected no timeout, got timeout")
case _, ok := <-out:
if ok {
t.Errorf("expected out to be closed")
}
}
}
func Test_Scaler_CloseIn(t *testing.T) {
s := Scaler[any, any]{
Fn: emptyFn,
}
in := make(chan any)
close(in)
// Test that the scaler can be used with a nil context.
//nolint:staticcheck // nil context on purpose
out, err := s.Exec(nil, in)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
select {
case <-time.After(time.Second):
t.Errorf("expected no timeout, got timeout")
case _, ok := <-out:
if ok {
t.Errorf("expected out to be closed")
}
}
}
func Test_Scaler_l2ctx(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
s := Scaler[any, any]{
Wait: time.Minute,
Fn: emptyFn,
}
in := make(chan any)
// Test that the scaler can be used with a nil context.
out, err := s.Exec(ctx, in)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
// Trigger the internal loop of the scaler
in <- 1
// Cancel the context while it's waiting to
// scale to layer 2.
cancel()
select {
case <-time.After(time.Second):
t.Errorf("expected no timeout, got timeout")
case _, ok := <-out:
if ok {
t.Errorf("expected out to be closed")
}
}
}
func Test_Scaler_layer2_ctx1(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
s := Scaler[any, any]{
Wait: time.Minute,
Life: time.Minute,
Fn: emptyFn,
}
out := s.layer2(ctx, nil)
select {
case <-time.After(time.Second):
t.Errorf("expected no timeout, got timeout")
case _, ok := <-out:
if ok {
t.Errorf("expected out to be closed")
}
}
}
func Test_Scaler_layer2_closeIn(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s := Scaler[any, any]{
Wait: time.Minute,
Life: time.Minute,
Fn: emptyFn,
}
in := make(chan any)
close(in)
out := s.layer2(ctx, in)
select {
case <-time.After(time.Second):
t.Errorf("expected no timeout, got timeout")
case _, ok := <-out:
if ok {
t.Errorf("expected out to be closed")
}
}
}
func Test_Scaler_layer2_ctx2(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
s := Scaler[any, any]{
Wait: time.Minute,
Life: time.Minute,
Fn: emptyFn,
}
in := make(chan any)
defer close(in)
out := s.layer2(ctx, in)
// Push data to the channel to trigger the internal loop and block
in <- 1
cancel()
<-ctx.Done()
for i := 0; i < 1000; i++ {
select {
case <-time.After(time.Second):
t.Errorf("expected no timeout, got timeout")
case _, ok := <-out:
if !ok {
return
}
}
}
t.Errorf("expected out to be closed")
}
func Test_Scaler_layer2_nosend(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s := Scaler[any, any]{
Wait: time.Minute,
Life: time.Minute,
Fn: nosendFn,
}
in := make(chan any)
defer close(in)
out := s.layer2(ctx, in)
// Push data to the channel to trigger the internal loop and block
in <- 1
select {
case <-time.After(time.Millisecond):
case <-out:
t.Fatalf("expected 0 data to be sent, got 1")
}
}
func TestTickDur(t *testing.T) {
testCases := []struct {
name string
tick DurationScaler
duration time.Duration
currentStep int
expected time.Duration
}{
{
name: "Test case 1",
tick: DurationScaler{Interval: 3, ScalingFactor: 0.1, originalDuration: 10 * time.Second},
duration: 10 * time.Second,
currentStep: 3,
expected: 11 * time.Second,
},
{
name: "Test case 2",
tick: DurationScaler{Interval: 5, ScalingFactor: -0.1, originalDuration: 20 * time.Second},
duration: 20 * time.Second,
currentStep: 10,
expected: 18 * time.Second,
},
{
name: "Test case 3",
tick: DurationScaler{Interval: 2, ScalingFactor: 0.5, originalDuration: 10 * time.Second},
duration: 10 * time.Second,
currentStep: 4,
expected: 15 * time.Second,
},
{
name: "Test case 4",
tick: DurationScaler{Interval: 4, ScalingFactor: -0.5, originalDuration: 30 * time.Second},
duration: 30 * time.Second,
currentStep: 8,
expected: 15 * time.Second,
},
{
name: "Test case 5",
tick: DurationScaler{Interval: 3, ScalingFactor: 0.1, originalDuration: 10 * time.Second},
duration: 10 * time.Second,
currentStep: 2,
expected: 10 * time.Second,
},
{
name: "Test case 6: Step is divisible, modifier in range",
tick: DurationScaler{Interval: 3, ScalingFactor: 0.1, originalDuration: 10 * time.Second},
duration: 10 * time.Second,
currentStep: 3,
expected: 11 * time.Second,
},
{
name: "Test case 7: Step is not divisible, modifier in range",
tick: DurationScaler{Interval: 3, ScalingFactor: 0.1, originalDuration: 10 * time.Second},
duration: 10 * time.Second,
currentStep: 2,
expected: 10 * time.Second,
},
{
name: "Test case 8: Step is divisible, modifier is zero",
tick: DurationScaler{Interval: 3, ScalingFactor: 0, originalDuration: 10 * time.Second},
duration: 10 * time.Second,
currentStep: 3,
expected: 10 * time.Second,
},
{
name: "Test case 9: Step is divisible, modifier is out of range",
tick: DurationScaler{Interval: 3, ScalingFactor: 1, originalDuration: 10 * time.Second},
duration: 10 * time.Second,
currentStep: 3,
expected: 10 * time.Second,
},
{
name: "Test case 10: Step is zero, modifier in range",
tick: DurationScaler{Interval: 0, ScalingFactor: 0.1, originalDuration: 10 * time.Second},
duration: 10 * time.Second,
currentStep: 3,
expected: 10 * time.Second,
},
{
name: "Test case 6: Step number decreases",
tick: DurationScaler{
Interval: 2,
ScalingFactor: 0.5,
originalDuration: 10 * time.Second,
lastInterval: 4,
},
duration: 15 * time.Second,
currentStep: 2,
expected: 10 * time.Second,
},
{
name: "Test case 7: testing below minwait",
tick: DurationScaler{
Interval: 1,
ScalingFactor: -0.999,
originalDuration: time.Millisecond * 2,
lastInterval: 0,
},
duration: MinWait,
currentStep: 1,
expected: MinWait,
},
{
name: "Test case 8: testing below minwait",
tick: DurationScaler{
Interval: 1,
ScalingFactor: -0.999,
originalDuration: time.Millisecond * 900,
lastInterval: 0,
},
duration: MinWait,
currentStep: 1,
expected: MinWait,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := (&tc.tick).scaledDuration(tc.duration, tc.currentStep)
if result != tc.expected {
t.Errorf("Expected: %v, got: %v", tc.expected, result)
}
})
}
}
func FuzzTick(f *testing.F) {
f.Fuzz(func(t *testing.T, step, cStep int, mod float64, orig, dur int64) {
tick := &DurationScaler{
Interval: step,
ScalingFactor: mod,
originalDuration: time.Duration(orig),
}
v := tick.scaledDuration(time.Duration(dur), cStep)
if v < 0 {
t.Fatalf("negative duration: %v", v)
}
})
}
func FuzzScaler(f *testing.F) {
interceptFunc := func(_ context.Context, t int) (string, bool) {
return fmt.Sprintf("%d", t), true
}
f.Fuzz(func(
t *testing.T,
wait, life int64,
step, _ int,
mod float64,
max uint,
in int,
) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
tick := DurationScaler{
Interval: step,
ScalingFactor: mod,
}
// Initialize Scaler
scaler := Scaler[int, string]{
Wait: time.Millisecond * time.Duration(wait),
Life: time.Millisecond * time.Duration(life),
Fn: interceptFunc,
WaitModifier: tick,
Max: max,
}
// Create a simple input channel
input := make(chan int, 1)
defer close(input)
// Execute the Scaler
out, err := scaler.Exec(ctx, input)
if err != nil {
t.Errorf("Scaler Exec failed: %v", err)
t.Fail()
}
// Send input value and check output
input <- in
select {
case <-ctx.Done():
t.Errorf("Scaler Exec timed out")
t.Fail()
case res := <-out:
if res != fmt.Sprintf("%d", in) {
t.Errorf("Scaler Exec failed: expected %d, got %s", in, res)
t.Fail()
}
t.Logf("Scaler Exec succeeded: expected %d, got %s", in, res)
}
})
}
//nolint:gocognit // This is a test function
func Test_Scaler_Max(t *testing.T) {
tests := map[string]struct {
max uint
send int
expected int
}{
"max 0": {
max: 0,
send: 1000,
expected: 1000,
},
"max 1": {
max: 1,
send: 10,
expected: 10,
},
"max 2": {
max: 2,
send: 10,
expected: 10,
},
"max 3": {
max: 3,
send: 10,
expected: 10,
},
"max 4": {
max: 4,
send: 100,
expected: 100,
},
"max 1000": {
max: 1000,
send: 10000,
expected: 10000,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
inited := 0
initedMu := sync.Mutex{}
release := make(chan struct{})
interceptFunc := func(_ context.Context, t int) (int, bool) {
defer func() {
initedMu.Lock()
defer initedMu.Unlock()
inited--
}()
initedMu.Lock()
inited++
initedMu.Unlock()
<-release
return t, true
}
// Initialize Scaler
scaler := Scaler[int, int]{
Wait: time.Millisecond,
Life: time.Millisecond,
Fn: interceptFunc,
Max: test.max,
}
// Create a simple input channel
input := make(chan int, test.send)
defer close(input)
for i := 0; i < test.send; i++ {
input <- i
}
// Execute the Scaler
out, err := scaler.Exec(ctx, input)
if err != nil {
t.Errorf("Scaler Exec failed: %v", err)
t.Fail()
}
recv := 1
tloop:
for {
select {
case <-ctx.Done():
t.Errorf("Scaler Exec timed out")
case _, ok := <-out:
if !ok {
break tloop
}
recv++
t.Logf("received %d", recv)
if recv >= test.expected {
break tloop
}
default:
time.Sleep(time.Millisecond)
initedMu.Lock()
if test.max > 0 && inited > int(test.max) {
t.Errorf("Scaler Exec failed: expected %d, got %d", test.max, inited)
t.Fail()
}
initedMu.Unlock()
// Release one goroutine
release <- struct{}{}
}
}
if recv != test.expected {
t.Errorf("Scaler Exec failed: expected %d, got %d", test.expected, recv)
t.Fail()
}
})
}
}