|
180 | 180 | end |
181 | 181 | end |
182 | 182 |
|
183 | | - context 'overflow policy' do |
| 183 | + context 'overflow policy', :brittle, :refactored do |
184 | 184 |
|
185 | 185 | before do |
186 | 186 | @queue = Queue.new |
187 | 187 | end |
188 | 188 |
|
189 | 189 | # On abort, it should raise an error |
190 | 190 | it "raises an error when overflow on abort" do |
| 191 | + latch = Concurrent::CountDownLatch.new(5) |
| 192 | + mutex = Mutex.new |
| 193 | + |
191 | 194 | subject = described_class.new(2, :max_queue => 2, :overflow_policy => :abort) |
192 | 195 | expect { |
193 | 196 | 5.times do |i| |
194 | | - subject.post { sleep 0.1; @queue << i} |
| 197 | + subject.post do |
| 198 | + sleep 0.1 |
| 199 | + mutex.synchronize{ @queue << i } |
| 200 | + latch.count_down |
| 201 | + end |
195 | 202 | end |
196 | 203 | subject.shutdown |
197 | | - subject.wait_for_termination(1) |
| 204 | + latch.wait(1) |
198 | 205 | }.to raise_error |
199 | 206 | end |
200 | 207 |
|
201 | 208 | # On discard, we'd expect no error, but also not all five results |
202 | 209 | it 'discards when overflow is :discard' do |
| 210 | + latch = Concurrent::CountDownLatch.new(5) |
| 211 | + mutex = Mutex.new |
| 212 | + |
203 | 213 | subject = described_class.new(2, :max_queue => 2, :overflow_policy => :discard) |
204 | 214 | 5.times do |i| |
205 | | - subject.post { sleep 0.1; @queue << i} |
| 215 | + subject.post do |
| 216 | + sleep 0.1 |
| 217 | + mutex.synchronize{ @queue << i } |
| 218 | + latch.count_down |
| 219 | + end |
206 | 220 | end |
207 | 221 | subject.shutdown |
208 | | - subject.wait_for_termination(1) |
| 222 | + latch.wait(1) |
| 223 | + |
209 | 224 | @queue.length.should be < 5 |
210 | 225 | end |
211 | 226 |
|
212 | 227 | # To check for caller_runs, we'll check how many unique threads |
213 | 228 | # actually ran the block |
214 | 229 |
|
215 | 230 | it 'uses the calling thread for overflow under caller_runs' do |
| 231 | + latch = Concurrent::CountDownLatch.new(5) |
| 232 | + mutex = Mutex.new |
| 233 | + |
216 | 234 | subject = described_class.new(2, :max_queue => 2, :overflow_policy => :caller_runs) |
| 235 | + |
217 | 236 | 5.times do |i| |
218 | | - subject.post { sleep 0.1; @queue << Thread.current} |
| 237 | + subject.post do |
| 238 | + sleep 0.1 |
| 239 | + mutex.synchronize{ @queue << Thread.current } |
| 240 | + latch.count_down |
| 241 | + end |
219 | 242 | end |
220 | 243 | subject.shutdown |
221 | | - subject.wait_for_termination(1) |
| 244 | + latch.wait(1) |
| 245 | + |
222 | 246 | # Turn the queue into an array |
223 | 247 | a = [] |
224 | 248 | a << @queue.shift until @queue.empty? |
|
227 | 251 | a.uniq.size.should eq 3 # one for each of teh two threads, plus the caller |
228 | 252 | end |
229 | 253 | end |
230 | | - |
231 | | - |
232 | 254 | end |
0 commit comments