Skip to content

Commit 1831906

Browse files
author
Petr Chalupa
committed
Merge pull request #353 from iNecas/fix-on-failure-callbacks
Fix Edge::Future#on_failure callbacks
2 parents 2b4a6c7 + 10b45dd commit 1831906

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

lib/concurrent/edge/future.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,10 @@ def reason
456456
def to_sym
457457
:failed
458458
end
459+
460+
def apply(block)
461+
block.call reason
462+
end
459463
end
460464

461465
# @!method state

spec/concurrent/edge/future_spec.rb

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,28 @@
160160

161161
describe 'Future' do
162162
it 'has sync and async callbacks' do
163-
queue = Queue.new
164-
future = Concurrent.future { :value } # executed on FAST_EXECUTOR pool by default
165-
future.on_completion(:io) { queue.push(:async) } # async callback overridden to execute on IO_EXECUTOR pool
166-
future.on_completion! { queue.push(:sync) } # sync callback executed right after completion in the same thread-pool
167-
future.on_success(:io) { queue.push(:async) } # async callback overridden to execute on IO_EXECUTOR pool
168-
future.on_success! { queue.push(:sync) } # sync callback executed right after completion in the same thread-pool
169-
170-
expect(future.value!).to eq :value
171-
expect([queue.pop, queue.pop, queue.pop, queue.pop].sort).to eq [:async, :async, :sync, :sync]
163+
callbacks_tester = ->(future) do
164+
queue = Queue.new
165+
future.on_completion(:io) { |result| queue.push("async on_completion #{ result.inspect }") }
166+
future.on_completion! { |result| queue.push("sync on_completion #{ result.inspect }") }
167+
future.on_success(:io) { |value| queue.push("async on_success #{ value.inspect }") }
168+
future.on_success! { |value| queue.push("sync on_success #{ value.inspect }") }
169+
future.on_failure(:io) { |reason| queue.push("async on_failure #{ reason.inspect }") }
170+
future.on_failure! { |reason| queue.push("sync on_failure #{ reason.inspect }") }
171+
future.wait
172+
[queue.pop, queue.pop, queue.pop, queue.pop].sort
173+
end
174+
callback_results = callbacks_tester.call(Concurrent.future { :value })
175+
expect(callback_results).to eq ["async on_completion [true, :value, nil]",
176+
"async on_success :value",
177+
"sync on_completion [true, :value, nil]",
178+
"sync on_success :value"]
179+
180+
callback_results = callbacks_tester.call(Concurrent.future { raise 'error' })
181+
expect(callback_results).to eq ["async on_completion [false, nil, #<RuntimeError: error>]",
182+
"async on_failure #<RuntimeError: error>",
183+
"sync on_completion [false, nil, #<RuntimeError: error>]",
184+
"sync on_failure #<RuntimeError: error>"]
172185
end
173186

174187
it 'supports setting timeout while waiting' do

0 commit comments

Comments
 (0)