summaryrefslogtreecommitdiffstats
path: root/test/ruby/test_thread.rb
blob: b5db362df4af8868f5d83c1b2322bfb597372280 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require 'test/unit'

class TestThread < Test::Unit::TestCase
  def test_mutex_synchronize
    m = Mutex.new
    r = 0
    max = 100
    (1..max).map{
      Thread.new{
        i=0
        while i<max*max
          i+=1
          m.synchronize{
            r += 1
          }
        end
      }
    }.each{|e|
      e.join
    }
    assert_equal(max * max * max, r)
  end
end

class TestThreadGroup < Test::Unit::TestCase
  def test_thread_init
    thgrp = ThreadGroup.new
    Thread.new{
      thgrp.add(Thread.current)
      assert_equal(thgrp, Thread.new{sleep 1}.group)
    }.join
  end

  def test_frozen_thgroup
    thgrp = ThreadGroup.new
    Thread.new{
      thgrp.add(Thread.current)
      thgrp.freeze
      assert_raise(ThreadError) do
        Thread.new{1}.join
      end
    }.join
  end

  def test_enclosed_thgroup
    thgrp = ThreadGroup.new
    thgrp.enclose
    Thread.new{
      assert_raise(ThreadError) do
        thgrp.add(Thread.current)
      end
      assert_nothing_raised do
        Thread.new{1}.join
      end
    }.join
  end
end