|
160 | 160 |
|
161 | 161 | describe 'Future' do
|
162 | 162 | 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>"] |
172 | 185 | end
|
173 | 186 |
|
174 | 187 | it 'supports setting timeout while waiting' do
|
|
0 commit comments