Skip to content

Commit 960f162

Browse files
authored
Allow name reuse on different handler types (#321)
Fixes #299
1 parent c3779da commit 960f162

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

temporalio/lib/temporalio/workflow/definition.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def self.method_added(method_name)
280280
@workflow_update_validators ||= {}
281281
@defined_methods ||= []
282282

283-
defn, hash, other_hashes =
283+
defn, hash =
284284
case handler[:type]
285285
when :init
286286
raise "workflow_init was applied to #{method_name} instead of initialize" if method_name != :initialize
@@ -306,7 +306,7 @@ def self.method_added(method_name)
306306
raw_args: handler[:raw_args],
307307
unfinished_policy: handler[:unfinished_policy],
308308
arg_hints: handler[:arg_hints]
309-
), @workflow_signals, [@workflow_queries, @workflow_updates]]
309+
), @workflow_signals]
310310
when :query
311311
[Query.new(
312312
name: handler[:dynamic] ? nil : (handler[:name] || method_name).to_s,
@@ -315,7 +315,7 @@ def self.method_added(method_name)
315315
raw_args: handler[:raw_args],
316316
arg_hints: handler[:arg_hints],
317317
result_hint: handler[:result_hint]
318-
), @workflow_queries, [@workflow_signals, @workflow_updates]]
318+
), @workflow_queries]
319319
when :update
320320
[Update.new(
321321
name: handler[:dynamic] ? nil : (handler[:name] || method_name).to_s,
@@ -325,7 +325,7 @@ def self.method_added(method_name)
325325
unfinished_policy: handler[:unfinished_policy],
326326
arg_hints: handler[:arg_hints],
327327
result_hint: handler[:result_hint]
328-
), @workflow_updates, [@workflow_signals, @workflow_queries]]
328+
), @workflow_updates]
329329
when :dynamic_options
330330
raise 'Dynamic options method already set' if @dynamic_options_method
331331

@@ -341,8 +341,6 @@ def self.method_added(method_name)
341341
if other && other.to_invoke != method_name
342342
raise "Workflow #{handler[:type].name} #{defn.name || '<dynamic>'} defined on " \
343343
"different methods #{other.to_invoke} and #{method_name}"
344-
elsif defn.name && other_hashes.any? { |h| h.include?(defn.name) }
345-
raise "Workflow signal #{defn.name} already defined as a different handler type"
346344
end
347345
hash[defn.name] = defn
348346

temporalio/test/worker_workflow_handler_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,4 +946,34 @@ def test_signal_with_start_user_metadata
946946
assert_equal 'my-details', desc.static_details
947947
end
948948
end
949+
950+
class HandlerNameReuseWorkflow < Temporalio::Workflow::Definition
951+
def execute
952+
Temporalio::Workflow.wait_condition { @signal }
953+
end
954+
955+
workflow_signal name: :foo
956+
def foo_signal(value)
957+
@signal = value
958+
end
959+
960+
workflow_query name: :foo
961+
def foo_query
962+
'query-done'
963+
end
964+
965+
workflow_update name: :foo
966+
def foo_update
967+
'update-done'
968+
end
969+
end
970+
971+
def test_handler_name_reuse
972+
execute_workflow(HandlerNameReuseWorkflow) do |handle|
973+
assert_equal 'query-done', handle.query(HandlerNameReuseWorkflow.foo_query)
974+
assert_equal 'update-done', handle.execute_update(HandlerNameReuseWorkflow.foo_update)
975+
handle.signal(HandlerNameReuseWorkflow.foo_signal, 'signal-done')
976+
assert_equal 'signal-done', handle.result
977+
end
978+
end
949979
end

temporalio/test/workflow/definition_test.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,6 @@ def my_signal_2; end
181181
CODE
182182
end
183183

184-
def test_invalid_duplicate_handlers_different_type
185-
assert_invalid_workflow_code 'my-name already defined as a different handler type', <<~CODE
186-
class TestInvalidDuplicateHandlersDifferentType < Temporalio::Workflow::Definition
187-
workflow_signal name: 'my-name'
188-
def my_signal; end
189-
190-
workflow_update name: 'my-name'
191-
def my_update; end
192-
end
193-
CODE
194-
end
195-
196184
def test_invalid_init_not_on_initialize
197185
assert_invalid_workflow_code 'was applied to not_initialize instead of initialize', <<~CODE
198186
class TestInvalidInitNotOnInitialize < Temporalio::Workflow::Definition

0 commit comments

Comments
 (0)