Skip to content

Commit 45c0d8a

Browse files
committed
Removed all sleep calls from Promise specs.
1 parent 1da7c0e commit 45c0d8a

File tree

1 file changed

+60
-60
lines changed

1 file changed

+60
-60
lines changed

spec/concurrent/promise_spec.rb

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -97,28 +97,27 @@ def get_ivar_from_args(opts)
9797

9898
describe '.new' do
9999
it 'should return an unscheduled Promise' do
100-
p = Promise.new(executor: executor){ nil }
100+
p = Promise.new(executor: :immediate){ nil }
101101
expect(p).to be_unscheduled
102102
end
103103
end
104104

105105
describe '.execute' do
106106
it 'creates a new Promise' do
107-
p = Promise.execute(executor: executor){ nil }
107+
p = Promise.execute(executor: :immediate){ nil }
108108
expect(p).to be_a(Promise)
109109
end
110110

111111
it 'passes the block to the new Promise' do
112-
p = Promise.execute(executor: executor){ 20 }
113-
sleep(0.1)
112+
p = Promise.execute(executor: :immediate){ 20 }
114113
expect(p.value).to eq 20
115114
end
116115

117116
it 'calls #execute on the new Promise' do
118117
p = double('promise')
119-
allow(Promise).to receive(:new).with({executor: executor}).and_return(p)
118+
allow(Promise).to receive(:new).with(any_args).and_return(p)
120119
expect(p).to receive(:execute).with(no_args)
121-
Promise.execute(executor: executor){ nil }
120+
Promise.execute(executor: :immediate){ nil }
122121
end
123122
end
124123
end
@@ -128,12 +127,21 @@ def get_ivar_from_args(opts)
128127
context 'unscheduled' do
129128

130129
it 'sets the promise to :pending' do
131-
p = Promise.new(executor: executor){ sleep(0.1) }.execute
130+
start_latch = CountDownLatch.new
131+
end_latch = CountDownLatch.new
132+
p = Promise.new(executor: executor) do
133+
start_latch.count_down
134+
end_latch.wait(1)
135+
end
136+
start_latch.wait(1)
137+
p.execute
132138
expect(p).to be_pending
139+
end_latch.count_down
133140
end
134141

135142
it 'posts the block given in construction' do
136-
expect(executor).to receive(:post).with(any_args)
143+
executor = ImmediateExecutor.new
144+
expect(executor).to receive(:post).with(any_args).and_call_original
137145
Promise.new(executor: executor){ nil }.execute
138146
end
139147
end
@@ -153,27 +161,28 @@ def get_ivar_from_args(opts)
153161

154162
describe 'with children' do
155163

156-
let(:root) { Promise.new(executor: executor){ sleep(0.1); nil } }
157-
let(:c1) { root.then { sleep(0.1); nil } }
158-
let(:c2) { root.then { sleep(0.1); nil } }
159-
let(:c2_1) { c2.then { sleep(0.1); nil } }
164+
let(:start_latch) { CountDownLatch.new(4) }
165+
let(:end_latch) { CountDownLatch.new(1) }
166+
let(:root) { Promise.new(executor: executor){ start_latch.count_down; end_latch.wait(5) } }
167+
let(:c1) { root.then { start_latch.count_down; end_latch.wait(5) } }
168+
let(:c2) { root.then { start_latch.count_down; end_latch.wait(5) } }
169+
let(:c2_1) { c2.then { start_latch.count_down; end_latch.wait(5) } }
160170

161171
context 'when called on the root' do
162172
it 'should set all promises to :pending' do
163173
root.execute
164-
165-
expect(c1).to be_pending
166-
expect(c2).to be_pending
167-
expect(c2_1).to be_pending
174+
start_latch.wait(1)
168175
[root, c1, c2, c2_1].each { |p| expect(p).to be_pending }
176+
end_latch.count_down
169177
end
170178
end
171179

172180
context 'when called on a child' do
173181
it 'should set all promises to :pending' do
174182
c2_1.execute
175-
183+
start_latch.wait(1)
176184
[root, c1, c2, c2_1].each { |p| expect(p).to be_pending }
185+
end_latch.count_down
177186
end
178187
end
179188
end
@@ -298,38 +307,38 @@ def get_ivar_from_args(opts)
298307
end
299308

300309
it 'succeeds if both promises succeed' do
301-
child = Promise.new(executor: executor) { 1 }.
302-
flat_map { |v| Promise.new(executor: executor) { v + 10 } }.execute.wait
310+
child = Promise.new(executor: :immediate) { 1 }.
311+
flat_map { |v| Promise.new(executor: :immediate) { v + 10 } }.execute.wait
303312

304313
expect(child.value!).to eq(11)
305314
end
306315

