2010-04-16 07:10:08 -04:00
|
|
|
require 'test/unit'
|
|
|
|
require 'thread'
|
2011-12-22 20:15:35 -05:00
|
|
|
require 'tmpdir'
|
2011-12-13 19:23:43 -05:00
|
|
|
require_relative '../ruby/envutil'
|
2010-04-16 07:10:08 -04:00
|
|
|
|
|
|
|
class TestQueue < Test::Unit::TestCase
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
end
|
2011-12-13 19:23:43 -05:00
|
|
|
|
2012-09-09 08:32:33 -04:00
|
|
|
def test_sized_queue_and_wakeup
|
|
|
|
sq = SizedQueue.new(1)
|
|
|
|
sq.push(0)
|
|
|
|
|
|
|
|
t1 = Thread.start { sq.push(1) ; sleep }
|
|
|
|
|
|
|
|
sleep 0.1 until t1.stop?
|
|
|
|
t1.wakeup
|
|
|
|
sleep 0.1 until t1.stop?
|
|
|
|
|
|
|
|
t2 = Thread.start { sq.push(2) }
|
|
|
|
sleep 0.1 until t1.stop? && t2.stop?
|
|
|
|
|
|
|
|
queue_wait = sq.instance_eval{ @queue_wait }
|
|
|
|
assert_equal(queue_wait.uniq, queue_wait)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def test_sized_queue_push_interrupt
|
|
|
|
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|
|
2012-09-29 07:19:11 -04:00
|
|
|
timeout = 30
|
2011-12-23 02:55:39 -05:00
|
|
|
total_count = 2000
|
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
|
|
|
|
th.kill.join
|
|
|
|
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
|
2010-04-16 07:10:08 -04:00
|
|
|
end
|