Skip to content

Commit 7bb72a7

Browse files
committed
Refactored Promise tests to be more deterministic.
1 parent 45c0d8a commit 7bb72a7

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

spec/concurrent/promise_spec.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ module Concurrent
1313
let!(:rejected_reason) { StandardError.new('mojo jojo') }
1414

1515
let(:pending_subject) do
16-
Promise.new(executor: executor){ sleep(0.1); fulfilled_value }.execute
16+
executor = Concurrent::SingleThreadExecutor.new
17+
executor.post{ sleep(5) }
18+
Promise.execute(executor: executor){ fulfilled_value }
1719
end
1820

1921
let(:fulfilled_subject) do
20-
Promise.fulfill(fulfilled_value, executor: executor)
22+
Promise.new(executor: executor){ fulfilled_value }.execute.tap{ sleep(0.1) }
2123
end
2224

2325
let(:rejected_subject) do
24-
Promise.reject(rejected_reason, executor: executor)
26+
Promise.new(executor: executor){ raise rejected_reason }.execute.tap{ sleep(0.1) }
2527
end
2628

2729
it_should_behave_like :ivar do
@@ -149,13 +151,21 @@ def get_ivar_from_args(opts)
149151
context 'pending' do
150152

151153
it 'sets the promise to :pending' do
152-
p = pending_subject.execute
154+
latch = CountDownLatch.new
155+
p = Promise.new{ latch.wait(1) }.execute
153156
expect(p).to be_pending
157+
latch.count_down
154158
end
155159

156-
it 'does not posts again' do
160+
it 'does not post again' do
161+
executor = SimpleExecutorService.new
157162
expect(executor).to receive(:post).with(any_args).once
158-
pending_subject.execute
163+
164+
latch = CountDownLatch.new
165+
p = Promise.new(executor: executor){ latch.wait(1) }.execute
166+
167+
10.times { p.execute }
168+
latch.count_down
159169
end
160170
end
161171

0 commit comments

Comments
 (0)