1# frozen_string_literal: false
2require 'test/unit'
3
4class TestThreadGroup < Test::Unit::TestCase
5  def test_thread_init
6    thgrp = ThreadGroup.new
7    th = Thread.new{
8      thgrp.add(Thread.current)
9      Thread.new{sleep 1}
10    }.value
11    assert_equal(thgrp, th.group)
12  ensure
13    th.join
14  end
15
16  def test_frozen_thgroup
17    thgrp = ThreadGroup.new
18
19    t = Thread.new{1}
20    Thread.new{
21      thgrp.add(Thread.current)
22      thgrp.freeze
23      assert_raise(ThreadError) do
24        Thread.new{1}.join
25      end
26      assert_raise(ThreadError) do
27        thgrp.add(t)
28      end
29      assert_raise(ThreadError) do
30        ThreadGroup.new.add Thread.current
31      end
32    }.join
33    t.join
34  end
35
36  def test_enclosed_thgroup
37    thgrp = ThreadGroup.new
38    assert_equal(false, thgrp.enclosed?)
39
40    t = Thread.new{1}
41    Thread.new{
42      thgrp.add(Thread.current)
43      thgrp.enclose
44      assert_equal(true, thgrp.enclosed?)
45      assert_nothing_raised do
46        Thread.new{1}.join
47      end
48      assert_raise(ThreadError) do
49        thgrp.add t
50      end
51      assert_raise(ThreadError) do
52        ThreadGroup.new.add Thread.current
53      end
54    }.join
55    t.join
56  end
57end
58