Skip to content

Commit 8a1f8c6

Browse files
committed
Removed one check from reduce_permits
1 parent 509c60e commit 8a1f8c6

File tree

2 files changed

+5
-12
lines changed

2 files changed

+5
-12
lines changed

lib/concurrent/atomic/semaphore.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ def release(permits = 1)
109109
end
110110

111111
# @!macro [attach] semaphore_method_reduce_permits
112+
#
113+
# @api private
112114
#
113115
# Shrinks the number of available permits by the indicated reduction.
114116
#
@@ -123,10 +125,6 @@ def reduce_permits(reduction)
123125
unless reduction.is_a?(Fixnum) && reduction >= 0
124126
fail ArgumentError, 'reduction must be an non-negative integer'
125127
end
126-
unless @free - reduction >= 0
127-
fail(ArgumentError,
128-
'cannot reduce number of available_permits below zero')
129-
end
130128
@mutex.synchronize { @free -= reduction }
131129
nil
132130
end
@@ -211,10 +209,6 @@ def reduce_permits(reduction)
211209
unless reduction.is_a?(Fixnum) && reduction >= 0
212210
fail ArgumentError, 'reduction must be an non-negative integer'
213211
end
214-
unless @semaphore.availablePermits - reduction >= 0
215-
fail(ArgumentError,
216-
'cannot reduce number of available_permits below zero')
217-
end
218212
@semaphore.reducePermits(reduction)
219213
end
220214
end

spec/concurrent/atomic/semaphore_spec.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,9 @@
8686
}.to raise_error(ArgumentError)
8787
end
8888

89-
it 'raises ArgumentError when reducing below zero' do
90-
expect {
91-
semaphore.reduce_permits 1000
92-
}.to raise_error(ArgumentError)
89+
it 'reduces permits below zero' do
90+
semaphore.reduce_permits 1003
91+
expect(semaphore.available_permits).to eq -1000
9392
end
9493

9594
it 'reduces permits' do

0 commit comments

Comments
 (0)