Skip to content

Commit 952f56b

Browse files
authored
Merge pull request #660 from ianks/promise-zip-ordering
Add specs for Promise#zip/Promise.zip ordering
2 parents 705325e + 8720d17 commit 952f56b

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

spec/concurrent/promise_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,24 @@ def get_ivar_from_args(opts)
394394

395395
expect(composite).to be_rejected
396396
end
397+
398+
it 'preserves ordering of the executed promises' do
399+
10.times do
400+
latch1 = CountDownLatch.new
401+
latch2 = CountDownLatch.new
402+
executor = SimpleExecutorService.new
403+
404+
p1 = Concurrent::Promise.execute(executor: executor) { latch1.wait; 'one' }
405+
p2 = Concurrent::Promise.execute(executor: executor) { latch2.wait; 'two' }
406+
p3 = Concurrent::Promise.execute(executor: executor) { 'three' }
407+
408+
latch1.count_down
409+
latch2.count_down
410+
411+
result = Concurrent::Promise.zip(p1, p2, p3).value!
412+
expect(result).to eq(['one', 'two', 'three'])
413+
end
414+
end
397415
end
398416

399417
describe '.zip' do
@@ -438,6 +456,27 @@ def get_ivar_from_args(opts)
438456

439457
expect(composite).to be_rejected
440458
end
459+
460+
it 'preserves ordering of the executed promises' do
461+
promise1 = Promise.execute do
462+
# resolves after the second promise
463+
sleep 0.2
464+
'one'
465+
end
466+
467+
promise2 = Promise.execute do
468+
sleep 0.1
469+
'two'
470+
end
471+
472+
promise3 = Promise.execute do
473+
'three'
474+
end
475+
476+
result = Promise.zip(promise1, promise2, promise3).value
477+
478+
expect(result).to eql(['one', 'two', 'three'])
479+
end
441480
end
442481

443482
describe 'aggregators' do

0 commit comments

Comments
 (0)