File tree Expand file tree Collapse file tree 2 files changed +28
-11
lines changed Expand file tree Collapse file tree 2 files changed +28
-11
lines changed Original file line number Diff line number Diff line change @@ -2031,10 +2031,12 @@ def initialize(max)
2031
2031
@Queue = Queue . new
2032
2032
end
2033
2033
2034
- def limit ( ready = nil , &block )
2034
+ def limit ( future = nil , &block )
2035
2035
# TODO (pitr-ch 11-Jun-2016): triggers should allocate resources when they are to be required
2036
+ trigger = future ? future & get_event : get_event
2037
+
2036
2038
if block_given?
2037
- block . call ( get_event ) . on_resolution! { done }
2039
+ block . call ( trigger ) . on_resolution! { done }
2038
2040
else
2039
2041
get_event
2040
2042
end
@@ -2067,4 +2069,12 @@ def get_event
2067
2069
end
2068
2070
end
2069
2071
end
2072
+
2073
+ class Promises ::AbstractEventFuture < Synchronization ::Object
2074
+
2075
+ def throttle ( throttle , &throttled_future )
2076
+ throttle . limit ( self , &throttled_future )
2077
+ end
2078
+
2079
+ end
2070
2080
end
Original file line number Diff line number Diff line change @@ -496,19 +496,26 @@ def behaves_as_delay(delay, value)
496
496
497
497
describe 'Throttling' do
498
498
specify do
499
- throttle = Concurrent ::Throttle . new 3
499
+ max_tree = Concurrent ::Throttle . new 3
500
500
counter = Concurrent ::AtomicFixnum . new
501
+ testing = -> do
502
+ counter . increment
503
+ sleep 0.01
504
+ # returns less then 3 since it's throttled
505
+ counter . decrement
506
+ end
507
+
501
508
expect ( Concurrent ::Promises . zip (
502
509
*12 . times . map do |i |
503
- throttle . limit do |trigger |
504
- trigger . then do
505
- counter . increment
506
- sleep 0.01
507
- counter . decrement
508
- end
509
- end
510
+ max_tree . limit { |trigger | trigger . then &testing }
511
+ end ) . value . all? { |v | v < 3 } ) . to be_truthy
512
+
513
+ expect ( Concurrent ::Promises . zip (
514
+ *12 . times . map do |i |
515
+ Concurrent ::Promises .
516
+ fulfilled_future ( i ) .
517
+ throttle ( max_tree ) { |trigger | trigger . then &testing }
510
518
end ) . value . all? { |v | v < 3 } ) . to be_truthy
511
519
end
512
520
end
513
-
514
521
end
You can’t perform that action at this time.
0 commit comments