diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/ruby/test_thread.rb | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/test/ruby/test_thread.rb b/test/ruby/test_thread.rb index d7b5dadf81..c82018dc8e 100644 --- a/test/ruby/test_thread.rb +++ b/test/ruby/test_thread.rb @@ -482,6 +482,36 @@ class TestThread < Test::Unit::TestCase t.kill if t end + def test_thread_local_fetch + t = Thread.new { sleep } + + assert_equal(false, t.key?(:foo)) + + t["foo"] = "foo" + t["bar"] = "bar" + t["baz"] = "baz" + + x = nil + assert_equal("foo", t.fetch(:foo, 0)) + assert_equal("foo", t.fetch(:foo) {x = true}) + assert_nil(x) + assert_equal("foo", t.fetch("foo", 0)) + assert_equal("foo", t.fetch("foo") {x = true}) + assert_nil(x) + + x = nil + assert_equal(0, t.fetch(:qux, 0)) + assert_equal(1, t.fetch(:qux) {x = 1}) + assert_equal(1, x) + assert_equal(2, t.fetch("qux", 2)) + assert_equal(3, t.fetch("qux") {x = 3}) + assert_equal(3, x) + + assert_raise(KeyError) {t.fetch(:qux)} + ensure + t.kill if t + end + def test_thread_local_security assert_raise(RuntimeError) do Thread.new do |