Skip to content

Commit 40d13e5

Browse files
committed
Merge pull request #208 from rkday/queue_overflow_uts
Rewrite one :discard on queue overflow test for robustness
2 parents 5929b46 + 445146a commit 40d13e5

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

spec/concurrent/executor/thread_pool_executor_shared.rb

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,49 @@
253253
end
254254

255255
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 }
259268
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
262299
end
263300

264301
specify 'a #<< task is never executed when the queue is at capacity' do

0 commit comments

Comments
 (0)