@@ -374,41 +374,41 @@ def get_ivar_from_args(opts)
374
374
375
375
composite = Promise . all? ( promise1 , promise2 , promise3 ) .
376
376
then { counter . up ; latch . count_down } .
377
- rescue { counter . down ; latch . count_down } .
378
- execute
377
+ rescue { counter . down ; latch . count_down } .
378
+ execute
379
379
380
380
latch . wait ( 1 )
381
381
382
382
expect ( counter . value ) . to eq 1
383
- end
383
+ end
384
384
385
385
it 'executes the #then condition when no promises are given' do
386
386
counter = Concurrent ::AtomicFixnum . new ( 0 )
387
387
latch = Concurrent ::CountDownLatch . new ( 1 )
388
388
389
389
composite = Promise . all? .
390
390
then { counter . up ; latch . count_down } .
391
- rescue { counter . down ; latch . count_down } .
392
- execute
391
+ rescue { counter . down ; latch . count_down } .
392
+ execute
393
393
394
394
latch . wait ( 1 )
395
395
396
396
expect ( counter . value ) . to eq 1
397
- end
397
+ end
398
398
399
399
it 'executes the #rescue handler if even one component fails' do
400
400
counter = Concurrent ::AtomicFixnum . new ( 0 )
401
401
latch = Concurrent ::CountDownLatch . new ( 1 )
402
402
403
403
composite = Promise . all? ( promise1 , promise2 , rejected_subject , promise3 ) .
404
404
then { counter . up ; latch . count_down } .
405
- rescue { counter . down ; latch . count_down } .
406
- execute
405
+ rescue { counter . down ; latch . count_down } .
406
+ execute
407
407
408
408
latch . wait ( 1 )
409
409
410
410
expect ( counter . value ) . to eq -1
411
- end
411
+ end
412
412
end
413
413
414
414
describe '.any?' do
@@ -429,46 +429,84 @@ def get_ivar_from_args(opts)
429
429
430
430
composite = Promise . any? ( promise1 , promise2 , rejected_subject , promise3 ) .
431
431
then { counter . up ; latch . count_down } .
432
- rescue { counter . down ; latch . count_down } .
433
- execute
432
+ rescue { counter . down ; latch . count_down } .
433
+ execute
434
434
435
435
latch . wait ( 1 )
436
436
437
437
expect ( counter . value ) . to eq 1
438
- end
438
+ end
439
439
440
440
it 'executes the #then condition when no promises are given' do
441
441
counter = Concurrent ::AtomicFixnum . new ( 0 )
442
442
latch = Concurrent ::CountDownLatch . new ( 1 )
443
443
444
444
composite = Promise . any? .
445
445
then { counter . up ; latch . count_down } .
446
- rescue { counter . down ; latch . count_down } .
447
- execute
446
+ rescue { counter . down ; latch . count_down } .
447
+ execute
448
448
449
449
latch . wait ( 1 )
450
450
451
451
expect ( counter . value ) . to eq 1
452
- end
452
+ end
453
453
454
454
it 'executes the #rescue handler if all componenst fail' do
455
455
counter = Concurrent ::AtomicFixnum . new ( 0 )
456
456
latch = Concurrent ::CountDownLatch . new ( 1 )
457
457
458
458
composite = Promise . any? ( rejected_subject , rejected_subject , rejected_subject , rejected_subject ) .
459
459
then { counter . up ; latch . count_down } .
460
- rescue { counter . down ; latch . count_down } .
461
- execute
460
+ rescue { counter . down ; latch . count_down } .
461
+ execute
462
462
463
463
latch . wait ( 1 )
464
464
465
465
expect ( counter . value ) . to eq -1
466
- end
466
+ end
467
467
end
468
468
end
469
469
470
470
context 'fulfillment' do
471
471
472
+ context '#set' do
473
+
474
+ it '#can only be called on the root promise' do
475
+ root = Promise . new { :foo }
476
+ child = root . then { :bar }
477
+
478
+ expect { child . set ( 'foo' ) } . to raise_error PromiseExecutionError
479
+ expect { root . set ( 'foo' ) } . not_to raise_error
480
+ end
481
+
482
+ it 'triggers children' do
483
+ expected = nil
484
+ root = Promise . new ( executor : :immediate ) { nil }
485
+ root . then { |result | expected = result }
486
+ root . set ( 20 )
487
+ expect ( expected ) . to eq 20
488
+ end
489
+ end
490
+
491
+ context '#fail' do
492
+
493
+ it 'can only be called on the root promise' do
494
+ root = Promise . new { :foo }
495
+ child = root . then { :bar }
496
+
497
+ expect { child . fail } . to raise_error PromiseExecutionError
498
+ expect { root . fail } . not_to raise_error
499
+ end
500
+
501
+ it 'rejects children' do
502
+ expected = nil
503
+ root = Promise . new ( executor : :immediate )
504
+ root . then ( Proc . new { |reason | expected = reason } )
505
+ root . fail ( ArgumentError . new ( 'simulated error' ) )
506
+ expect ( expected ) . to be_a ArgumentError
507
+ end
508
+ end
509
+
472
510
it 'passes the result of each block to all its children' do
473
511
expected = nil
474
512
Promise . new ( executor : executor ) { 20 } . then { |result | expected = result } . execute
0 commit comments