Skip to content

Commit

Permalink
Use CountDownLatch
Browse files Browse the repository at this point in the history
  • Loading branch information
pitr-ch committed Feb 24, 2018
1 parent 47befe9 commit 8720d17
Showing 1 changed file with 9 additions and 26 deletions.
35 changes: 9 additions & 26 deletions spec/concurrent/promise_spec.rb
Expand Up @@ -371,36 +371,19 @@ def get_ivar_from_args(opts)

it 'preserves ordering of the executed promises' do
10.times do
running = Mutex.new
cond = ConditionVariable.new
cond2 = ConditionVariable.new
latch1 = CountDownLatch.new
latch2 = CountDownLatch.new
executor = SimpleExecutorService.new

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

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

p3 = Concurrent::Promise.execute(executor: executor) do
running.synchronize do
'three'
end
end
p1 = Concurrent::Promise.execute(executor: executor) { latch1.wait; 'one' }
p2 = Concurrent::Promise.execute(executor: executor) { latch2.wait; 'two' }
p3 = Concurrent::Promise.execute(executor: executor) { 'three' }

cond2.signal
cond.signal
latch1.count_down
latch2.count_down

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

0 comments on commit 8720d17

Please sign in to comment.