-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworkflow_test.go
More file actions
987 lines (876 loc) · 21.9 KB
/
workflow_test.go
File metadata and controls
987 lines (876 loc) · 21.9 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
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
package probe
import (
"fmt"
"os"
"reflect"
"sync"
"testing"
"time"
)
func TestWorkflowExecutor_DependencyManagement(t *testing.T) {
tests := []struct {
name string
jobs []Job
expectError bool
}{
{
name: "jobs without dependencies",
jobs: []Job{
{
Name: "job1",
Steps: []*Step{},
},
{
Name: "job2",
Steps: []*Step{},
},
},
expectError: false,
},
{
name: "jobs with valid dependencies",
jobs: []Job{
{
Name: "job1",
Steps: []*Step{},
},
{
Name: "job2",
Needs: []string{"job1"},
Steps: []*Step{},
},
},
expectError: false,
},
{
name: "jobs with circular dependencies",
jobs: []Job{
{
Name: "job1",
Needs: []string{"job2"},
Steps: []*Step{},
},
{
Name: "job2",
Needs: []string{"job1"},
Steps: []*Step{},
},
},
expectError: true,
},
{
name: "jobs with missing dependencies",
jobs: []Job{
{
Name: "job1",
Needs: []string{"nonexistent"},
Steps: []*Step{},
},
},
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
workflow := &Workflow{
Name: "test-workflow",
Jobs: tt.jobs,
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
err := workflow.Start(config)
if tt.expectError && err == nil {
t.Error("Expected error but got none")
}
if !tt.expectError && err != nil {
t.Errorf("Expected no error but got: %v", err)
}
})
}
}
func TestWorkflowExecutor_ParallelExecution(t *testing.T) {
t.Run("parallel execution without dependencies", func(t *testing.T) {
workflow := &Workflow{
Name: "parallel-test",
Jobs: []Job{
{
Name: "job1",
Steps: []*Step{},
},
{
Name: "job2",
Steps: []*Step{},
},
{
Name: "job3",
Steps: []*Step{},
},
},
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
start := time.Now()
err := workflow.Start(config)
duration := time.Since(start)
if err != nil {
t.Errorf("Parallel execution should not error: %v", err)
}
// Parallel execution should be faster than sequential
// With empty steps, this should complete very quickly
if duration > 1*time.Second {
t.Errorf("Parallel execution took too long: %v", duration)
}
})
}
func TestWorkflowExecutor_SequentialWithDependencies(t *testing.T) {
t.Run("sequential execution with dependencies", func(t *testing.T) {
workflow := &Workflow{
Name: "sequential-test",
Jobs: []Job{
{
Name: "first",
Steps: []*Step{},
},
{
Name: "second",
Needs: []string{"first"},
Steps: []*Step{},
},
{
Name: "third",
Needs: []string{"second"},
Steps: []*Step{},
},
},
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
err := workflow.Start(config)
if err != nil {
t.Errorf("Sequential execution with dependencies should not error: %v", err)
}
})
}
func TestWorkflowExecutor_BufferedOutput(t *testing.T) {
t.Run("buffered output with multiple jobs", func(t *testing.T) {
workflow := &Workflow{
Name: "buffered-test",
Jobs: []Job{
{
Name: "job1",
Steps: []*Step{},
},
{
Name: "job2",
Needs: []string{"job1"},
Steps: []*Step{},
},
},
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
err := workflow.Start(config)
if err != nil {
t.Errorf("Buffered execution should not error: %v", err)
}
// With dependencies and multiple jobs, buffering should be used
// This test mainly verifies that the workflow completes successfully
})
}
func TestWorkflowExecutor_RepeatJobs(t *testing.T) {
t.Run("job with repeat in parallel execution", func(t *testing.T) {
workflow := &Workflow{
Name: "repeat-test",
Jobs: []Job{
{
Name: "repeat-job",
Steps: []*Step{},
Repeat: &Repeat{
Count: 3,
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("Repeat job execution should not error: %v", err)
}
// Should take at least the interval time * (count-1)
expectedMinDuration := 2 * 10 * time.Millisecond // 2 intervals for 3 executions
if duration < expectedMinDuration {
t.Errorf("Duration %v should be at least %v for repeat execution", duration, expectedMinDuration)
}
})
}
func TestWorkflowExecutor_MixedScenarios(t *testing.T) {
t.Run("complex workflow with dependencies and repeats", func(t *testing.T) {
workflow := &Workflow{
Name: "complex-test",
Jobs: []Job{
{
Name: "setup",
Steps: []*Step{},
},
{
Name: "worker1",
Needs: []string{"setup"},
Steps: []*Step{},
Repeat: &Repeat{
Count: 2,
Interval: Interval{Duration: 5 * time.Millisecond},
},
},
{
Name: "worker2",
Needs: []string{"setup"},
Steps: []*Step{},
},
{
Name: "cleanup",
Needs: []string{"worker1", "worker2"},
Steps: []*Step{},
},
},
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
err := workflow.Start(config)
if err != nil {
t.Errorf("Complex workflow should not error: %v", err)
}
})
}
func TestWorkflowExecutor_ErrorHandling(t *testing.T) {
t.Run("workflow with failed job dependency", func(t *testing.T) {
// This test verifies that jobs with failed dependencies are properly skipped
// Since we're using empty steps, jobs should succeed, but we test the structure
workflow := &Workflow{
Name: "error-handling-test",
Jobs: []Job{
{
Name: "might-fail",
Steps: []*Step{},
},
{
Name: "depends-on-failed",
Needs: []string{"might-fail"},
Steps: []*Step{},
},
},
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
err := workflow.Start(config)
// Should complete without error even if dependency logic is exercised
if err != nil {
t.Errorf("Error handling test should not error: %v", err)
}
})
}
func TestWorkflowExecutor_PrintDetailedResults(t *testing.T) {
t.Run("print detailed results functionality", func(t *testing.T) {
workflow := &Workflow{
Name: "detailed-results-test",
Jobs: []Job{
{
Name: "test-job",
Steps: []*Step{},
},
},
printer: newBufferPrinter(),
}
// Create workflow buffer
result := NewResult()
jobResult := &JobResult{
JobName: "test-job",
JobID: "test-job",
Status: "Completed",
StartTime: time.Now().Add(-100 * time.Millisecond),
EndTime: time.Now(),
Success: true,
}
result.Jobs["test-job"] = jobResult
// This should not panic and should execute successfully
workflow.printer.PrintReport(result)
// If we get here without panic, the test passes
})
}
func TestParallelExecution_EdgeCases(t *testing.T) {
t.Run("empty workflow", func(t *testing.T) {
workflow := &Workflow{
Name: "empty-workflow",
Jobs: []Job{},
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
err := workflow.Start(config)
if err != nil {
t.Errorf("Empty workflow should not error: %v", err)
}
})
t.Run("single job parallel execution", func(t *testing.T) {
workflow := &Workflow{
Name: "single-job",
Jobs: []Job{
{
Name: "solo",
Steps: []*Step{},
},
},
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
err := workflow.Start(config)
if err != nil {
t.Errorf("Single job workflow should not error: %v", err)
}
})
t.Run("many jobs parallel execution", func(t *testing.T) {
// Create a workflow with many jobs to test parallel execution limits
jobs := make([]Job, 10)
for i := range 10 {
jobs[i] = Job{
Name: fmt.Sprintf("job-%d", i),
Steps: []*Step{},
}
}
workflow := &Workflow{
Name: "many-jobs",
Jobs: jobs,
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
start := time.Now()
err := workflow.Start(config)
duration := time.Since(start)
if err != nil {
t.Errorf("Many jobs workflow should not error: %v", err)
}
// Should complete quickly in parallel
if duration > 2*time.Second {
t.Errorf("Many parallel jobs took too long: %v", duration)
}
})
}
func TestBufferedExecution_EdgeCases(t *testing.T) {
t.Run("buffered execution with single job", func(t *testing.T) {
workflow := &Workflow{
Name: "single-buffered",
Jobs: []Job{
{
Name: "buffered-job",
Needs: []string{}, // Force dependency path but no actual dependencies
Steps: []*Step{},
},
},
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
err := workflow.Start(config)
if err != nil {
t.Errorf("Single buffered job should not error: %v", err)
}
})
t.Run("buffered execution with concurrent output", func(t *testing.T) {
// Test that concurrent buffered output doesn't cause race conditions
workflow := &Workflow{
Name: "concurrent-buffered",
Jobs: []Job{
{
Name: "producer1",
Steps: []*Step{},
},
{
Name: "producer2",
Steps: []*Step{},
},
{
Name: "consumer",
Needs: []string{"producer1", "producer2"},
Steps: []*Step{},
},
},
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
err := workflow.Start(config)
if err != nil {
t.Errorf("Concurrent buffered execution should not error: %v", err)
}
})
}
func TestRepeatExecution_EdgeCases(t *testing.T) {
t.Run("repeat with zero interval", func(t *testing.T) {
workflow := &Workflow{
Name: "zero-interval-repeat",
Jobs: []Job{
{
Name: "fast-repeat",
Steps: []*Step{},
Repeat: &Repeat{
Count: 5,
Interval: Interval{Duration: 0}, // Zero interval
},
},
},
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
start := time.Now()
err := workflow.Start(config)
duration := time.Since(start)
if err != nil {
t.Errorf("Zero interval repeat should not error: %v", err)
}
// Should complete very quickly with zero interval
if duration > 100*time.Millisecond {
t.Errorf("Zero interval repeat took too long: %v", duration)
}
})
t.Run("repeat with very short interval", func(t *testing.T) {
workflow := &Workflow{
Name: "short-interval-repeat",
Jobs: []Job{
{
Name: "quick-repeat",
Steps: []*Step{},
Repeat: &Repeat{
Count: 3,
Interval: Interval{Duration: 1 * time.Millisecond},
},
},
},
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
start := time.Now()
err := workflow.Start(config)
duration := time.Since(start)
if err != nil {
t.Errorf("Short interval repeat should not error: %v", err)
}
// Should take at least the minimum interval time
expectedMin := 2 * time.Millisecond // 2 intervals for 3 executions
if duration < expectedMin {
t.Errorf("Duration %v should be at least %v", duration, expectedMin)
}
})
t.Run("repeat with single count", func(t *testing.T) {
workflow := &Workflow{
Name: "single-repeat",
Jobs: []Job{
{
Name: "once-repeat",
Steps: []*Step{},
Repeat: &Repeat{
Count: 1,
Interval: Interval{Duration: 10 * time.Millisecond},
},
},
},
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
err := workflow.Start(config)
if err != nil {
t.Errorf("Single count repeat should not error: %v", err)
}
})
}
func TestExecutor_ConcurrencyEdgeCases(t *testing.T) {
t.Run("high concurrency with dependencies", func(t *testing.T) {
// Create a workflow with multiple levels of dependencies
workflow := &Workflow{
Name: "high-concurrency",
Jobs: []Job{
{Name: "root", Steps: []*Step{}},
{Name: "level1-a", Needs: []string{"root"}, Steps: []*Step{}},
{Name: "level1-b", Needs: []string{"root"}, Steps: []*Step{}},
{Name: "level1-c", Needs: []string{"root"}, Steps: []*Step{}},
{Name: "level2-a", Needs: []string{"level1-a", "level1-b"}, Steps: []*Step{}},
{Name: "level2-b", Needs: []string{"level1-b", "level1-c"}, Steps: []*Step{}},
{Name: "final", Needs: []string{"level2-a", "level2-b"}, Steps: []*Step{}},
},
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
err := workflow.Start(config)
if err != nil {
t.Errorf("High concurrency workflow should not error: %v", err)
}
})
t.Run("mixed repeat and parallel execution", func(t *testing.T) {
workflow := &Workflow{
Name: "mixed-execution",
Jobs: []Job{
{
Name: "parallel1",
Steps: []*Step{},
},
{
Name: "repeat1",
Steps: []*Step{},
Repeat: &Repeat{
Count: 2,
Interval: Interval{Duration: 5 * time.Millisecond},
},
},
{
Name: "parallel2",
Steps: []*Step{},
},
},
printer: newBufferPrinter(),
}
config := Config{Verbose: false}
err := workflow.Start(config)
if err != nil {
t.Errorf("Mixed execution workflow should not error: %v", err)
}
})
}
func TestEnv(t *testing.T) {
_ = os.Setenv("HOST", "http://localhost")
_ = os.Setenv("TOKEN", "secrets")
defer func() {
_ = os.Unsetenv("HOST")
_ = os.Unsetenv("TOKEN")
}()
expected := map[string]string{
"HOST": "http://localhost",
"TOKEN": "secrets",
}
wf := &Workflow{}
actual := wf.Env()
if actual["HOST"] != expected["HOST"] || actual["TOKEN"] != expected["TOKEN"] {
t.Errorf("expected %+v, got %+v", expected, actual)
}
}
func Test_evalVars(t *testing.T) {
tests := []struct {
name string
wf *Workflow
expected map[string]any
err error
}{
{
name: "use expr",
wf: &Workflow{
Name: "Test",
Vars: map[string]any{
"host": "{{HOST ?? 'http://localhost:3000'}}",
"token": "{{TOKEN}}",
},
env: map[string]string{
"TOKEN": "secrets",
},
},
expected: map[string]any{
"host": "http://localhost:3000",
"token": "secrets",
},
err: nil,
},
{
name: "not exists environment",
wf: &Workflow{
Name: "Test",
Vars: map[string]any{
"host": "{{HOST}}",
"token": "{{TOKEN}}",
},
env: map[string]string{
"TOKEN": "secrets",
},
},
expected: map[string]any{
"host": "<nil>",
"token": "secrets",
},
err: fmt.Errorf("environment(HOST) is nil"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual, err := tt.wf.evalVars()
if err != nil && err.Error() != tt.err.Error() {
t.Errorf("expected error %+v, got %+v", tt.err, err)
}
if !reflect.DeepEqual(tt.expected, actual) {
t.Errorf("expected %#v, got %#v", tt.expected, actual)
}
})
}
}
func TestStepRepeatCounter(t *testing.T) {
tests := []struct {
name string
successCount int
failureCount int
expected string
}{
{
name: "all success",
successCount: 100,
failureCount: 0,
expected: "100/100 success (100.0%)",
},
{
name: "partial success",
successCount: 80,
failureCount: 20,
expected: "80/100 success (80.0%)",
},
{
name: "all failure",
successCount: 0,
failureCount: 100,
expected: "0/100 success (0.0%)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
counter := StepRepeatCounter{
SuccessCount: tt.successCount,
FailureCount: tt.failureCount,
Name: "Test Step",
}
totalCount := counter.SuccessCount + counter.FailureCount
successRate := float64(counter.SuccessCount) / float64(totalCount) * 100
actual := fmt.Sprintf("%d/%d success (%.1f%%)",
counter.SuccessCount, totalCount, successRate)
if actual != tt.expected {
t.Errorf("expected %s, got %s", tt.expected, actual)
}
})
}
}
func TestJobContextRepeatTracking(t *testing.T) {
ctx := JobContext{
IsRepeating: true,
RepeatCurrent: 5,
RepeatTotal: 10,
StepCounters: make(map[int]StepRepeatCounter),
Printer: newBufferPrinter(),
countersMu: &sync.Mutex{},
}
// Test initial state
if !ctx.IsRepeating {
t.Error("expected IsRepeating to be true")
}
if ctx.RepeatCurrent != 5 {
t.Errorf("expected RepeatCurrent to be 5, got %d", ctx.RepeatCurrent)
}
if ctx.RepeatTotal != 10 {
t.Errorf("expected RepeatTotal to be 10, got %d", ctx.RepeatTotal)
}
// Test step counter initialization
counter := StepRepeatCounter{
SuccessCount: 3,
FailureCount: 2,
Name: "Test Step",
LastResult: true,
}
ctx.StepCounters[0] = counter
if len(ctx.StepCounters) != 1 {
t.Errorf("expected 1 step counter, got %d", len(ctx.StepCounters))
}
if ctx.StepCounters[0].SuccessCount != 3 {
t.Errorf("expected SuccessCount to be 3, got %d", ctx.StepCounters[0].SuccessCount)
}
}
func TestStepRepeatCounterUpdate(t *testing.T) {
// Test counter update logic
jCtx := &JobContext{
IsRepeating: true,
RepeatCurrent: 3,
RepeatTotal: 10,
StepCounters: make(map[int]StepRepeatCounter),
Printer: newBufferPrinter(),
countersMu: &sync.Mutex{},
}
step := &Step{
Name: "Test Step",
Test: "true", // Always success
Idx: 0,
Expr: &Expr{},
}
// Capture stdout to avoid test output noise
oldStdout := os.Stdout
os.Stdout, _ = os.Open(os.DevNull)
defer func() { os.Stdout = oldStdout }()
// Execute multiple times
for i := 1; i <= 3; i++ {
jCtx.RepeatCurrent = i
step.handleRepeatExecution(jCtx, "Test Step", false) // false = no error
}
// Check final counter state
counter := jCtx.StepCounters[0]
if counter.SuccessCount != 3 {
t.Errorf("Expected SuccessCount to be 3, got %d", counter.SuccessCount)
}
if counter.FailureCount != 0 {
t.Errorf("Expected FailureCount to be 0, got %d", counter.FailureCount)
}
if counter.Name != "Test Step" {
t.Errorf("Expected Name to be 'Test Step', got %s", counter.Name)
}
}
func TestStepRepeatDisplayConditions(t *testing.T) {
tests := []struct {
name string
repeatCurrent int
repeatTotal int
shouldDisplay bool
description string
}{
{
name: "first execution",
repeatCurrent: 1,
repeatTotal: 10,
shouldDisplay: true,
description: "should show initial message",
},
{
name: "middle execution",
repeatCurrent: 5,
repeatTotal: 10,
shouldDisplay: false,
description: "should not display in middle",
},
{
name: "final execution",
repeatCurrent: 10,
repeatTotal: 10,
shouldDisplay: true,
description: "should show final result",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Test the display condition logic
totalCount := tt.repeatCurrent // Simulate counter state
isFirstExecution := totalCount == 1
isFinalExecution := tt.repeatCurrent == tt.repeatTotal
shouldDisplay := isFirstExecution || isFinalExecution
if shouldDisplay != tt.shouldDisplay {
t.Errorf("%s: expected shouldDisplay to be %v, got %v",
tt.description, tt.shouldDisplay, shouldDisplay)
}
})
}
}
// WorkflowBuffer tests
func TestWorkflowBuffer_AddStepResult(t *testing.T) {
wb := NewResult()
jobID := "test-job"
// Add a job buffer first
wb.Jobs[jobID] = &JobResult{
JobID: jobID,
JobName: "Test Job",
StartTime: time.Now(),
StepResults: []StepResult{},
}
// Create test step results
stepResult1 := StepResult{
Index: 0,
Name: "Step 1",
Status: StatusSuccess,
}
stepResult2 := StepResult{
Index: 1,
Name: "Step 2",
Status: StatusError,
RepeatCounter: &StepRepeatCounter{
SuccessCount: 3,
FailureCount: 1,
},
}
// Add step results
wb.AddStepResult(jobID, stepResult1)
wb.AddStepResult(jobID, stepResult2)
// Verify step results were added
jobResult, exists := wb.Jobs[jobID]
if !exists {
t.Fatal("Job buffer should exist")
}
if len(jobResult.StepResults) != 2 {
t.Errorf("Expected 2 step results, got %d", len(jobResult.StepResults))
}
if jobResult.StepResults[0].Name != "Step 1" {
t.Errorf("Expected first step name 'Step 1', got '%s'", jobResult.StepResults[0].Name)
}
if jobResult.StepResults[1].Name != "Step 2" {
t.Errorf("Expected second step name 'Step 2', got '%s'", jobResult.StepResults[1].Name)
}
if jobResult.StepResults[1].RepeatCounter == nil {
t.Error("Expected RepeatCounter to be set for second step")
} else if jobResult.StepResults[1].RepeatCounter.SuccessCount != 3 {
t.Errorf("Expected RepeatCounter.SuccessCount = 3, got %d", jobResult.StepResults[1].RepeatCounter.SuccessCount)
}
}
func TestWorkflowBuffer_AddStepResult_NonExistentJob(t *testing.T) {
wb := NewResult()
stepResult := StepResult{
Index: 0,
Name: "Step 1",
Status: StatusSuccess,
}
// This should not panic even if job doesn't exist
wb.AddStepResult("non-existent-job", stepResult)
// Verify no job buffer was created
if _, exists := wb.Jobs["non-existent-job"]; exists {
t.Error("Job buffer should not be created for non-existent job")
}
}
func TestWorkflowBuffer_ConcurrentAccess(t *testing.T) {
wb := NewResult()
jobID := "test-job"
// Add a job buffer first
wb.Jobs[jobID] = &JobResult{
JobID: jobID,
JobName: "Test Job",
StartTime: time.Now(),
StepResults: []StepResult{},
}
// Test concurrent add and get operations
done := make(chan bool, 2)
// Goroutine 1: Add step results
go func() {
for i := range 10 {
stepResult := StepResult{
Index: i,
Name: "Step " + string(rune('0'+i)),
Status: StatusSuccess,
}
wb.AddStepResult(jobID, stepResult)
}
done <- true
}()
// Goroutine 2: Read job buffer
go func() {
for range 5 {
jobResult := wb.Jobs[jobID]
if jobResult != nil {
_ = len(jobResult.StepResults)
}
}
done <- true
}()
// Wait for both goroutines to complete
<-done
<-done
// Verify final state
jobResult, exists := wb.Jobs[jobID]
if !exists {
t.Fatal("Job buffer should exist after concurrent operations")
}
if len(jobResult.StepResults) != 10 {
t.Errorf("Expected 10 step results after concurrent operations, got %d", len(jobResult.StepResults))
}
}