Skip to content

Commit 59dab45

Browse files
andrykonchinheadius
authored andcommitted
Fix converting a thread-local variable name into Symbol in Thread methods
1 parent 052b036 commit 59dab45

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

core/thread/element_reference_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@
3737
t2["value"].should == 2
3838
end
3939

40+
it "converts a key that is neither String nor Symbol with #to_str" do
41+
key = mock('value')
42+
key.should_receive(:to_str).and_return('value')
43+
44+
th = Thread.new do
45+
Thread.current[:value] = 1
46+
end.join
47+
48+
th[key].should == 1
49+
end
50+
4051
it "raises exceptions on the wrong type of keys" do
4152
-> { Thread.current[nil] }.should raise_error(TypeError)
4253
-> { Thread.current[5] }.should raise_error(TypeError)

core/thread/element_set_spec.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,33 @@
1212
th.freeze
1313
-> {
1414
th[:foo] = "bar"
15-
}.should raise_error(FrozenError, /frozen/)
15+
}.should raise_error(FrozenError, "can't modify frozen thread locals")
1616
end.join
1717
end
1818

19+
it "accepts Strings and Symbols" do
20+
t1 = Thread.new do
21+
Thread.current[:value] = 1
22+
end.join
23+
t2 = Thread.new do
24+
Thread.current["value"] = 2
25+
end.join
26+
27+
t1[:value].should == 1
28+
t2[:value].should == 2
29+
end
30+
31+
it "converts a key that is neither String nor Symbol with #to_str" do
32+
key = mock('value')
33+
key.should_receive(:to_str).and_return('value')
34+
35+
th = Thread.new do
36+
Thread.current[key] = 1
37+
end.join
38+
39+
th[:value].should == 1
40+
end
41+
1942
it "raises exceptions on the wrong type of keys" do
2043
-> { Thread.current[nil] = true }.should raise_error(TypeError)
2144
-> { Thread.current[5] = true }.should raise_error(TypeError)

core/thread/key_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
@th.key?(:stanley.to_s).should == false
1717
end
1818

19+
it "converts a key that is neither String nor Symbol with #to_str" do
20+
key = mock('key')
21+
key.should_receive(:to_str).and_return('oliver')
22+
23+
@th.key?(key).should == true
24+
end
25+
1926
it "raises exceptions on the wrong type of keys" do
2027
-> { Thread.current.key? nil }.should raise_error(TypeError)
2128
-> { Thread.current.key? 5 }.should raise_error(TypeError)

0 commit comments

Comments
 (0)