aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-02-22 07:16:13 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-02-22 07:16:13 +0000
commit7d54b4ea58bd0f072d4c368590247209e2183a54 (patch)
tree981a47a205537b3fbafedb5d337e92b6f7a4f3eb /test
parent31ef3124a9db534abcc3e323f5d3cb696eda3bf5 (diff)
downloadruby-7d54b4ea58bd0f072d4c368590247209e2183a54.tar.gz
Thread#fetch
* thread.c (rb_thread_fetch): add new method Thread#fetch. [Feature #13009] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57683 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r--test/ruby/test_thread.rb30
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