@@ -361,7 +361,7 @@ def behaves_as_delay(delay, value)
361
361
branch1 . zip ( branch2 ) . then { |b1 , b2 | b1 + b2 } ,
362
362
( branch1 & branch2 ) . then { |b1 , b2 | b1 + b2 } ]
363
363
364
- sleep 0.1
364
+ Thread . pass until branch1 . resolved?
365
365
expect ( branch1 ) . to be_resolved
366
366
expect ( branch2 ) . not_to be_resolved
367
367
@@ -496,7 +496,7 @@ def behaves_as_delay(delay, value)
496
496
497
497
describe 'Throttling' do
498
498
specify do
499
- max_tree = Concurrent ::Throttle . new 3
499
+ max_tree = Concurrent ::Promises :: Throttle . new 3
500
500
counter = Concurrent ::AtomicFixnum . new
501
501
testing = -> *args do
502
502
counter . increment
@@ -508,14 +508,43 @@ def behaves_as_delay(delay, value)
508
508
expect ( Concurrent ::Promises . zip (
509
509
*12 . times . map do |i |
510
510
max_tree . limit { |trigger | trigger . then &testing }
511
- end ) . value . all? { |v | v < 3 } ) . to be_truthy
511
+ end ) . value! . all? { |v | v < 3 } ) . to be_truthy
512
512
513
513
expect ( Concurrent ::Promises . zip (
514
514
*12 . times . map do |i |
515
515
Concurrent ::Promises .
516
516
fulfilled_future ( i ) .
517
517
throttle ( max_tree ) { |trigger | trigger . then &testing }
518
- end ) . value . all? { |v | v < 3 } ) . to be_truthy
518
+ end ) . value! . all? { |v | v < 3 } ) . to be_truthy
519
+ end
520
+
521
+ specify do
522
+ max_five = Concurrent ::Promises ::Throttle . new 5
523
+ jobs = 20 . times . map do |i |
524
+ max_five . limit do |trigger |
525
+ # trigger is an event, has same chain-able capabilities as current promise
526
+ trigger . then do
527
+ # at any given time there max 5 simultaneous executions of this block
528
+ the_work = i * 2
529
+ end
530
+ end
531
+ end
532
+ result = Concurrent ::Promises . zip_futures ( *jobs )
533
+ p result . value!
534
+ # => [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38]
535
+ end
536
+
537
+ specify do
538
+ max_five = Concurrent ::Promises ::Throttle . new 5
539
+ jobs = 20 . times . map do |i |
540
+ max_five . then_limit do
541
+ # at any given time there max 5 simultaneous executions of this block
542
+ the_work = i * 2
543
+ end # returns promise
544
+ end
545
+ result = Concurrent ::Promises . zip_futures ( *jobs )
546
+ p result . value!
547
+ # => [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38]
519
548
end
520
549
end
521
550
end
0 commit comments