-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrepeat_test.go
More file actions
647 lines (573 loc) · 16.1 KB
/
repeat_test.go
File metadata and controls
647 lines (573 loc) · 16.1 KB
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
package probe
import (
"fmt"
"os"
"testing"
"time"
)
// Test Interval YAML unmarshaling
func TestIntervalUnmarshal(t *testing.T) {
tests := []struct {
name string
input any
expected time.Duration
hasError bool
}{
{
name: "integer seconds",
input: 10,
expected: 10 * time.Second,
hasError: false,
},
{
name: "uint64 seconds",
input: uint64(5),
expected: 5 * time.Second,
hasError: false,
},
{
name: "float64 seconds",
input: float64(3),
expected: 3 * time.Second,
hasError: false,
},
{
name: "duration string",
input: "2m30s",
expected: 2*time.Minute + 30*time.Second,
hasError: false,
},
{
name: "milliseconds string",
input: "500ms",
expected: 500 * time.Millisecond,
hasError: false,
},
{
name: "plain number string",
input: "15",
expected: 15 * time.Second,
hasError: false,
},
{
name: "invalid string",
input: "invalid",
expected: 0,
hasError: true,
},
{
name: "invalid type",
input: []string{"invalid"},
expected: 0,
hasError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var interval Interval
unmarshalFunc := func(v any) error {
// Simulate the unmarshal behavior
if vPtr, ok := v.(*any); ok {
*vPtr = tt.input
return nil
}
return fmt.Errorf("invalid unmarshal target")
}
err := interval.UnmarshalYAML(unmarshalFunc)
if tt.hasError {
if err == nil {
t.Errorf("expected error, got none")
}
return
}
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
if interval.Duration != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, interval.Duration)
}
})
}
}
// Test Interval YAML marshaling
func TestIntervalMarshal(t *testing.T) {
tests := []struct {
name string
interval Interval
expected any
}{
{
name: "whole seconds",
interval: Interval{Duration: 10 * time.Second},
expected: 10,
},
{
name: "milliseconds",
interval: Interval{Duration: 500 * time.Millisecond},
expected: "500ms",
},
{
name: "complex duration",
interval: Interval{Duration: 2*time.Minute + 30*time.Second},
expected: 150, // 2m30s = 150 seconds, will be marshaled as integer
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := tt.interval.MarshalYAML()
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
if result != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, result)
}
})
}
}
func TestRepeatFunctionality_Intervals(t *testing.T) {
tests := []struct {
name string
count int
interval time.Duration
expectedMinTime time.Duration
expectedMaxTime time.Duration
}{
{
name: "short interval repeat",
count: 3,
interval: 5 * time.Millisecond,
expectedMinTime: 10 * time.Millisecond, // 2 intervals
expectedMaxTime: 50 * time.Millisecond, // generous upper bound
},
{
name: "medium interval repeat",
count: 4,
interval: 20 * time.Millisecond,
expectedMinTime: 60 * time.Millisecond, // 3 intervals
expectedMaxTime: 150 * time.Millisecond, // generous upper bound
},
{
name: "zero interval repeat",
count: 5,
interval: 0,
expectedMinTime: 0,
expectedMaxTime: 50 * time.Millisecond, // should be very fast
},
{
name: "single repeat",
count: 1,
interval: 10 * time.Millisecond,
expectedMinTime: 0, // no intervals needed
expectedMaxTime: 20 * time.Millisecond,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
workflow := &Workflow{
Name: "repeat-interval-test",
Jobs: []Job{
{
Name: "repeat-job",
Steps: []*Step{}, // Empty steps to avoid external dependencies
Repeat: &Repeat{
Count: tt.count,
Interval: Interval{Duration: tt.interval},
},
},
},
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
start := time.Now()
err := workflow.Start(config)
duration := time.Since(start)
if err != nil {
t.Errorf("Repeat workflow should not error: %v", err)
}
if duration < tt.expectedMinTime {
t.Errorf("Duration %v should be at least %v", duration, tt.expectedMinTime)
}
if duration > tt.expectedMaxTime {
t.Errorf("Duration %v should not exceed %v", duration, tt.expectedMaxTime)
}
})
}
}
func TestRepeatFunctionality_DifferentTimeUnits(t *testing.T) {
tests := []struct {
name string
interval string
expected time.Duration
}{
{
name: "milliseconds",
interval: "100ms",
expected: 100 * time.Millisecond,
},
{
name: "seconds",
interval: "1s",
expected: 1 * time.Second,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Test parsing of different time units
duration, err := time.ParseDuration(tt.interval)
if err != nil {
t.Errorf("Failed to parse duration %s: %v", tt.interval, err)
return
}
if duration != tt.expected {
t.Errorf("Parsed duration %v, expected %v", duration, tt.expected)
}
// Test in actual workflow
workflow := &Workflow{
Name: "time-unit-test",
Jobs: []Job{
{
Name: "time-unit-job",
Steps: []*Step{},
Repeat: &Repeat{
Count: 2,
Interval: Interval{Duration: duration},
},
},
},
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
start := time.Now()
err = workflow.Start(config)
elapsed := time.Since(start)
if err != nil {
t.Errorf("Time unit workflow should not error: %v", err)
}
// For very small intervals, just check it completes
if duration > 10*time.Millisecond {
expectedMin := duration // at least 1 interval
if elapsed < expectedMin {
t.Errorf("Elapsed time %v should be at least %v", elapsed, expectedMin)
}
}
})
}
}
func TestRepeatFunctionality_ParallelVsSequential(t *testing.T) {
t.Run("repeat in parallel mode", func(t *testing.T) {
workflow := &Workflow{
Name: "parallel-repeat-test",
Jobs: []Job{
{
Name: "parallel-repeat-job1",
Steps: []*Step{},
Repeat: &Repeat{
Count: 3,
Interval: Interval{Duration: 10 * time.Millisecond},
},
},
{
Name: "parallel-repeat-job2",
Steps: []*Step{},
Repeat: &Repeat{
Count: 2,
Interval: Interval{Duration: 15 * time.Millisecond},
},
},
},
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
start := time.Now()
err := workflow.Start(config)
duration := time.Since(start)
if err != nil {
t.Errorf("Parallel repeat workflow should not error: %v", err)
}
// In parallel mode with repeat, individual goroutines are created for each repeat iteration
// This should complete relatively quickly as iterations run concurrently
maxExpected := 100 * time.Millisecond // generous bound for parallel execution
if duration > maxExpected {
t.Errorf("Parallel repeat took too long: %v (max expected: %v)", duration, maxExpected)
}
})
t.Run("repeat in sequential mode with dependencies", func(t *testing.T) {
workflow := &Workflow{
Name: "sequential-repeat-test",
Jobs: []Job{
{
Name: "first-repeat-job",
Steps: []*Step{},
Repeat: &Repeat{
Count: 2,
Interval: Interval{Duration: 10 * time.Millisecond},
},
},
{
Name: "second-repeat-job",
Needs: []string{"first-repeat-job"},
Steps: []*Step{},
Repeat: &Repeat{
Count: 2,
Interval: Interval{Duration: 10 * time.Millisecond},
},
},
},
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
start := time.Now()
err := workflow.Start(config)
duration := time.Since(start)
if err != nil {
t.Errorf("Sequential repeat workflow should not error: %v", err)
}
// Sequential execution should take longer as jobs run one after another
// Each job has 2 repeats with 10ms intervals, so minimum ~20ms per job
minExpected := 20 * time.Millisecond // conservative minimum
if duration < minExpected {
t.Errorf("Sequential repeat completed too quickly: %v (min expected: %v)", duration, minExpected)
}
})
}
func TestRepeatFunctionality_EdgeCases(t *testing.T) {
t.Run("zero count repeat", func(t *testing.T) {
workflow := &Workflow{
Name: "zero-count-test",
Jobs: []Job{
{
Name: "zero-count-job",
Steps: []*Step{},
Repeat: &Repeat{
Count: 0, // Edge case: zero count
Interval: Interval{Duration: 10 * time.Millisecond},
},
},
},
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
err := workflow.Start(config)
// Should handle zero count gracefully (might not execute or execute once)
if err != nil {
t.Errorf("Zero count repeat should not error: %v", err)
}
})
t.Run("high count repeat", func(t *testing.T) {
workflow := &Workflow{
Name: "high-count-test",
Jobs: []Job{
{
Name: "high-count-job",
Steps: []*Step{},
Repeat: &Repeat{
Count: 100, // High count
Interval: Interval{Duration: 0}, // Zero interval to keep test fast
},
},
},
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
start := time.Now()
err := workflow.Start(config)
duration := time.Since(start)
if err != nil {
t.Errorf("High count repeat should not error: %v", err)
}
// Even with high count, zero interval should keep it reasonably fast
maxExpected := 1 * time.Second
if duration > maxExpected {
t.Errorf("High count repeat took too long: %v", duration)
}
})
t.Run("negative interval handling", func(t *testing.T) {
// Test that negative intervals are handled properly
workflow := &Workflow{
Name: "negative-interval-test",
Jobs: []Job{
{
Name: "negative-interval-job",
Steps: []*Step{},
Repeat: &Repeat{
Count: 2,
Interval: Interval{Duration: -10 * time.Millisecond}, // Negative interval
},
},
},
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
err := workflow.Start(config)
// Should handle negative intervals gracefully (likely treat as zero)
if err != nil {
t.Errorf("Negative interval repeat should not error: %v", err)
}
})
}
func TestEnvironmentVariableOverride(t *testing.T) {
t.Run("default max repeat count", func(t *testing.T) {
// Ensure environment variable is not set
_ = os.Unsetenv(EnvMaxRepeatCount)
max := getMaxRepeatCount()
if max != DefaultMaxRepeatCount {
t.Errorf("Expected default max repeat count %d, got %d", DefaultMaxRepeatCount, max)
}
})
t.Run("custom max repeat count via env", func(t *testing.T) {
// Set custom max via environment variable
customMax := 50000
if err := os.Setenv(EnvMaxRepeatCount, fmt.Sprintf("%d", customMax)); err != nil {
t.Fatalf("Failed to set environment variable: %v", err)
}
defer func() { _ = os.Unsetenv(EnvMaxRepeatCount) }()
max := getMaxRepeatCount()
if max != customMax {
t.Errorf("Expected custom max repeat count %d, got %d", customMax, max)
}
})
t.Run("default max attempts", func(t *testing.T) {
// Ensure environment variable is not set
_ = os.Unsetenv(EnvMaxAttempts)
max := getMaxAttempts()
if max != DefaultMaxAttempts {
t.Errorf("Expected default max attempts %d, got %d", DefaultMaxAttempts, max)
}
})
t.Run("custom max attempts via env", func(t *testing.T) {
// Set custom max via environment variable
customMax := 25000
if err := os.Setenv(EnvMaxAttempts, fmt.Sprintf("%d", customMax)); err != nil {
t.Fatalf("Failed to set environment variable: %v", err)
}
defer func() { _ = os.Unsetenv(EnvMaxAttempts) }()
max := getMaxAttempts()
if max != customMax {
t.Errorf("Expected custom max attempts %d, got %d", customMax, max)
}
})
t.Run("invalid env value falls back to default", func(t *testing.T) {
// Set invalid value
if err := os.Setenv(EnvMaxRepeatCount, "invalid"); err != nil {
t.Fatalf("Failed to set environment variable: %v", err)
}
defer func() { _ = os.Unsetenv(EnvMaxRepeatCount) }()
max := getMaxRepeatCount()
if max != DefaultMaxRepeatCount {
t.Errorf("Expected default max repeat count %d for invalid env value, got %d", DefaultMaxRepeatCount, max)
}
})
t.Run("negative env value falls back to default", func(t *testing.T) {
// Set negative value
if err := os.Setenv(EnvMaxRepeatCount, "-100"); err != nil {
t.Fatalf("Failed to set environment variable: %v", err)
}
defer func() { _ = os.Unsetenv(EnvMaxRepeatCount) }()
max := getMaxRepeatCount()
if max != DefaultMaxRepeatCount {
t.Errorf("Expected default max repeat count %d for negative env value, got %d", DefaultMaxRepeatCount, max)
}
})
}
func TestRepeatValidation(t *testing.T) {
t.Run("valid repeat count", func(t *testing.T) {
_ = os.Unsetenv(EnvMaxRepeatCount)
r := &Repeat{Count: 100}
err := r.Validate()
if err != nil {
t.Errorf("Expected no error for valid repeat count, got: %v", err)
}
})
t.Run("repeat count at default limit", func(t *testing.T) {
_ = os.Unsetenv(EnvMaxRepeatCount)
r := &Repeat{Count: DefaultMaxRepeatCount}
err := r.Validate()
if err == nil {
t.Errorf("Expected error for repeat count at limit")
}
})
t.Run("repeat count exceeds default limit", func(t *testing.T) {
_ = os.Unsetenv(EnvMaxRepeatCount)
r := &Repeat{Count: DefaultMaxRepeatCount + 1}
err := r.Validate()
if err == nil {
t.Errorf("Expected error for repeat count exceeding limit")
}
})
t.Run("repeat count valid with custom limit", func(t *testing.T) {
customMax := 50000
if err := os.Setenv(EnvMaxRepeatCount, fmt.Sprintf("%d", customMax)); err != nil {
t.Fatalf("Failed to set environment variable: %v", err)
}
defer func() { _ = os.Unsetenv(EnvMaxRepeatCount) }()
r := &Repeat{Count: 20000}
err := r.Validate()
if err != nil {
t.Errorf("Expected no error for repeat count under custom limit, got: %v", err)
}
})
t.Run("repeat count exceeds custom limit", func(t *testing.T) {
customMax := 50000
if err := os.Setenv(EnvMaxRepeatCount, fmt.Sprintf("%d", customMax)); err != nil {
t.Fatalf("Failed to set environment variable: %v", err)
}
defer func() { _ = os.Unsetenv(EnvMaxRepeatCount) }()
r := &Repeat{Count: customMax}
err := r.Validate()
if err == nil {
t.Errorf("Expected error for repeat count at custom limit")
}
})
}
func TestStepRetryValidation(t *testing.T) {
t.Run("valid max attempts", func(t *testing.T) {
_ = os.Unsetenv(EnvMaxAttempts)
s := &StepRetry{MaxAttempts: 100}
err := s.Validate()
if err != nil {
t.Errorf("Expected no error for valid max attempts, got: %v", err)
}
})
t.Run("max attempts at default limit", func(t *testing.T) {
_ = os.Unsetenv(EnvMaxAttempts)
s := &StepRetry{MaxAttempts: DefaultMaxAttempts}
err := s.Validate()
if err != nil {
t.Errorf("Expected no error for max attempts at limit, got: %v", err)
}
})
t.Run("max attempts exceeds default limit", func(t *testing.T) {
_ = os.Unsetenv(EnvMaxAttempts)
s := &StepRetry{MaxAttempts: DefaultMaxAttempts + 1}
err := s.Validate()
if err == nil {
t.Errorf("Expected error for max attempts exceeding limit")
}
})
t.Run("max attempts valid with custom limit", func(t *testing.T) {
customMax := 25000
if err := os.Setenv(EnvMaxAttempts, fmt.Sprintf("%d", customMax)); err != nil {
t.Fatalf("Failed to set environment variable: %v", err)
}
defer func() { _ = os.Unsetenv(EnvMaxAttempts) }()
s := &StepRetry{MaxAttempts: 15000}
err := s.Validate()
if err != nil {
t.Errorf("Expected no error for max attempts under custom limit, got: %v", err)
}
})
t.Run("max attempts exceeds custom limit", func(t *testing.T) {
customMax := 25000
if err := os.Setenv(EnvMaxAttempts, fmt.Sprintf("%d", customMax)); err != nil {
t.Fatalf("Failed to set environment variable: %v", err)
}
defer func() { _ = os.Unsetenv(EnvMaxAttempts) }()
s := &StepRetry{MaxAttempts: customMax + 1}
err := s.Validate()
if err == nil {
t.Errorf("Expected error for max attempts exceeding custom limit")
}
})
}