|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe Mars::Workflows::Parallel do |
| 4 | + let(:add_step_class) do |
| 5 | + Class.new do |
| 6 | + def initialize(value) |
| 7 | + @value = value |
| 8 | + end |
| 9 | + |
| 10 | + def run(input) |
| 11 | + sleep 0.1 |
| 12 | + puts "add step: #{input}" |
| 13 | + input + @value |
| 14 | + end |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + let(:multiply_step_class) do |
| 19 | + Class.new do |
| 20 | + def initialize(multiplier) |
| 21 | + @multiplier = multiplier |
| 22 | + end |
| 23 | + |
| 24 | + def run(input) |
| 25 | + puts "multiply step: #{input}" |
| 26 | + input * @multiplier |
| 27 | + end |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + let(:error_step_class) do |
| 32 | + Class.new do |
| 33 | + attr_reader :name |
| 34 | + |
| 35 | + def initialize(message, name) |
| 36 | + @message = message |
| 37 | + @name = name |
| 38 | + end |
| 39 | + |
| 40 | + def run(_input) |
| 41 | + puts "error step: #{@name}" |
| 42 | + raise StandardError, @message |
| 43 | + end |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + describe "#run" do |
| 48 | + it "executes steps in parallel" do |
| 49 | + add_five = add_step_class.new(5) |
| 50 | + multiply_three = multiply_step_class.new(3) |
| 51 | + add_two = add_step_class.new(2) |
| 52 | + |
| 53 | + workflow = described_class.new("math_workflow", steps: [add_five, multiply_three, add_two]) |
| 54 | + |
| 55 | + # 10 + 5 = 15, 10 * 3 = 30, 10 + 2 = 12 |
| 56 | + expect(workflow.run(10)).to eq("15\n30\n12") |
| 57 | + end |
| 58 | + |
| 59 | + it "executes steps in parallel with a custom aggregator" do |
| 60 | + add_five = add_step_class.new(5) |
| 61 | + multiply_three = multiply_step_class.new(3) |
| 62 | + add_two = add_step_class.new(2) |
| 63 | + aggregator = Mars::Aggregator.new("Custom Aggregator", operation: lambda(&:sum)) |
| 64 | + workflow = described_class.new("math_workflow", steps: [add_five, multiply_three, add_two], |
| 65 | + aggregator: aggregator) |
| 66 | + |
| 67 | + expect(workflow.run(10)).to eq(57) |
| 68 | + end |
| 69 | + |
| 70 | + it "handles single step" do |
| 71 | + multiply_step = multiply_step_class.new(7) |
| 72 | + workflow = described_class.new("single_step", steps: [multiply_step]) |
| 73 | + |
| 74 | + expect(workflow.run(6)).to eq("42") |
| 75 | + end |
| 76 | + |
| 77 | + it "returns input unchanged when no steps" do |
| 78 | + workflow = described_class.new("empty", steps: []) |
| 79 | + |
| 80 | + expect(workflow.run(42)).to eq("") |
| 81 | + end |
| 82 | + |
| 83 | + it "propagates errors from steps" do |
| 84 | + add_step = add_step_class.new(5) |
| 85 | + error_step = error_step_class.new("Step failed", "error_step_one") |
| 86 | + error_step_two = error_step_class.new("Step failed two", "error_step_two") |
| 87 | + |
| 88 | + workflow = described_class.new("error_workflow", steps: [add_step, error_step, error_step_two]) |
| 89 | + |
| 90 | + expect { workflow.run(10) }.to raise_error( |
| 91 | + Mars::Workflows::AggregateError, |
| 92 | + "error_step_one: Step failed\nerror_step_two: Step failed two" |
| 93 | + ) |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + describe "inheritance" do |
| 98 | + it "inherits from Mars::Runnable" do |
| 99 | + workflow = described_class.new("test", steps: []) |
| 100 | + expect(workflow).to be_a(Mars::Runnable) |
| 101 | + end |
| 102 | + end |
| 103 | +end |
0 commit comments