|
253 | 253 | end
|
254 | 254 |
|
255 | 255 | specify 'a #post task is never executed when the queue is at capacity' do
|
256 |
| - executed = Concurrent::AtomicFixnum.new(0) |
257 |
| - 1000.times do |
258 |
| - subject.post{ sleep; executed.increment } |
| 256 | + lock = Mutex.new |
| 257 | + lock.lock |
| 258 | + |
| 259 | + latch = Concurrent::CountDownLatch.new(max_threads) |
| 260 | + |
| 261 | + initial_executed = Concurrent::AtomicFixnum.new(0) |
| 262 | + subsequent_executed = Concurrent::AtomicFixnum.new(0) |
| 263 | + |
| 264 | + # Fill up all the threads (with a task that won't run until |
| 265 | + # lock.unlock is called) |
| 266 | + max_threads.times do |
| 267 | + subject.post{ latch.count_down; lock.lock; initial_executed.increment; lock.unlock } |
259 | 268 | end
|
260 |
| - sleep(0.1) |
261 |
| - expect(executed.value).to be 0 |
| 269 | + |
| 270 | + # Wait for all those tasks to be taken off the queue onto a |
| 271 | + # worker thread and start executing |
| 272 | + latch.wait |
| 273 | + |
| 274 | + # Fill up the queue (with a task that won't run until |
| 275 | + # lock.unlock is called) |
| 276 | + max_queue.times do |
| 277 | + subject.post{ lock.lock; initial_executed.increment; lock.unlock } |
| 278 | + end |
| 279 | + |
| 280 | + # Inject 100 more tasks, which should be dropped without an exception |
| 281 | + 100.times do |
| 282 | + subject.post{ subsequent_executed.increment; } |
| 283 | + end |
| 284 | + |
| 285 | + # Unlock the lock, so that the tasks in the threads and on |
| 286 | + # the queue can run to completion |
| 287 | + lock.unlock |
| 288 | + |
| 289 | + # Wait for all tasks to finish |
| 290 | + subject.shutdown |
| 291 | + subject.wait_for_termination |
| 292 | + |
| 293 | + # The tasks should have run until all the threads and the |
| 294 | + # queue filled up... |
| 295 | + expect(initial_executed.value).to be (max_threads + max_queue) |
| 296 | + |
| 297 | + # ..but been dropped after that |
| 298 | + expect(subsequent_executed.value).to be 0 |
262 | 299 | end
|
263 | 300 |
|
264 | 301 | specify 'a #<< task is never executed when the queue is at capacity' do
|
|
0 commit comments