From 7d54b4ea58bd0f072d4c368590247209e2183a54 Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 22 Feb 2017 07:16:13 +0000 Subject: 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 --- test/ruby/test_thread.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'test/ruby') 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 -- cgit v1.2.3