Skip to content

Commit

Permalink
Use executor from arg in then_on/rescue_on/chain_on for Promises
Browse files Browse the repository at this point in the history
  • Loading branch information
tgwizard authored and eregon committed Nov 17, 2023
1 parent 25ccddc commit 53a67a1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions lib/concurrent-ruby/concurrent/promises.rb
Expand Up @@ -611,7 +611,7 @@ def chain(*args, &task)
# @yieldparam [Object] value
# @yieldparam [Object] reason
def chain_on(executor, *args, &task)
ChainPromise.new_blocked_by1(self, @DefaultExecutor, executor, args, &task).future
ChainPromise.new_blocked_by1(self, executor, executor, args, &task).future
end

# @return [String] Short string representation.
Expand Down Expand Up @@ -1034,7 +1034,7 @@ def then(*args, &task)
# @return [Future]
# @yield [value, *args] to the task.
def then_on(executor, *args, &task)
ThenPromise.new_blocked_by1(self, @DefaultExecutor, executor, args, &task).future
ThenPromise.new_blocked_by1(self, executor, executor, args, &task).future
end

# @!macro promises.shortcut.on
Expand All @@ -1052,7 +1052,7 @@ def rescue(*args, &task)
# @return [Future]
# @yield [reason, *args] to the task.
def rescue_on(executor, *args, &task)
RescuePromise.new_blocked_by1(self, @DefaultExecutor, executor, args, &task).future
RescuePromise.new_blocked_by1(self, executor, executor, args, &task).future
end

# @!macro promises.method.zip
Expand Down
22 changes: 11 additions & 11 deletions spec/concurrent/promises_spec.rb
Expand Up @@ -380,12 +380,12 @@ def behaves_as_delay(delay, value)
end

it 'chains' do
future0 = future { 1 }.then { |v| v + 2 } # both executed on default FAST_EXECUTOR
future1 = future0.then_on(:fast) { raise 'boo' } # executed on IO_EXECUTOR
future2 = future1.then { |v| v + 1 } # will reject with 'boo' error, executed on default FAST_EXECUTOR
future3 = future1.rescue { |err| err.message } # executed on default FAST_EXECUTOR
future4 = future0.chain { |success, value, reason| success } # executed on default FAST_EXECUTOR
future5 = future3.with_default_executor(:fast) # connects new future with different executor, the new future is resolved when future3 is
future0 = future { 1 }.then { |v| v + 2 } # both executed on default IO_EXECUTOR
future1 = future0.then_on(:fast) { raise 'boo' } # executed on FAST_EXECUTOR
future2 = future1.then { |v| v + 1 } # will reject with 'boo' error, executed on FAST_EXECUTOR
future3 = future1.rescue { |err| err.message } # executed on FAST_EXECUTOR
future4 = future0.chain { |success, value, reason| success } # executed on FAST_EXECUTOR
future5 = future3.with_default_executor(:io) # connects new future with different executor, the new future is resolved when future3 is
future6 = future5.then(&:capitalize) # executes on IO_EXECUTOR because default was set to :io on future5
future7 = future0 & future3
future8 = future0.rescue { raise 'never happens' } # future0 fulfills so future8'll have same value as future 0
Expand All @@ -402,12 +402,12 @@ def behaves_as_delay(delay, value)
expect(table.join("\n")).to eq <<-TABLE.gsub(/^\s+\|/, '').strip
|index success value reason pool d.pool
| 0 true 3 io io
| 1 false boo fast io
| 2 false boo io io
| 3 true boo io io
| 1 false boo fast fast
| 2 false boo fast fast
| 3 true boo fast fast
| 4 true true io io
| 5 true boo fast
| 6 true Boo fast fast
| 5 true boo io
| 6 true Boo io io
| 7 true [3, "boo"] io
| 8 true 3 io io
TABLE
Expand Down

0 comments on commit 53a67a1

Please sign in to comment.