307316
it 'fails if the left promise fails' do
308-
child = Promise.new(executor: executor) { fail }.
309-
flat_map { |v| Promise.new(executor: executor) { v + 10 } }.execute.wait
317+
child = Promise.new(executor: :immediate) { fail }.
318+
flat_map { |v| Promise.new(executor: :immediate) { v + 10 } }.execute.wait
310319

311320
expect(child).to be_rejected
312321
end
313322

314323
it 'fails if the right promise fails' do
315-
child = Promise.new(executor: executor) { 1 }.
316-
flat_map { |v| Promise.new(executor: executor) { fail } }.execute.wait
324+
child = Promise.new(executor: :immediate) { 1 }.
325+
flat_map { |v| Promise.new(executor: :immediate) { fail } }.execute.wait
317326

318327
expect(child).to be_rejected
319328
end
320329

321330
it 'fails if the generating block fails' do
322-
child = Promise.new(executor: executor) { }.flat_map { fail }.execute.wait
331+
child = Promise.new(executor: :immediate) { }.flat_map { fail }.execute.wait
323332

324333
expect(child).to be_rejected
325334
end
326335

327336
end
328337

329338
describe '#zip' do
330-
let(:promise1) { Promise.new(executor: executor) { 1 } }
331-
let(:promise2) { Promise.new(executor: executor) { 2 } }
332-
let(:promise3) { Promise.new(executor: executor) { [3] } }
339+
let(:promise1) { Promise.new(executor: :immediate) { 1 } }
340+
let(:promise2) { Promise.new(executor: :immediate) { 2 } }
341+
let(:promise3) { Promise.new(executor: :immediate) { [3] } }
333342

334343
it 'yields the results as an array' do
335344
composite = promise1.zip(promise2, promise3).execute.wait
@@ -345,9 +354,9 @@ def get_ivar_from_args(opts)
345354
end
346355

347356
describe '.zip' do
348-
let(:promise1) { Promise.new(executor: executor) { 1 } }
349-
let(:promise2) { Promise.new(executor: executor) { 2 } }
350-
let(:promise3) { Promise.new(executor: executor) { [3] } }
357+
let(:promise1) { Promise.new(executor: :immediate) { 1 } }
358+
let(:promise2) { Promise.new(executor: :immediate) { 2 } }
359+
let(:promise3) { Promise.new(executor: :immediate) { [3] } }
351360

352361
it 'yields the results as an array' do
353362
composite = Promise.zip(promise1, promise2, promise3).execute.wait
@@ -364,9 +373,9 @@ def get_ivar_from_args(opts)
364373

365374
describe 'aggregators' do
366375

367-
let(:promise1) { Promise.new(executor: executor) { 1 } }
368-
let(:promise2) { Promise.new(executor: executor) { 2 } }
369-
let(:promise3) { Promise.new(executor: executor) { [3] } }
376+
let(:promise1) { Promise.new(executor: :immediate) { 1 } }
377+
let(:promise2) { Promise.new(executor: :immediate) { 2 } }
378+
let(:promise3) { Promise.new(executor: :immediate) { [3] } }
370379

371380
describe '.all?' do
372381

@@ -386,7 +395,7 @@ def get_ivar_from_args(opts)
386395

387396
composite = Promise.all?(promise1, promise2, promise3).
388397
then { counter.up; latch.count_down }.
389-
rescue { counter.down; latch.count_down }.
398+
rescue { counter.down; latch.count_down }.
390399
execute
391400

392401
latch.wait(1)
@@ -400,7 +409,7 @@ def get_ivar_from_args(opts)
400409

401410
composite = Promise.all?.
402411
then { counter.up; latch.count_down }.
403-
rescue { counter.down; latch.count_down }.
412+
rescue { counter.down; latch.count_down }.
404413
execute
405414

406415
latch.wait(1)
@@ -414,7 +423,7 @@ def get_ivar_from_args(opts)
414423

415424
composite = Promise.all?(promise1, promise2, rejected_subject, promise3).
416425
then { counter.up; latch.count_down }.
417-
rescue { counter.down; latch.count_down }.
426+
rescue { counter.down; latch.count_down }.
418427
execute
419428

420429
latch.wait(1)
@@ -441,7 +450,7 @@ def get_ivar_from_args(opts)
441450

442451
composite = Promise.any?(promise1, promise2, rejected_subject, promise3).
443452
then { counter.up; latch.count_down }.
444-
rescue { counter.down; latch.count_down }.
453+
rescue { counter.down; latch.count_down }.
445454
execute
446455

447456
latch.wait(1)
@@ -455,7 +464,7 @@ def get_ivar_from_args(opts)
455464

456465
composite = Promise.any?.
457466
then { counter.up; latch.count_down }.
458-
rescue { counter.down; latch.count_down }.
467+
rescue { counter.down; latch.count_down }.
459468
execute
460469

