Skip to content

Commit 47befe9

Browse files
iankspitr-ch
authored andcommitted
Use CVs to test ordering of Promise#zip resolution
1 parent 94d19cd commit 47befe9

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

spec/concurrent/promise_spec.rb

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -370,24 +370,38 @@ def get_ivar_from_args(opts)
370370
end
371371

372372
it 'preserves ordering of the executed promises' do
373-
promise1 = Promise.execute do
374-
# resolves after the second promise
375-
sleep 0.2
376-
'one'
377-
end
373+
10.times do
374+
running = Mutex.new
375+
cond = ConditionVariable.new
376+
cond2 = ConditionVariable.new
377+
executor = SimpleExecutorService.new
378378

379-
promise2 = Promise.execute do
380-
sleep 0.1
381-
'two'
382-
end
379+
p1 = Concurrent::Promise.execute(executor: executor) do
380+
running.synchronize do
381+
cond.wait(running)
382+
'one'
383+
end
384+
end
383385

384-
promise3 = Promise.execute do
385-
'three'
386-
end
386+
p2 = Concurrent::Promise.execute(executor: executor) do
387+
running.synchronize do
388+
cond2.wait(running)
389+
'two'
390+
end
391+
end
387392

388-
result = promise1.zip(promise2, promise3).value
393+
p3 = Concurrent::Promise.execute(executor: executor) do
394+
running.synchronize do
395+
'three'
396+
end
397+
end
389398

390-
expect(result).to eql(['one', 'two', 'three'])
399+
cond2.signal
400+
cond.signal
401+
402+
result = Concurrent::Promise.zip(p1, p2, p3).value
403+
expect(result) .to eq(['one', 'two', 'three'])
404+
end
391405
end
392406
end
393407

0 commit comments

Comments
 (0)