Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/temporal/activity.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'temporal/activity/workflow_convenience_methods'
require 'temporal/callable'
require 'temporal/concerns/executable'
require 'temporal/errors'

Expand All @@ -9,7 +10,9 @@ class Activity

def self.execute_in_context(context, input)
activity = new(context)
activity.execute(*input)
callable = Temporal::Callable.new(method: activity.method(:execute))

callable.call(input)
end

def initialize(context)
Expand Down
19 changes: 19 additions & 0 deletions lib/temporal/callable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Temporal
class Callable
def initialize(method:)
@method = method
end

def call(input)
if input.is_a?(Array) && input.last.instance_of?(Hash)
*args, kwargs = input

@method.call(*args, **kwargs)
else
@method.call(*input)
end
end
end
end
5 changes: 4 additions & 1 deletion lib/temporal/workflow.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'temporal/callable'
require 'temporal/concerns/executable'
require 'temporal/workflow/convenience_methods'
require 'temporal/thread_local_context'
Expand All @@ -13,7 +14,9 @@ def self.execute_in_context(context, input)
Temporal::ThreadLocalContext.set(context)

workflow = new(context)
result = workflow.execute(*input)
callable = Temporal::Callable.new(method: workflow.method(:execute))

result = callable.call(input)

context.complete(result) unless context.completed?
rescue StandardError, ScriptError => error
Expand Down
49 changes: 45 additions & 4 deletions spec/unit/lib/temporal/activity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,28 @@
describe Temporal::Activity do
it_behaves_like 'an executable'

class ArgsActivity < Temporal::Activity
def execute(a)
'args result'
end
end

class KwargsActivity < Temporal::Activity
def execute(a, b:, c:)
'kwargs result'
end
end

subject { described_class.new(context) }
let(:context) { instance_double('Temporal::Activity::Context') }

describe '.execute_in_context' do
subject { ArgsActivity.new(context) }

let(:input) { ['test'] }

before do
allow(described_class).to receive(:new).and_return(subject)
allow(subject).to receive(:execute).and_return('result')
end

it 'passes the context' do
Expand All @@ -22,13 +35,41 @@
end

it 'calls #execute' do
described_class.execute_in_context(context, input)
expect(subject).to receive(:execute).with(*input)

expect(subject).to have_received(:execute).with(*input)
described_class.execute_in_context(context, input)
end

it 'returns #execute result' do
expect(described_class.execute_in_context(context, input)).to eq('result')
expect(described_class.execute_in_context(context, input)).to eq('args result')
end

context 'when using keyword arguments' do
subject { KwargsActivity.new(context) }

let(:input) { ['test', { b: 'b', c: 'c' }] }

it 'passes the context' do
described_class.execute_in_context(context, input)

expect(described_class).to have_received(:new).with(context)
end

it 'calls #execute' do
expect(subject).to receive(:execute).with('test', b: 'b', c: 'c')

described_class.execute_in_context(context, input)
end

it 'does not raise an ArgumentError' do
expect {
described_class.execute_in_context(context, input)
}.not_to raise_error(ArgumentError, 'wrong number of arguments (given 2, expected 1; required keywords: b, c)')
end

it 'returns #execute result' do
expect(described_class.execute_in_context(context, input)).to eq('kwargs result')
end
end
end

Expand Down
72 changes: 72 additions & 0 deletions spec/unit/lib/temporal/workflow_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,78 @@
require 'temporal/workflow'
require 'temporal/workflow/context'
require 'shared_examples/an_executable'

describe Temporal::Workflow do
it_behaves_like 'an executable'

class ArgsWorkflow < Temporal::Workflow
def execute(a)
'args result'
end
end

class KwargsWorkflow < Temporal::Workflow
def execute(a, b:, c:)
'kwargs result'
end
end

subject { described_class.new(ctx) }
let(:ctx) { instance_double('Temporal::Workflow::Context') }

before do
allow(ctx).to receive(:completed?).and_return(true)
end

describe '.execute_in_context' do
subject { ArgsWorkflow.new(ctx) }

let(:input) { ['test'] }

before do
allow(described_class).to receive(:new).and_return(subject)
end

it 'passes the context' do
described_class.execute_in_context(ctx, input)

expect(described_class).to have_received(:new).with(ctx)
end

it 'calls #execute' do
expect(subject).to receive(:execute).with(*input)

described_class.execute_in_context(ctx, input)
end

context 'when using keyword arguments' do
subject { KwargsWorkflow.new(ctx) }

let(:input) { ['test', { b: 'b', c: 'c' }] }

it 'passes the context' do
described_class.execute_in_context(ctx, input)

expect(described_class).to have_received(:new).with(ctx)
end

it 'calls #execute' do
expect(subject).to receive(:execute).with('test', b: 'b', c: 'c')

described_class.execute_in_context(ctx, input)
end

it 'does not raise an ArgumentError' do
expect {
described_class.execute_in_context(ctx, input)
}.not_to raise_error(ArgumentError, 'wrong number of arguments (given 2, expected 1; required keywords: b, c)')
end
end
end

describe '#execute' do
it 'is not implemented on a superclass' do
expect { subject.execute }.to raise_error(NotImplementedError)
end
end
end