|
| 1 | +package engine_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + . "github.com/onsi/ginkgo/v2" |
| 10 | + . "github.com/onsi/gomega" |
| 11 | + |
| 12 | + "github.com/jahvon/flow/internal/runner/engine" |
| 13 | +) |
| 14 | + |
| 15 | +func TestEngine_Execute(t *testing.T) { |
| 16 | + RegisterFailHandler(Fail) |
| 17 | + RunSpecs(t, "Execute Engine Suite") |
| 18 | +} |
| 19 | + |
| 20 | +var _ = Describe("e.Execute", func() { |
| 21 | + var ( |
| 22 | + eng engine.Engine |
| 23 | + ctx context.Context |
| 24 | + cancel context.CancelFunc |
| 25 | + ) |
| 26 | + |
| 27 | + BeforeEach(func() { |
| 28 | + eng = engine.NewExecEngine() |
| 29 | + ctx, cancel = context.WithCancel(context.Background()) |
| 30 | + }) |
| 31 | + |
| 32 | + AfterEach(func() { |
| 33 | + cancel() |
| 34 | + }) |
| 35 | + |
| 36 | + Context("Parallel execution", func() { |
| 37 | + It("should execute execs in parallel", func() { |
| 38 | + execs := []engine.Exec{ |
| 39 | + {ID: "exec1", Function: func() error { time.Sleep(100 * time.Millisecond); return nil }}, |
| 40 | + {ID: "exec2", Function: func() error { return nil }}, |
| 41 | + } |
| 42 | + |
| 43 | + start := time.Now() |
| 44 | + ff := false |
| 45 | + summary := eng.Execute(ctx, execs, engine.WithMode(engine.Parallel), engine.WithFailFast(&ff)) |
| 46 | + duration := time.Since(start) |
| 47 | + |
| 48 | + Expect(summary.Results).To(HaveLen(2)) |
| 49 | + Expect(summary.Results[0].Error).NotTo(HaveOccurred()) |
| 50 | + Expect(summary.Results[1].Error).NotTo(HaveOccurred()) |
| 51 | + Expect(duration).To(BeNumerically("<", 200*time.Millisecond)) |
| 52 | + }) |
| 53 | + |
| 54 | + It("should handle exec failures with fail fast", func() { |
| 55 | + execs := []engine.Exec{ |
| 56 | + {ID: "exec1", Function: func() error { return errors.New("error") }}, |
| 57 | + {ID: "exec2", Function: func() error { time.Sleep(100 * time.Millisecond); return nil }}, |
| 58 | + } |
| 59 | + |
| 60 | + ff := true |
| 61 | + summary := eng.Execute(ctx, execs, engine.WithMode(engine.Parallel), engine.WithFailFast(&ff)) |
| 62 | + |
| 63 | + Expect(summary.Results).To(HaveLen(2)) |
| 64 | + Expect(summary.Results[0].Error).To(HaveOccurred()) |
| 65 | + Expect(summary.Results[1].Error).ToNot(HaveOccurred()) |
| 66 | + Expect(summary.HasErrors()).To(BeTrue()) |
| 67 | + }) |
| 68 | + |
| 69 | + It("should limit the number of concurrent execs", func() { |
| 70 | + execs := []engine.Exec{ |
| 71 | + {ID: "exec1", Function: func() error { time.Sleep(100 * time.Millisecond); return nil }}, |
| 72 | + {ID: "exec2", Function: func() error { time.Sleep(100 * time.Millisecond); return nil }}, |
| 73 | + {ID: "exec3", Function: func() error { time.Sleep(100 * time.Millisecond); return nil }}, |
| 74 | + {ID: "exec4", Function: func() error { time.Sleep(100 * time.Millisecond); return nil }}, |
| 75 | + {ID: "exec5", Function: func() error { time.Sleep(100 * time.Millisecond); return nil }}, |
| 76 | + } |
| 77 | + |
| 78 | + start := time.Now() |
| 79 | + ff := false |
| 80 | + summary := eng.Execute(ctx, execs, |
| 81 | + engine.WithMode(engine.Parallel), engine.WithFailFast(&ff), engine.WithMaxThreads(2)) |
| 82 | + duration := time.Since(start) |
| 83 | + |
| 84 | + Expect(summary.Results).To(HaveLen(5)) |
| 85 | + Expect(summary.Results[0].Error).NotTo(HaveOccurred()) |
| 86 | + Expect(summary.Results[1].Error).NotTo(HaveOccurred()) |
| 87 | + Expect(summary.Results[2].Error).NotTo(HaveOccurred()) |
| 88 | + Expect(summary.Results[3].Error).NotTo(HaveOccurred()) |
| 89 | + Expect(summary.Results[4].Error).NotTo(HaveOccurred()) |
| 90 | + Expect(duration).To(BeNumerically(">=", 250*time.Millisecond)) |
| 91 | + }) |
| 92 | + }) |
| 93 | + |
| 94 | + Context("Serial execution", func() { |
| 95 | + It("should execute execs serially", func() { |
| 96 | + execs := []engine.Exec{ |
| 97 | + {ID: "exec1", Function: func() error { time.Sleep(100 * time.Millisecond); return nil }}, |
| 98 | + {ID: "exec2", Function: func() error { time.Sleep(110 * time.Millisecond); return nil }}, |
| 99 | + } |
| 100 | + |
| 101 | + start := time.Now() |
| 102 | + ff := false |
| 103 | + summary := eng.Execute(ctx, execs, engine.WithMode(engine.Serial), engine.WithFailFast(&ff)) |
| 104 | + duration := time.Since(start) |
| 105 | + |
| 106 | + Expect(summary.Results).To(HaveLen(2)) |
| 107 | + Expect(summary.Results[0].Error).NotTo(HaveOccurred()) |
| 108 | + Expect(summary.Results[1].Error).NotTo(HaveOccurred()) |
| 109 | + Expect(duration).To(BeNumerically(">=", 200*time.Millisecond)) |
| 110 | + }) |
| 111 | + |
| 112 | + It("should handle exec failures with fail fast", func() { |
| 113 | + execs := []engine.Exec{ |
| 114 | + {ID: "exec1", Function: func() error { return errors.New("error") }}, |
| 115 | + {ID: "exec2", Function: func() error { return nil }}, |
| 116 | + } |
| 117 | + |
| 118 | + ff := true |
| 119 | + summary := eng.Execute(ctx, execs, engine.WithMode(engine.Serial), engine.WithFailFast(&ff)) |
| 120 | + |
| 121 | + Expect(summary.Results).To(HaveLen(1)) |
| 122 | + Expect(summary.Results[0].Error).To(HaveOccurred()) |
| 123 | + Expect(summary.HasErrors()).To(BeTrue()) |
| 124 | + }) |
| 125 | + }) |
| 126 | +}) |
0 commit comments