Project

General

Profile

Backport #2092 ยป test_queue.patch

quix (James M. Lawrence), 09/12/2009 09:49 PM

View differences:

test/ruby/test_queue.rb (revision 0)
1
require 'test/unit'
2
require 'thread'
3

  
4
class TestQueue < Test::Unit::TestCase
5
  def test_queue
6
    grind(5, 10_000, 15, Queue)
7
  end
8

  
9
  def test_sized_queue
10
    grind(5, 10_000, 15, SizedQueue, 10_000)
11
  end
12

  
13
  def grind(num_threads, num_objects, num_iterations, klass, *args)
14
    from_workers = klass.new(*args)
15
    to_workers = klass.new(*args)
16

  
17
    workers = (1..num_threads).map {
18
      Thread.new {
19
        while object = to_workers.pop
20
          from_workers.push object
21
        end
22
      }
23
    }
24

  
25
    Thread.new {
26
      num_iterations.times {
27
        num_objects.times { to_workers.push 99 }
28
        num_objects.times { from_workers.pop }
29
      }
30
    }.join
31
  
32
    num_threads.times { to_workers.push nil }
33
    workers.each { |t| t.join }
34

  
35
    assert 0, from_workers.size
36
    assert 0, to_workers.size
37
  end
38
end