diff options
author | nagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-02-09 16:07:41 +0000 |
---|---|---|
committer | nagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-02-09 16:07:41 +0000 |
commit | 08e1428b7bf2baaa9fefeacfaef518dc75cc9c64 (patch) | |
tree | 90f61fe0e3eba2898ed2f2e03843baf5a890fc7c /test | |
parent | f0e5252835e5492333e9d223be043781d91667ce (diff) |
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
Diffstat (limited to 'test')
-rw-r--r-- | test/thread/test_queue.rb | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/test/thread/test_queue.rb b/test/thread/test_queue.rb index 993514f86d..6b4f10c4d2 100644 --- a/test/thread/test_queue.rb +++ b/test/thread/test_queue.rb @@ -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 |