diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-12-11 17:45:27 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-12-11 18:52:02 -0300 |
commit | a565f80bcb0dd9687330d5644ce9baef58e9c835 (patch) | |
tree | e71f6fd1074b5539cd5fc5c5fe47fdb9655e49d0 /activesupport/test/core_ext | |
parent | 48583f8bf74d1cefefea3cd6591bd546a9eaff6c (diff) | |
download | rails-a565f80bcb0dd9687330d5644ce9baef58e9c835.tar.gz rails-a565f80bcb0dd9687330d5644ce9baef58e9c835.tar.bz2 rails-a565f80bcb0dd9687330d5644ce9baef58e9c835.zip |
Backport thread-local variables from Ruby 2.0
Diffstat (limited to 'activesupport/test/core_ext')
-rw-r--r-- | activesupport/test/core_ext/thread_test.rb | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/thread_test.rb b/activesupport/test/core_ext/thread_test.rb new file mode 100644 index 0000000000..b58f59a8d4 --- /dev/null +++ b/activesupport/test/core_ext/thread_test.rb @@ -0,0 +1,77 @@ +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? + refute 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) + refute 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_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 |