From 84f45918c1ea1899c0f85d3daa4ce0bb221564c9 Mon Sep 17 00:00:00 2001 From: Ian Ker-Seymer Date: Wed, 15 Nov 2017 23:24:47 -0500 Subject: [PATCH] Use CVs to test ordering of Promise#zip resolution --- spec/concurrent/promise_spec.rb | 42 ++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/spec/concurrent/promise_spec.rb b/spec/concurrent/promise_spec.rb index ba9ffec89..304ce628f 100644 --- a/spec/concurrent/promise_spec.rb +++ b/spec/concurrent/promise_spec.rb @@ -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