-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1682 from herwinw/thread_variables
Add Thread local variables
- Loading branch information
Showing
8 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require_relative '../../spec_helper' | ||
|
||
describe "Thread#thread_variable_get" do | ||
before :each do | ||
@t = Thread.new { } | ||
end | ||
|
||
after :each do | ||
@t.join | ||
end | ||
|
||
it "returns nil if the variable is not set" do | ||
@t.thread_variable_get(:a).should be_nil | ||
end | ||
|
||
it "returns the value previously set by #thread_variable_set" do | ||
@t.thread_variable_set :a, 49 | ||
@t.thread_variable_get(:a).should == 49 | ||
end | ||
|
||
it "returns a value private to self" do | ||
@t.thread_variable_set :thread_variable_get_spec, 82 | ||
Thread.current.thread_variable_get(:thread_variable_get_spec).should be_nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require_relative '../../spec_helper' | ||
|
||
describe "Thread#thread_variable_set" do | ||
before :each do | ||
@t = Thread.new { } | ||
end | ||
|
||
after :each do | ||
@t.join | ||
end | ||
|
||
it "returns the value set" do | ||
(@t.thread_variable_set :a, 2).should == 2 | ||
end | ||
|
||
it "sets a value that will be returned by #thread_variable_get" do | ||
@t.thread_variable_set :a, 49 | ||
@t.thread_variable_get(:a).should == 49 | ||
end | ||
|
||
it "sets a value private to self" do | ||
@t.thread_variable_set :thread_variable_get_spec, 82 | ||
@t.thread_variable_get(:thread_variable_get_spec).should == 82 | ||
Thread.current.thread_variable_get(:thread_variable_get_spec).should be_nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require_relative '../../spec_helper' | ||
|
||
describe "Thread#thread_variable?" do | ||
before :each do | ||
@t = Thread.new { } | ||
end | ||
|
||
after :each do | ||
@t.join | ||
end | ||
|
||
it "returns false if the thread variables do not contain 'key'" do | ||
@t.thread_variable_set :a, 2 | ||
@t.thread_variable?(:b).should be_false | ||
end | ||
|
||
it "returns true if the thread variables contain 'key'" do | ||
@t.thread_variable_set :a, 2 | ||
@t.thread_variable?(:a).should be_true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require_relative '../../spec_helper' | ||
|
||
describe "Thread#thread_variables" do | ||
before :each do | ||
@t = Thread.new { } | ||
end | ||
|
||
after :each do | ||
@t.join | ||
end | ||
|
||
it "returns the keys of all the values set" do | ||
@t.thread_variable_set :a, 2 | ||
@t.thread_variable_set :b, 4 | ||
@t.thread_variable_set :c, 6 | ||
@t.thread_variables.sort.should == [:a, :b, :c] | ||
end | ||
|
||
it "sets a value private to self" do | ||
@t.thread_variable_set :a, 82 | ||
@t.thread_variable_set :b, 82 | ||
Thread.current.thread_variables.should_not include(:a, :b) | ||
end | ||
|
||
it "only contains user thread variables and is empty initially" do | ||
Thread.current.thread_variables.should == [] | ||
@t.thread_variables.should == [] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters