From 76e1ade0e747f7cec44e6dc721162132ac1c3b44 Mon Sep 17 00:00:00 2001 From: Anthony Dmitriyev Date: Wed, 22 Sep 2021 14:31:23 +0100 Subject: [PATCH 1/2] [WIP] Spec for a child workflow --- .../spec/integration/parent_workflow_spec.rb | 26 +++++++++++++++++-- lib/cadence/workflow.rb | 3 +++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/examples/spec/integration/parent_workflow_spec.rb b/examples/spec/integration/parent_workflow_spec.rb index d87c7ea6..f426c098 100644 --- a/examples/spec/integration/parent_workflow_spec.rb +++ b/examples/spec/integration/parent_workflow_spec.rb @@ -1,3 +1,4 @@ +require 'securerandom' require 'workflows/parent_workflow' describe ParentWorkflow do @@ -14,13 +15,34 @@ expect(HelloWorldWorkflow).to have_received(:execute!) end - it 'executes HelloWorldActivity' do + it 'executes HelloWorldActivity twice' do subject.execute_locally - expect(HelloWorldActivity).to have_received(:execute!).with('Bob') + expect(HelloWorldActivity).to have_received(:execute!).with('Alice').ordered + expect(HelloWorldActivity).to have_received(:execute!).with('Bob').ordered end it 'returns nil' do expect(subject.execute_locally).to eq(nil) end + + context 'integration' do + let(:workflow_id) { SecureRandom.uuid } + + around do |example| + Cadence::Testing.local! do + example.run + end + end + + it 'works' do + run_id = Cadence.start_workflow(described_class, options: { workflow_id: workflow_id }) + info = Cadence.fetch_workflow_execution_info('test', workflow_id, run_id) + + expect(HelloWorldActivity).to have_received(:execute!).with('Alice').ordered + expect(HelloWorldActivity).to have_received(:execute!).with('Bob').ordered + + expect(info).to be_completed + end + end end diff --git a/lib/cadence/workflow.rb b/lib/cadence/workflow.rb index c718b03c..37da0fcf 100644 --- a/lib/cadence/workflow.rb +++ b/lib/cadence/workflow.rb @@ -9,6 +9,7 @@ class Workflow extend ConvenienceMethods def self.execute_in_context(context, input) + previous_context = Cadence::ThreadLocalContext.get Cadence::ThreadLocalContext.set(context) workflow = new(context) @@ -22,6 +23,8 @@ def self.execute_in_context(context, input) Cadence::ErrorHandler.handle(error, metadata: context.metadata) context.fail(error.class.name, error.message) + ensure + Cadence::ThreadLocalContext.set(previous_context) if previous_context end def initialize(context) From c1218dd7e73256e56d0b511defa4ed682a65c6ba Mon Sep 17 00:00:00 2001 From: Anthony Dmitriyev Date: Wed, 22 Sep 2021 18:23:16 +0100 Subject: [PATCH 2/2] Test failure --- examples/activities/hello_world_activity.rb | 2 ++ examples/spec/integration/parent_workflow_spec.rb | 4 ++-- examples/workflows/hello_world_workflow.rb | 2 +- examples/workflows/parent_workflow.rb | 2 ++ 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/examples/activities/hello_world_activity.rb b/examples/activities/hello_world_activity.rb index 9fcd3d91..a957c310 100644 --- a/examples/activities/hello_world_activity.rb +++ b/examples/activities/hello_world_activity.rb @@ -1,5 +1,7 @@ class HelloWorldActivity < Cadence::Activity def execute(name) + raise 'Test error' if name == 'Failure' + p "Hello World, #{name}" return diff --git a/examples/spec/integration/parent_workflow_spec.rb b/examples/spec/integration/parent_workflow_spec.rb index f426c098..df5993e5 100644 --- a/examples/spec/integration/parent_workflow_spec.rb +++ b/examples/spec/integration/parent_workflow_spec.rb @@ -39,8 +39,8 @@ run_id = Cadence.start_workflow(described_class, options: { workflow_id: workflow_id }) info = Cadence.fetch_workflow_execution_info('test', workflow_id, run_id) - expect(HelloWorldActivity).to have_received(:execute!).with('Alice').ordered - expect(HelloWorldActivity).to have_received(:execute!).with('Bob').ordered + expect(HelloWorldActivity).to have_received(:execute!).with('Failure').ordered + expect(HelloWorldActivity).to have_received(:execute!).with('Rescue').ordered expect(info).to be_completed end diff --git a/examples/workflows/hello_world_workflow.rb b/examples/workflows/hello_world_workflow.rb index 31021dd8..92f51a09 100644 --- a/examples/workflows/hello_world_workflow.rb +++ b/examples/workflows/hello_world_workflow.rb @@ -2,7 +2,7 @@ class HelloWorldWorkflow < Cadence::Workflow def execute - HelloWorldActivity.execute!('Alice') + HelloWorldActivity.execute!('Failure') return end diff --git a/examples/workflows/parent_workflow.rb b/examples/workflows/parent_workflow.rb index 68d4e95d..7c44f174 100644 --- a/examples/workflows/parent_workflow.rb +++ b/examples/workflows/parent_workflow.rb @@ -7,5 +7,7 @@ def execute HelloWorldActivity.execute!('Bob') return + rescue + HelloWorldActivity.execute!('Rescue') end end