Skip to content

Commit 45106b4

Browse files
committed
Extend reworking of queue overflow tests to :abort tests
1 parent 7dd0e49 commit 45106b4

File tree

1 file changed

+88
-19
lines changed

1 file changed

+88
-19
lines changed

spec/concurrent/executor/thread_pool_executor_shared.rb

Lines changed: 88 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -216,30 +216,100 @@
216216
end
217217

218218
specify 'a #post task is never executed when the queue is at capacity' do
219-
executed = Concurrent::AtomicFixnum.new(0)
220-
10.times do
221-
begin
222-
subject.post{ executed.increment; sleep(0.1) }
223-
rescue
224-
end
219+
all_tasks_posted = Concurrent::Event.new
220+
221+
latch = Concurrent::CountDownLatch.new(max_threads)
222+
223+
initial_executed = Concurrent::AtomicFixnum.new(0)
224+
subsequent_executed = Concurrent::AtomicFixnum.new(0)
225+
226+
# Fill up all the threads (with a task that won't complete until
227+
# all tasks are posted)
228+
max_threads.times do
229+
subject.post{ latch.count_down; all_tasks_posted.wait ; initial_executed.increment;}
225230
end
226-
sleep(0.2)
227-
expect(executed.value).to be < 10
231+
232+
# Wait for all those tasks to be taken off the queue onto a
233+
# worker thread and start executing
234+
latch.wait
235+
236+
# Fill up the queue (with a task that won't complete until
237+
# all tasks are posted)
238+
max_queue.times do
239+
subject.post{ all_tasks_posted.wait; initial_executed.increment; }
240+
end
241+
242+
# Inject 100 more tasks, which should throw an exception
243+
100.times do
244+
expect {
245+
subject.post { subsequent_executed.increment; }
246+
}.to raise_error(Concurrent::RejectedExecutionError)
247+
end
248+
249+
# Trigger the event, so that the tasks in the threads and on
250+
# the queue can run to completion
251+
all_tasks_posted.set
252+
253+
# Wait for all tasks to finish
254+
subject.shutdown
255+
subject.wait_for_termination
256+
257+
# The tasks should have run until all the threads and the
258+
# queue filled up...
259+
expect(initial_executed.value).to be (max_threads + max_queue)
260+
261+
# ..but been dropped after that
262+
expect(subsequent_executed.value).to be 0
228263
end
229264

230265
specify 'a #<< task is never executed when the queue is at capacity' do
231-
executed = Concurrent::AtomicFixnum.new(0)
232-
10.times do
233-
begin
234-
subject << proc { executed.increment; sleep(0.1) }
235-
rescue
236-
end
266+
all_tasks_posted = Concurrent::Event.new
267+
268+
latch = Concurrent::CountDownLatch.new(max_threads)
269+
270+
initial_executed = Concurrent::AtomicFixnum.new(0)
271+
subsequent_executed = Concurrent::AtomicFixnum.new(0)
272+
273+
# Fill up all the threads (with a task that won't complete until
274+
# all tasks are posted)
275+
max_threads.times do
276+
subject << proc { latch.count_down; all_tasks_posted.wait ; initial_executed.increment;}
277+
end
278+
279+
# Wait for all those tasks to be taken off the queue onto a
280+
# worker thread and start executing
281+
latch.wait
282+
283+
# Fill up the queue (with a task that won't complete until
284+
# all tasks are posted)
285+
max_queue.times do
286+
subject << proc { all_tasks_posted.wait; initial_executed.increment; }
237287
end
238-
sleep(0.2)
239-
expect(executed.value).to be < 10
288+
289+
# Inject 100 more tasks, which should throw an exeption
290+
100.times do
291+
expect {
292+
subject << proc { subsequent_executed.increment; }
293+
}.to raise_error(Concurrent::RejectedExecutionError)
294+
end
295+
296+
# Trigger the event, so that the tasks in the threads and on
297+
# the queue can run to completion
298+
all_tasks_posted.set
299+
300+
# Wait for all tasks to finish
301+
subject.shutdown
302+
subject.wait_for_termination
303+
304+
# The tasks should have run until all the threads and the
305+
# queue filled up...
306+
expect(initial_executed.value).to be (max_threads + max_queue)
307+
308+
# ..but been rejected after that
309+
expect(subsequent_executed.value).to be 0
240310
end
241311
end
242-
312+
243313
context ':discard' do
244314

245315
subject do
@@ -367,9 +437,8 @@
367437
end
368438

369439
specify '#post returns false when the executor is shutting down' do
370-
executed = Concurrent::AtomicFixnum.new(0)
371440
subject.shutdown
372-
ret = subject.post{ executed.increment }
441+
ret = subject.post{ nil }
373442
expect(ret).to be false
374443
end
375444
end

0 commit comments

Comments
 (0)