merge revision(s) 44595: [Backport #9342]

* ext/thread/thread.c (rb_szqueue_clear): notify SZQUEUE_WAITERS
	  on SizedQueue#clear. [ruby-core:59462] [Bug #9342]

	* test/thread/test_queue.rb: add test. the patch is from
	  Justin Collins.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_0_0@44899 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nagachika 2014-02-09 16:07:41 +00:00
parent f0e5252835
commit 08e1428b7b
4 changed files with 88 additions and 1 deletions

View file

@ -108,4 +108,71 @@ class TestQueue < Test::Unit::TestCase
end
}
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
end
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
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
assert_nothing_raised(TimeoutError) do
timeout(1) { th2.join }
end
end
end