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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
require 'abstract_unit'
require 'active_support/core_ext/thread'
class ThreadExt < ActiveSupport::TestCase
def test_main_thread_variable_in_enumerator
assert_equal Thread.main, Thread.current
Thread.current.thread_variable_set :foo, "bar"
thread, value = Fiber.new {
Fiber.yield [Thread.current, Thread.current.thread_variable_get(:foo)]
}.resume
assert_equal Thread.current, thread
assert_equal Thread.current.thread_variable_get(:foo), value
end
def test_thread_variable_in_enumerator
Thread.new {
Thread.current.thread_variable_set :foo, "bar"
thread, value = Fiber.new {
Fiber.yield [Thread.current, Thread.current.thread_variable_get(:foo)]
}.resume
assert_equal Thread.current, thread
assert_equal Thread.current.thread_variable_get(:foo), value
}.join
end
def test_thread_variables
assert_equal [], Thread.new { Thread.current.thread_variables }.join.value
t = Thread.new {
Thread.current.thread_variable_set(:foo, "bar")
Thread.current.thread_variables
}
assert_equal [:foo], t.join.value
end
def test_thread_variable?
assert_not Thread.new { Thread.current.thread_variable?("foo") }.join.value
t = Thread.new {
Thread.current.thread_variable_set("foo", "bar")
}.join
assert t.thread_variable?("foo")
assert t.thread_variable?(:foo)
assert_not t.thread_variable?(:bar)
end
def test_thread_variable_strings_and_symbols_are_the_same_key
t = Thread.new {}.join
t.thread_variable_set("foo", "bar")
assert_equal "bar", t.thread_variable_get(:foo)
end
def test_thread_variable_frozen
t = Thread.new { }.join
t.freeze
assert_raises(RuntimeError) do
t.thread_variable_set(:foo, "bar")
end
end
def test_thread_variable_frozen_after_set
t = Thread.new { }.join
t.thread_variable_set :foo, "bar"
t.freeze
assert_raises(RuntimeError) do
t.thread_variable_set(:baz, "qux")
end
end
def test_thread_variable_security
t = Thread.new { sleep }
assert_raises(SecurityError) do
Thread.new { $SAFE = 4; t.thread_variable_get(:foo) }.join
end
assert_raises(SecurityError) do
Thread.new { $SAFE = 4; t.thread_variable_set(:foo, :baz) }.join
end
end
end
|