mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
a46d29bb4e
positive number. patch by Masaki Matsushita. [ruby-dev:44449] [Bug #5259] * test/thread/test_queue.rb (test_sized_queue_initialize, test_sized_queue_assign_max): add tests for it. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33170 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
56 lines
1.3 KiB
Ruby
56 lines
1.3 KiB
Ruby
require 'test/unit'
|
|
require 'thread'
|
|
|
|
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
|
|
|
|
num_threads.times { to_workers.push nil }
|
|
workers.each { |t| t.join }
|
|
|
|
assert_equal 0, from_workers.size
|
|
assert_equal 0, to_workers.size
|
|
end
|
|
|
|
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
|
|
end
|