2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2010-04-16 07:10:08 -04:00
|
|
|
require 'test/unit'
|
|
|
|
require 'thread'
|
2011-12-22 20:15:35 -05:00
|
|
|
require 'tmpdir'
|
2013-09-06 11:15:07 -04:00
|
|
|
require 'timeout'
|
2010-04-16 07:10:08 -04:00
|
|
|
|
|
|
|
class TestQueue < Test::Unit::TestCase
|
2016-08-30 02:22:30 -04:00
|
|
|
Queue = Thread::Queue
|
|
|
|
SizedQueue = Thread::SizedQueue
|
|
|
|
|
2014-08-18 21:13:24 -04:00
|
|
|
def test_queue_initialized
|
|
|
|
assert_raise(TypeError) {
|
|
|
|
Queue.allocate.push(nil)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sized_queue_initialized
|
|
|
|
assert_raise(TypeError) {
|
|
|
|
SizedQueue.allocate.push(nil)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-04-16 07:10:08 -04:00
|
|
|
def test_queue
|
|
|
|
grind(5, 1000, 15, Queue)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sized_queue
|
|
|
|
grind(5, 1000, 15, SizedQueue, 1000)
|
|
|
|
end
|
|
|
|
|
|
|
|
def grind(num_threads, num_objects, num_iterations, klass, *args)
|
|
|
|
from_workers = klass.new(*args)
|
|
|
|
to_workers = klass.new(*args)
|
|
|
|
|
|
|
|
workers = (1..num_threads).map {
|
|
|
|
Thread.new {
|
|
|
|
while object = to_workers.pop
|
|
|
|
from_workers.push object
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread.new {
|
|
|
|
num_iterations.times {
|
|
|
|
num_objects.times { to_workers.push 99 }
|
|
|
|
num_objects.times { from_workers.pop }
|
|
|
|
}
|
|
|
|
}.join
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2015-08-26 18:59:32 -04:00
|
|
|
# close the queue the old way to test for backwards-compatibility
|
2010-04-16 07:10:08 -04:00
|
|
|
num_threads.times { to_workers.push nil }
|
|
|
|
workers.each { |t| t.join }
|
|
|
|
|
2010-04-16 18:45:45 -04:00
|
|
|
assert_equal 0, from_workers.size
|
|
|
|
assert_equal 0, to_workers.size
|
2010-04-16 07:10:08 -04:00
|
|
|
end
|
2011-09-03 05:46:51 -04:00
|
|
|
|
|
|
|
def test_sized_queue_initialize
|
|
|
|
q = SizedQueue.new(1)
|
|
|
|
assert_equal 1, q.max
|
|
|
|
assert_raise(ArgumentError) { SizedQueue.new(0) }
|
|
|
|
assert_raise(ArgumentError) { SizedQueue.new(-1) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sized_queue_assign_max
|
|
|
|
q = SizedQueue.new(2)
|
|
|
|
assert_equal(2, q.max)
|
|
|
|
q.max = 1
|
|
|
|
assert_equal(1, q.max)
|
|
|
|
assert_raise(ArgumentError) { q.max = 0 }
|
|
|
|
assert_equal(1, q.max)
|
|
|
|
assert_raise(ArgumentError) { q.max = -1 }
|
|
|
|
assert_equal(1, q.max)
|
2014-02-05 18:35:06 -05:00
|
|
|
|
|
|
|
before = q.max
|
|
|
|
q.max.times { q << 1 }
|
|
|
|
t1 = Thread.new { q << 1 }
|
|
|
|
sleep 0.01 until t1.stop?
|
|
|
|
q.max = q.max + 1
|
|
|
|
assert_equal before + 1, q.max
|
2014-05-26 01:01:33 -04:00
|
|
|
ensure
|
|
|
|
t1.join if t1
|
2011-09-03 05:46:51 -04:00
|
|
|
end
|
2011-12-13 19:23:43 -05:00
|
|
|
|
2012-09-09 08:32:33 -04:00
|
|
|
def test_queue_pop_interrupt
|
|
|
|
q = Queue.new
|
|
|
|
t1 = Thread.new { q.pop }
|
|
|
|
sleep 0.01 until t1.stop?
|
|
|
|
t1.kill.join
|
|
|
|
assert_equal(0, q.num_waiting)
|
|
|
|
end
|
|
|
|
|
2014-07-17 06:56:53 -04:00
|
|
|
def test_queue_pop_non_block
|
|
|
|
q = Queue.new
|
|
|
|
assert_raise_with_message(ThreadError, /empty/) do
|
|
|
|
q.pop(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-09 08:32:33 -04:00
|
|
|
def test_sized_queue_pop_interrupt
|
|
|
|
q = SizedQueue.new(1)
|
|
|
|
t1 = Thread.new { q.pop }
|
|
|
|
sleep 0.01 until t1.stop?
|
|
|
|
t1.kill.join
|
|
|
|
assert_equal(0, q.num_waiting)
|
|
|
|
end
|
|
|
|
|
2014-07-17 06:56:53 -04:00
|
|
|
def test_sized_queue_pop_non_block
|
|
|
|
q = SizedQueue.new(1)
|
|
|
|
assert_raise_with_message(ThreadError, /empty/) do
|
|
|
|
q.pop(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-09 08:32:33 -04:00
|
|
|
def test_sized_queue_push_interrupt
|
2014-07-17 06:57:13 -04:00
|
|
|
q = SizedQueue.new(1)
|
|
|
|
q.push(1)
|
|
|
|
assert_raise_with_message(ThreadError, /full/) do
|
|
|
|
q.push(2, true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sized_queue_push_non_block
|
2012-09-09 08:32:33 -04:00
|
|
|
q = SizedQueue.new(1)
|
|
|
|
q.push(1)
|
|
|
|
t1 = Thread.new { q.push(2) }
|
|
|
|
sleep 0.01 until t1.stop?
|
|
|
|
t1.kill.join
|
|
|
|
assert_equal(0, q.num_waiting)
|
|
|
|
end
|
|
|
|
|
2011-12-13 19:23:43 -05:00
|
|
|
def test_thr_kill
|
|
|
|
bug5343 = '[ruby-core:39634]'
|
2011-12-22 20:15:35 -05:00
|
|
|
Dir.mktmpdir {|d|
|
2016-11-09 21:23:58 -05:00
|
|
|
timeout = 60
|
2013-03-09 23:00:44 -05:00
|
|
|
total_count = 250
|
2011-12-22 20:15:35 -05:00
|
|
|
begin
|
|
|
|
assert_normal_exit(<<-"_eom", bug5343, {:timeout => timeout, :chdir=>d})
|
|
|
|
require "thread"
|
2011-12-23 02:55:39 -05:00
|
|
|
#{total_count}.times do |i|
|
2011-12-22 20:15:35 -05:00
|
|
|
open("test_thr_kill_count", "w") {|f| f.puts i }
|
|
|
|
queue = Queue.new
|
|
|
|
r, w = IO.pipe
|
|
|
|
th = Thread.start {
|
|
|
|
queue.push(nil)
|
|
|
|
r.read 1
|
|
|
|
}
|
|
|
|
queue.pop
|
2013-03-09 23:00:44 -05:00
|
|
|
th.kill
|
|
|
|
th.join
|
2011-12-22 20:15:35 -05:00
|
|
|
end
|
|
|
|
_eom
|
|
|
|
rescue Timeout::Error
|
|
|
|
count = File.read("#{d}/test_thr_kill_count").to_i
|
2011-12-23 02:55:39 -05:00
|
|
|
flunk "only #{count}/#{total_count} done in #{timeout} seconds."
|
2011-12-13 21:26:17 -05:00
|
|
|
end
|
2011-12-22 20:15:35 -05:00
|
|
|
}
|
2011-12-13 19:23:43 -05:00
|
|
|
end
|
2013-03-10 19:01:21 -04:00
|
|
|
|
|
|
|
def test_queue_push_return_value
|
|
|
|
q = Queue.new
|
|
|
|
retval = q.push(1)
|
|
|
|
assert_same q, retval
|
2013-03-10 20:29:00 -04:00
|
|
|
end
|
2013-03-10 19:01:21 -04:00
|
|
|
|
|
|
|
def test_queue_clear_return_value
|
|
|
|
q = Queue.new
|
|
|
|
retval = q.clear
|
|
|
|
assert_same q, retval
|
2014-01-14 07:14:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sized_queue_clear
|
|
|
|
# Fill queue, then test that SizedQueue#clear wakes up all waiting threads
|
|
|
|
sq = SizedQueue.new(2)
|
|
|
|
2.times { sq << 1 }
|
|
|
|
|
|
|
|
t1 = Thread.new do
|
|
|
|
sq << 1
|
|
|
|
end
|
|
|
|
|
|
|
|
t2 = Thread.new do
|
|
|
|
sq << 1
|
|
|
|
end
|
|
|
|
|
|
|
|
t3 = Thread.new do
|
|
|
|
Thread.pass
|
|
|
|
sq.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
[t3, t2, t1].each(&:join)
|
|
|
|
assert_equal sq.length, 2
|
2013-03-10 19:01:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sized_queue_push_return_value
|
|
|
|
q = SizedQueue.new(1)
|
|
|
|
retval = q.push(1)
|
|
|
|
assert_same q, retval
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sized_queue_clear_return_value
|
|
|
|
q = SizedQueue.new(1)
|
|
|
|
retval = q.clear
|
|
|
|
assert_same q, retval
|
|
|
|
end
|
|
|
|
|
2014-01-12 22:18:38 -05:00
|
|
|
def test_sized_queue_throttle
|
|
|
|
q = SizedQueue.new(1)
|
|
|
|
i = 0
|
|
|
|
consumer = Thread.new do
|
|
|
|
while q.pop
|
|
|
|
i += 1
|
|
|
|
Thread.pass
|
|
|
|
end
|
|
|
|
end
|
|
|
|
nprod = 4
|
|
|
|
npush = 100
|
|
|
|
|
|
|
|
producer = nprod.times.map do
|
|
|
|
Thread.new do
|
|
|
|
npush.times { q.push(true) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
producer.each(&:join)
|
|
|
|
q.push(nil)
|
|
|
|
consumer.join
|
|
|
|
assert_equal(nprod * npush, i)
|
|
|
|
end
|
|
|
|
|
2013-09-06 11:15:07 -04:00
|
|
|
def test_queue_thread_raise
|
|
|
|
q = Queue.new
|
|
|
|
th1 = Thread.new do
|
|
|
|
begin
|
|
|
|
q.pop
|
|
|
|
rescue RuntimeError
|
|
|
|
sleep
|
|
|
|
end
|
|
|
|
end
|
|
|
|
th2 = Thread.new do
|
|
|
|
sleep 0.1
|
|
|
|
q.pop
|
|
|
|
end
|
|
|
|
sleep 0.1
|
|
|
|
th1.raise
|
|
|
|
sleep 0.1
|
|
|
|
q << :s
|
2014-10-07 16:00:09 -04:00
|
|
|
assert_nothing_raised(Timeout::Error) do
|
2015-07-13 06:07:01 -04:00
|
|
|
Timeout.timeout(1) { th2.join }
|
2013-09-06 11:15:07 -04:00
|
|
|
end
|
2014-05-26 01:01:33 -04:00
|
|
|
ensure
|
|
|
|
[th1, th2].each do |th|
|
|
|
|
if th and th.alive?
|
|
|
|
th.wakeup
|
|
|
|
th.join
|
|
|
|
end
|
|
|
|
end
|
2013-09-06 11:15:07 -04:00
|
|
|
end
|
2014-01-22 01:59:41 -05:00
|
|
|
|
|
|
|
def test_dup
|
|
|
|
bug9440 = '[ruby-core:59961] [Bug #9440]'
|
|
|
|
q = Queue.new
|
|
|
|
assert_raise(NoMethodError, bug9440) do
|
|
|
|
q.dup
|
|
|
|
end
|
|
|
|
end
|
2014-03-25 22:20:26 -04:00
|
|
|
|
2014-03-25 22:20:52 -04:00
|
|
|
(DumpableQueue = Queue.dup).class_eval {remove_method :marshal_dump}
|
|
|
|
|
2014-03-25 22:20:26 -04:00
|
|
|
def test_dump
|
|
|
|
bug9674 = '[ruby-core:61677] [Bug #9674]'
|
|
|
|
q = Queue.new
|
|
|
|
assert_raise_with_message(TypeError, /#{Queue}/, bug9674) do
|
|
|
|
Marshal.dump(q)
|
|
|
|
end
|
|
|
|
|
|
|
|
sq = SizedQueue.new(1)
|
|
|
|
assert_raise_with_message(TypeError, /#{SizedQueue}/, bug9674) do
|
|
|
|
Marshal.dump(sq)
|
|
|
|
end
|
2014-03-25 22:20:52 -04:00
|
|
|
|
|
|
|
q = DumpableQueue.new
|
2017-05-09 15:52:10 -04:00
|
|
|
assert_raise(TypeError, bug9674) do
|
2014-03-25 22:20:52 -04:00
|
|
|
Marshal.dump(q)
|
|
|
|
end
|
2014-03-25 22:20:26 -04:00
|
|
|
end
|
2015-08-26 18:59:32 -04:00
|
|
|
|
|
|
|
def test_close
|
|
|
|
[->{Queue.new}, ->{SizedQueue.new 3}].each do |qcreate|
|
|
|
|
q = qcreate.call
|
|
|
|
assert_equal false, q.closed?
|
|
|
|
q << :something
|
|
|
|
assert_equal q, q.close
|
|
|
|
assert q.closed?
|
|
|
|
assert_raise_with_message(ClosedQueueError, /closed/){q << :nothing}
|
|
|
|
assert_equal q.pop, :something
|
|
|
|
assert_nil q.pop
|
2015-11-20 19:32:09 -05:00
|
|
|
assert_nil q.pop
|
2015-08-26 18:59:32 -04:00
|
|
|
# non-blocking
|
|
|
|
assert_raise_with_message(ThreadError, /queue empty/){q.pop(non_block=true)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# test that waiting producers are woken up on close
|
|
|
|
def close_wakeup( num_items, num_threads, &qcreate )
|
|
|
|
raise "This test won't work with num_items(#{num_items}) >= num_threads(#{num_threads})" if num_items >= num_threads
|
|
|
|
|
|
|
|
# create the Queue
|
|
|
|
q = yield
|
|
|
|
threads = num_threads.times.map{Thread.new{q.pop}}
|
|
|
|
num_items.times{|i| q << i}
|
|
|
|
|
|
|
|
# wait until queue empty
|
|
|
|
(Thread.pass; sleep 0.01) until q.size == 0
|
|
|
|
|
2015-12-15 17:58:25 -05:00
|
|
|
# close the queue so remaining threads will wake up
|
2015-08-26 18:59:32 -04:00
|
|
|
q.close
|
|
|
|
|
|
|
|
# wait for them to go away
|
|
|
|
Thread.pass until threads.all?{|thr| thr.status == false}
|
|
|
|
|
|
|
|
# check that they've gone away. Convert nil to -1 so we can sort and do the comparison
|
|
|
|
expected_values = [-1] * (num_threads - num_items) + num_items.times.to_a
|
|
|
|
assert_equal expected_values, threads.map{|thr| thr.value || -1 }.sort
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_queue_close_wakeup
|
|
|
|
close_wakeup(15, 18){Queue.new}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_size_queue_close_wakeup
|
|
|
|
close_wakeup(5, 8){SizedQueue.new 9}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sized_queue_one_closed_interrupt
|
|
|
|
q = SizedQueue.new 1
|
|
|
|
q << :one
|
|
|
|
t1 = Thread.new { q << :two }
|
|
|
|
sleep 0.01 until t1.stop?
|
|
|
|
q.close
|
|
|
|
|
|
|
|
t1.kill.join
|
|
|
|
assert_equal 1, q.size
|
|
|
|
assert_equal :one, q.pop
|
|
|
|
assert q.empty?, "queue not empty"
|
|
|
|
end
|
|
|
|
|
|
|
|
# make sure that shutdown state is handled properly by empty? for the non-blocking case
|
|
|
|
def test_empty_non_blocking
|
|
|
|
return
|
|
|
|
q = SizedQueue.new 3
|
|
|
|
3.times{|i| q << i}
|
|
|
|
|
|
|
|
# these all block cos the queue is full
|
|
|
|
prod_threads = 4.times.map{|i| Thread.new{q << 3+i}}
|
|
|
|
sleep 0.01 until prod_threads.all?{|thr| thr.status == 'sleep'}
|
|
|
|
q.close
|
|
|
|
|
|
|
|
items = []
|
|
|
|
# sometimes empty? is false but pop will raise ThreadError('empty'),
|
|
|
|
# meaning a value is not immediately available but will be soon.
|
|
|
|
until q.empty?
|
|
|
|
items << q.pop(non_block=true) rescue nil
|
|
|
|
end
|
|
|
|
items.compact!
|
|
|
|
|
|
|
|
assert_equal 7.times.to_a, items.sort
|
|
|
|
assert q.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sized_queue_closed_push_non_blocking
|
|
|
|
q = SizedQueue.new 7
|
|
|
|
q.close
|
|
|
|
assert_raise_with_message(ClosedQueueError, /queue closed/){q.push(non_block=true)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_blocked_pushers
|
|
|
|
q = SizedQueue.new 3
|
|
|
|
prod_threads = 6.times.map do |i|
|
|
|
|
thr = Thread.new{q << i}; thr[:pc] = i; thr
|
|
|
|
end
|
|
|
|
|
|
|
|
# wait until some producer threads have finished, and the other 3 are blocked
|
|
|
|
sleep 0.01 while prod_threads.reject{|t| t.status}.count < 3
|
|
|
|
# this would ensure that all producer threads call push before close
|
|
|
|
# sleep 0.01 while prod_threads.select{|t| t.status == 'sleep'}.count < 3
|
|
|
|
q.close
|
|
|
|
|
|
|
|
# more than prod_threads
|
|
|
|
cons_threads = 10.times.map do |i|
|
|
|
|
thr = Thread.new{q.pop}; thr[:pc] = i; thr
|
|
|
|
end
|
|
|
|
|
|
|
|
# values that came from the queue
|
|
|
|
popped_values = cons_threads.map &:value
|
|
|
|
|
|
|
|
# wait untl all threads have finished
|
|
|
|
sleep 0.01 until prod_threads.find_all{|t| t.status}.count == 0
|
|
|
|
|
|
|
|
# pick only the producer threads that got in before close
|
|
|
|
successful_prod_threads = prod_threads.reject{|thr| thr.status == nil}
|
|
|
|
assert_nothing_raised{ successful_prod_threads.map(&:value) }
|
|
|
|
|
|
|
|
# the producer threads that tried to push after q.close should all fail
|
|
|
|
unsuccessful_prod_threads = prod_threads - successful_prod_threads
|
|
|
|
unsuccessful_prod_threads.each do |thr|
|
|
|
|
assert_raise(ClosedQueueError){ thr.value }
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal cons_threads.size, popped_values.size
|
|
|
|
assert_equal 0, q.size
|
|
|
|
|
|
|
|
# check that consumer threads with values match producers that called push before close
|
|
|
|
assert_equal successful_prod_threads.map{|thr| thr[:pc]}, popped_values.compact.sort
|
|
|
|
assert_nil q.pop
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_deny_pushers
|
|
|
|
[->{Queue.new}, ->{SizedQueue.new 3}].each do |qcreate|
|
|
|
|
prod_threads = nil
|
|
|
|
q = qcreate[]
|
|
|
|
synq = Queue.new
|
|
|
|
producers_start = Thread.new do
|
|
|
|
prod_threads = 20.times.map do |i|
|
|
|
|
Thread.new{ synq.pop; q << i }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
q.close
|
|
|
|
synq.close # start producer threads
|
|
|
|
|
|
|
|
# wait for all threads to be finished, because of exceptions
|
|
|
|
# NOTE: thr.status will be nil (raised) or false (terminated)
|
2015-12-14 03:04:28 -05:00
|
|
|
sleep 0.01 until prod_threads&.all?{|thr| !thr.status}
|
2015-08-26 18:59:32 -04:00
|
|
|
|
|
|
|
# check that all threads failed to call push
|
|
|
|
prod_threads.each do |thr|
|
|
|
|
assert_kind_of ClosedQueueError, (thr.value rescue $!)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# size should account for waiting pushers during shutdown
|
|
|
|
def sized_queue_size_close
|
|
|
|
q = SizedQueue.new 4
|
|
|
|
4.times{|i| q << i}
|
|
|
|
Thread.new{ q << 5 }
|
|
|
|
Thread.new{ q << 6 }
|
|
|
|
assert_equal 4, q.size
|
|
|
|
assert_equal 4, q.items
|
|
|
|
q.close
|
|
|
|
assert_equal 6, q.size
|
|
|
|
assert_equal 4, q.items
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_blocked_pushers_empty
|
|
|
|
q = SizedQueue.new 3
|
|
|
|
prod_threads = 6.times.map do |i|
|
|
|
|
Thread.new{ q << i}
|
|
|
|
end
|
|
|
|
|
|
|
|
# this ensures that all producer threads call push before close
|
|
|
|
sleep 0.01 while prod_threads.select{|t| t.status == 'sleep'}.count < 3
|
|
|
|
q.close
|
|
|
|
|
|
|
|
ary = []
|
|
|
|
until q.empty?
|
|
|
|
ary << q.pop
|
|
|
|
end
|
|
|
|
assert_equal 0, q.size
|
|
|
|
|
|
|
|
assert_equal 3, ary.size
|
|
|
|
ary.each{|e| assert [0,1,2,3,4,5].include?(e)}
|
|
|
|
assert_nil q.pop
|
|
|
|
|
|
|
|
prod_threads.each{|t|
|
|
|
|
begin
|
|
|
|
t.join
|
|
|
|
rescue => e
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# test thread wakeup on one-element SizedQueue with close
|
|
|
|
def test_one_element_sized_queue
|
|
|
|
q = SizedQueue.new 1
|
|
|
|
t = Thread.new{ q.pop }
|
|
|
|
q.close
|
|
|
|
assert_nil t.value
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_close_twice
|
|
|
|
[->{Queue.new}, ->{SizedQueue.new 3}].each do |qcreate|
|
|
|
|
q = qcreate[]
|
|
|
|
q.close
|
2015-09-01 05:37:33 -04:00
|
|
|
assert_nothing_raised(ClosedQueueError){q.close}
|
2015-08-26 18:59:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_queue_close_multi_multi
|
|
|
|
q = SizedQueue.new rand(800..1200)
|
|
|
|
|
|
|
|
count_items = rand(3000..5000)
|
|
|
|
count_producers = rand(10..20)
|
|
|
|
|
|
|
|
producers = count_producers.times.map do
|
|
|
|
Thread.new do
|
|
|
|
sleep(rand / 100)
|
|
|
|
count_items.times{|i| q << [i,"#{i} for #{Thread.current.inspect}"]}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
consumers = rand(7..12).times.map do
|
|
|
|
Thread.new do
|
|
|
|
count = 0
|
2015-11-20 19:32:09 -05:00
|
|
|
while e = q.pop
|
|
|
|
i, st = e
|
Use Integer instead of Fixnum and Bignum.
* object.c, numeric.c, enum.c, ext/-test-/bignum/mul.c,
lib/rexml/quickpath.rb, lib/rexml/text.rb, lib/rexml/xpath_parser.rb,
lib/rubygems/specification.rb, lib/uri/generic.rb,
bootstraptest/test_eval.rb, basictest/test.rb,
test/-ext-/bignum/test_big2str.rb, test/-ext-/bignum/test_div.rb,
test/-ext-/bignum/test_mul.rb, test/-ext-/bignum/test_str2big.rb,
test/csv/test_data_converters.rb, test/date/test_date.rb,
test/json/test_json_generate.rb, test/minitest/test_minitest_mock.rb,
test/openssl/test_cipher.rb, test/rexml/test_jaxen.rb,
test/ruby/test_array.rb, test/ruby/test_basicinstructions.rb,
test/ruby/test_bignum.rb, test/ruby/test_case.rb,
test/ruby/test_class.rb, test/ruby/test_complex.rb,
test/ruby/test_enum.rb, test/ruby/test_eval.rb,
test/ruby/test_iseq.rb, test/ruby/test_literal.rb,
test/ruby/test_math.rb, test/ruby/test_module.rb,
test/ruby/test_numeric.rb, test/ruby/test_range.rb,
test/ruby/test_rational.rb, test/ruby/test_refinement.rb,
test/ruby/test_rubyvm.rb, test/ruby/test_struct.rb,
test/ruby/test_variable.rb, test/rubygems/test_gem_specification.rb,
test/thread/test_queue.rb: Use Integer instead of Fixnum and Bignum.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55029 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-05-17 09:15:57 -04:00
|
|
|
count += 1 if i.is_a?(Integer) && st.is_a?(String)
|
2015-08-26 18:59:32 -04:00
|
|
|
end
|
|
|
|
count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-14 14:39:21 -05:00
|
|
|
# No dead or finished threads, give up to 10 seconds to start running
|
|
|
|
t = Time.now
|
2016-11-15 10:04:56 -05:00
|
|
|
Thread.pass until Time.now - t > 10 || (consumers + producers).all?{|thr| thr.status =~ /\A(?:run|sleep)\z/}
|
2016-11-14 14:39:21 -05:00
|
|
|
|
2016-11-15 10:04:56 -05:00
|
|
|
assert (consumers + producers).all?{|thr| thr.status =~ /\A(?:run|sleep)\z/}, 'no threads running'
|
2015-08-26 18:59:32 -04:00
|
|
|
|
|
|
|
# just exercising the concurrency of the support methods.
|
|
|
|
counter = Thread.new do
|
|
|
|
until q.closed? && q.empty?
|
|
|
|
raise if q.size > q.max
|
|
|
|
# otherwise this exercise causes too much contention on the lock
|
|
|
|
sleep 0.01
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
producers.each &:join
|
2015-11-20 19:32:09 -05:00
|
|
|
q.close
|
2015-08-26 18:59:32 -04:00
|
|
|
|
|
|
|
# results not randomly distributed. Not sure why.
|
|
|
|
# consumers.map{|thr| thr.value}.each do |x|
|
|
|
|
# assert_not_equal 0, x
|
|
|
|
# end
|
|
|
|
|
|
|
|
all_items_count = consumers.map{|thr| thr.value}.inject(:+)
|
|
|
|
assert_equal count_items * count_producers, all_items_count
|
|
|
|
|
|
|
|
# don't leak this thread
|
|
|
|
assert_nothing_raised{counter.join}
|
|
|
|
end
|
2017-01-31 02:00:38 -05:00
|
|
|
|
|
|
|
def test_queue_with_trap
|
2017-02-01 17:14:22 -05:00
|
|
|
assert_in_out_err([], <<-INPUT, %w(INT INT exit), [])
|
2017-01-31 02:00:38 -05:00
|
|
|
q = Queue.new
|
2017-02-01 17:14:22 -05:00
|
|
|
trap(:INT){
|
|
|
|
q.push 'INT'
|
2017-01-31 02:00:38 -05:00
|
|
|
}
|
|
|
|
Thread.new{
|
|
|
|
loop{
|
2017-02-01 17:14:22 -05:00
|
|
|
Process.kill :INT, $$
|
2017-01-31 02:00:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
puts q.pop
|
|
|
|
puts q.pop
|
|
|
|
puts 'exit'
|
|
|
|
INPUT
|
|
|
|
end
|
2010-04-16 07:10:08 -04:00
|
|
|
end
|