Project

General

Profile

Feature #3176 ยป prio_test.rb

coatl (caleb clausen), 05/18/2010 07:47 AM

 
1
require 'thread'
2
require 'test/unit'
3

    
4
class ThreadPriorityTest<Test::Unit::TestCase
5
  def assert_greater_than a,b
6
    assert_operator a, :>, b
7
  end
8

    
9
  def test_priorities(priobase=nil,mult=nil)
10
    $c1=$c2=$c3=0
11

    
12
    priobase||=ENV['PRIOBASE']||'0'
13
    priobase=priobase.to_i
14

    
15
    mult||=ENV['PRIOGAP']||'1'
16
    mult=mult.to_i
17

    
18
    $m=Mutex.new
19
    $cv=ConditionVariable.new
20
    t3=Thread.new{ $m.synchronize{$cv.wait $m};  $c3+=1 while 1 }
21
    t2=Thread.new{ $m.synchronize{$cv.wait $m};  $c2+=1 while 1 }
22
    t1=Thread.new{ $m.synchronize{$cv.wait $m};  $c1+=1 while 1 }
23

    
24
    Thread.current.priority=priobase
25
    t1.priority=priobase
26
    t2.priority=priobase-1*mult
27
    t3.priority=priobase-2*mult
28
    sleep 0.2
29
    $m.synchronize{$cv.broadcast}
30

    
31
    sleep 10
32
    t1.kill; t2.kill; t3.kill
33

    
34
    if ENV['VERBOSE']
35
      p [:c1=, $c1]
36
      p [:c2=, $c2]
37
      p [:c3=, $c3]
38
      p [:total, $c1+$c2+$c3]
39
    end
40

    
41
    assert_greater_than $c1, 1.1*$c2
42
    assert_greater_than $c2, 1.1*$c3
43

    
44
  ensure
45
    Thread.current.priority=0
46
  end
47
end