Skip to content

Commit 8720d17

Browse files
committed
Use CountDownLatch
1 parent 47befe9 commit 8720d17

File tree

1 file changed

+9
-26
lines changed

1 file changed

+9
-26
lines changed

spec/concurrent/promise_spec.rb

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -371,36 +371,19 @@ def get_ivar_from_args(opts)
371371

372372
it 'preserves ordering of the executed promises' do
373373
10.times do
374-
running = Mutex.new
375-
cond = ConditionVariable.new
376-
cond2 = ConditionVariable.new
374+
latch1 = CountDownLatch.new
375+
latch2 = CountDownLatch.new
377376
executor = SimpleExecutorService.new
378377

379-
p1 = Concurrent::Promise.execute(executor: executor) do
380-
running.synchronize do
381-
cond.wait(running)
382-
'one'
383-
end
384-
end
385-
386-
p2 = Concurrent::Promise.execute(executor: executor) do
387-
running.synchronize do
388-
cond2.wait(running)
389-
'two'
390-
end
391-
end
392-
393-
p3 = Concurrent::Promise.execute(executor: executor) do
394-
running.synchronize do
395-
'three'
396-
end
397-
end
378+
p1 = Concurrent::Promise.execute(executor: executor) { latch1.wait; 'one' }
379+
p2 = Concurrent::Promise.execute(executor: executor) { latch2.wait; 'two' }
380+
p3 = Concurrent::Promise.execute(executor: executor) { 'three' }
398381

399-
cond2.signal
400-
cond.signal
382+
latch1.count_down
383+
latch2.count_down
401384

402-
result = Concurrent::Promise.zip(p1, p2, p3).value
403-
expect(result) .to eq(['one', 'two', 'three'])
385+
result = Concurrent::Promise.zip(p1, p2, p3).value!
386+
expect(result).to eq(['one', 'two', 'three'])
404387
end
405388
end
406389
end

0 commit comments

Comments
 (0)