Skip to content

Commit

Permalink
Use CVs to test ordering of Promise#zip resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
ianks committed Nov 16, 2017
1 parent d9ab262 commit 84f4591
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions spec/concurrent/promise_spec.rb
Expand Up @@ -370,24 +370,38 @@ def get_ivar_from_args(opts)
end

it 'preserves ordering of the executed promises' do
promise1 = Promise.execute do
# resolves after the second promise
sleep 0.2
'one'
end
10.times do
running = Mutex.new
cond = ConditionVariable.new
cond2 = ConditionVariable.new
executor = SimpleExecutorService.new

promise2 = Promise.execute do
sleep 0.1
'two'
end
p1 = Concurrent::Promise.execute(executor: executor) do
running.synchronize do
cond.wait(running)
'one'
end
end

promise3 = Promise.execute do
'three'
end
p2 = Concurrent::Promise.execute(executor: executor) do
running.synchronize do
cond2.wait(running)
'two'
end
end

result = promise1.zip(promise2, promise3).value
p3 = Concurrent::Promise.execute(executor: executor) do
running.synchronize do
'three'
end
end

expect(result).to eql(['one', 'two', 'three'])
cond2.signal
cond.signal

result = Concurrent::Promise.zip(p1, p2, p3).value
expect(result) .to eq(['one', 'two', 'three'])
end
end
end

Expand Down

0 comments on commit 84f4591

Please sign in to comment.