461470
latch.wait(1)
@@ -469,7 +478,7 @@ def get_ivar_from_args(opts)
469478

470479
composite = Promise.any?(rejected_subject, rejected_subject, rejected_subject, rejected_subject).
471480
then { counter.up; latch.count_down }.
472-
rescue { counter.down; latch.count_down }.
481+
rescue { counter.down; latch.count_down }.
473482
execute
474483

475484
latch.wait(1)
@@ -500,7 +509,7 @@ def get_ivar_from_args(opts)
500509
end
501510

502511
it 'can be called with a block' do
503-
p = Promise.new(executor: executor)
512+
p = Promise.new(executor: :immediate)
504513
ch = p.then(&:to_s)
505514
p.set { :value }
506515

@@ -533,46 +542,40 @@ def get_ivar_from_args(opts)
533542

534543
it 'passes the result of each block to all its children' do
535544
expected = nil
536-
Promise.new(executor: executor){ 20 }.then{ |result| expected = result }.execute
537-
sleep(0.1)
545+
Promise.new(executor: :immediate){ 20 }.then{ |result| expected = result }.execute
538546
expect(expected).to eq 20
539547
end
540548

541549
it 'sets the promise value to the result if its block' do
542-
root = Promise.new(executor: executor){ 20 }
550+
root = Promise.new(executor: :immediate){ 20 }
543551
p = root.then{ |result| result * 2}.execute
544-
sleep(0.1)
545552
expect(root.value).to eq 20
546553
expect(p.value).to eq 40
547554
end
548555

549556
it 'sets the promise state to :fulfilled if the block completes' do
550-
p = Promise.new(executor: executor){ 10 * 2 }.then{|result| result * 2}.execute
551-
sleep(0.1)
557+
p = Promise.new(executor: :immediate){ 10 * 2 }.then{|result| result * 2}.execute
552558
expect(p).to be_fulfilled
553559
end
554560

555561
it 'passes the last result through when a promise has no block' do
556562
expected = nil
557-
Promise.new(executor: executor){ 20 }.then(Proc.new{}).then{|result| expected = result}.execute
558-
sleep(0.1)
563+
Promise.new(executor: :immediate){ 20 }.then(Proc.new{}).then{|result| expected = result}.execute
559564
expect(expected).to eq 20
560565
end
561566

562567
it 'uses result as fulfillment value when a promise has no block' do
563-
p = Promise.new(executor: executor){ 20 }.then(Proc.new{}).execute
564-
sleep(0.1)
568+
p = Promise.new(executor: :immediate){ 20 }.then(Proc.new{}).execute
565569
expect(p.value).to eq 20
566570
end
567571

568572
it 'can manage long chain' do
569-
root = Promise.new(executor: executor){ 20 }
573+
root = Promise.new(executor: :immediate){ 20 }
570574
p1 = root.then { |b| b * 3 }
571575
p2 = root.then { |c| c + 2 }
572576
p3 = p1.then { |d| d + 7 }
573577

574578
root.execute
575-
sleep(0.1)
576579

577580
expect(root.value).to eq 20
578581
expect(p1.value).to eq 60
@@ -585,27 +588,24 @@ def get_ivar_from_args(opts)
585588

586589
it 'passes the reason to all its children' do
587590
expected = nil
588-
Promise.new(executor: executor){ raise ArgumentError }.then(Proc.new{ |reason| expected = reason }).execute
589-
sleep(0.1)
591+
handler = proc { |reason| expected = reason }
592+
Promise.new(executor: :immediate){ raise ArgumentError }.then(handler).execute
590593
expect(expected).to be_a ArgumentError
591594
end
592595

593596
it 'sets the promise value to the result if its block' do
594-
root = Promise.new(executor: executor){ raise ArgumentError }
597+
root = Promise.new(executor: :immediate){ raise ArgumentError }
595598
p = root.then(Proc.new{ |reason| 42 }).execute
596-
sleep(0.1)
597599
expect(p.value).to eq 42
598600
end
599601

600602
it 'sets the promise state to :rejected if the block completes' do
601-
p = Promise.new(executor: executor){ raise ArgumentError }.execute
602-
sleep(0.1)
603+
p = Promise.new(executor: :immediate){ raise ArgumentError }.execute
603604
expect(p).to be_rejected
604605
end
605606

606607
it 'uses reason as rejection reason when a promise has no rescue callable' do
607-
p = Promise.new(executor: ImmediateExecutor.new){ raise ArgumentError }.then{ |val| val }.execute
608-
sleep(0.1)
608+
p = Promise.new(executor: :immediate){ raise ArgumentError }.then{ |val| val }.execute
609609
expect(p).to be_rejected
610610
expect(p.reason).to be_a ArgumentError
611611
end

0 commit comments

Comments
 (0)