-
Notifications
You must be signed in to change notification settings - Fork 107
Named signal handlers #157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e47fde8
tests for named signal handlers
chuckremes2 d15110d
support for named signal handlers
chuckremes2 32127cb
improve tests and disallow duplicate registration of named handlers
chuckremes2 21f3d99
extra work to make sure we only match non-nil handler_name
chuckremes2 1109741
change behavior
chuckremes2 af43a41
remove unused constant; update docs; rename method
chuckremes2 9f4ccca
verify named handler and default are both always called
chuckremes2 693533a
test for proper dispatcher behavior with named and defaults
chuckremes2 47881f5
correct tests to expect new behavior
chuckremes2 f909fd6
change logging so named handler is easier to pick out
chuckremes2 757d96d
fix signal event registration to use special naming and a WILDCARD
chuckremes2 d3bad0b
restore original simple API
chuckremes2 ab05211
construct proper signal event name before dispatching; fix method sig…
chuckremes2 945809a
fix unit tests
chuckremes2 84bab22
use 'signaled' as base event and dispatch twice to get named and catc…
chuckremes2 e5234f4
switch to dedicated Signal target
chuckremes2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| require 'workflows/wait_for_named_signal_workflow' | ||
|
|
||
| describe WaitForNamedSignalWorkflow, :integration do | ||
| let(:receiver_workflow_id) { SecureRandom.uuid } | ||
|
|
||
| context 'when the signal is named' do | ||
| let(:arg1) { "arg1" } | ||
| let(:arg2) { 7890.1234 } | ||
|
|
||
| context 'and the workflow has a named signal handler matching the signal name' do | ||
| let(:signal_name) { "NamedSignal" } | ||
|
|
||
| it 'receives the signal in its named handler' do | ||
| _, run_id = run_workflow(WaitForNamedSignalWorkflow, signal_name, options: { workflow_id: receiver_workflow_id}) | ||
|
|
||
| Temporal.signal_workflow(WaitForNamedSignalWorkflow, signal_name, receiver_workflow_id, run_id, [arg1, arg2]) | ||
|
|
||
| result = Temporal.await_workflow_result( | ||
| WaitForNamedSignalWorkflow, | ||
| workflow_id: receiver_workflow_id, | ||
| run_id: run_id, | ||
| ) | ||
|
|
||
| expect(result[:received]).to include({signal_name => [arg1, arg2]}) | ||
| expect(result[:counts]).to include({signal_name => 1}) | ||
| expect(result).to eq( | ||
| { | ||
| received: { | ||
| signal_name => [arg1, arg2], | ||
| 'catch-all' => [arg1, arg2] | ||
| }, | ||
| counts: { | ||
| signal_name => 1, | ||
| 'catch-all' => 1 | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| end | ||
|
|
||
| it 'receives the signal in its catch-all signal handler' do | ||
| _, run_id = run_workflow(WaitForNamedSignalWorkflow, signal_name, options: { workflow_id: receiver_workflow_id}) | ||
|
|
||
| Temporal.signal_workflow(WaitForNamedSignalWorkflow, signal_name, receiver_workflow_id, run_id, [arg1, arg2]) | ||
|
|
||
| result = Temporal.await_workflow_result( | ||
| WaitForNamedSignalWorkflow, | ||
| workflow_id: receiver_workflow_id, | ||
| run_id: run_id, | ||
| ) | ||
|
|
||
| expect(result[:received]).to include({"catch-all" => [arg1, arg2]}) | ||
| expect(result[:counts]).to include({"catch-all" => 1}) | ||
| end | ||
| end | ||
|
|
||
| context 'and the workflow does NOT have a named signal handler matching the signal name' do | ||
| let(:signal_name) { 'doesNOTmatchAsignalHandler' } | ||
|
|
||
| it 'receives the signal in its catch-all signal handler' do | ||
| _, run_id = run_workflow(WaitForNamedSignalWorkflow, signal_name, options: { workflow_id: receiver_workflow_id}) | ||
|
|
||
| Temporal.signal_workflow(WaitForNamedSignalWorkflow, signal_name, receiver_workflow_id, run_id, [arg1, arg2]) | ||
|
|
||
| result = Temporal.await_workflow_result( | ||
| WaitForNamedSignalWorkflow, | ||
| workflow_id: receiver_workflow_id, | ||
| run_id: run_id, | ||
| ) | ||
|
|
||
| expect(result).to eq( | ||
| { | ||
| received: { | ||
| 'catch-all' => [arg1, arg2] | ||
| }, | ||
| counts: { | ||
| 'catch-all' => 1 | ||
| } | ||
| } | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Can receive signals to its named signal handler. If a signal doesn't match the | ||
| # named handler's signature, then it matches the catch-all signal handler | ||
| # | ||
| class WaitForNamedSignalWorkflow < Temporal::Workflow | ||
| def execute(expected_signal) | ||
| signals_received = {} | ||
| signal_counts = Hash.new { |h,k| h[k] = 0 } | ||
|
|
||
| # catch-all handler | ||
| workflow.on_signal do |signal, input| | ||
| workflow.logger.info("Received signal name as #{signal}, with input #{input.inspect}") | ||
| signals_received['catch-all'] = input | ||
| signal_counts['catch-all'] += 1 | ||
| end | ||
|
|
||
| workflow.on_signal('NamedSignal') do |input| | ||
| workflow.logger.info("Received signal name -NamedSignal-, with input #{input.inspect}") | ||
| signals_received['NamedSignal'] = input | ||
| signal_counts['NamedSignal'] += 1 | ||
| end | ||
|
|
||
| timeout_timer = workflow.start_timer(1) | ||
| workflow.wait_for(timeout_timer) | ||
|
|
||
| { received: signals_received, counts: signal_counts } | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| module Temporal | ||
| class Workflow | ||
| Signal = Struct.new(:signal_name) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like a cleaner solution would be to add the
handler_nametoevent_nameand dispatch the event twice:I think this would keep the dispatcher simpler for now and does allow us to achieve everything that is needed