Skip to content

Commit

Permalink
Merge pull request #581 from rspec/fix-thread-local
Browse files Browse the repository at this point in the history
Make RSpec::Support.thread_local_data thread but not fiber local
  • Loading branch information
JonRowe committed Jun 26, 2023
2 parents 05d835b + 01eb9b2 commit 65addb0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Changelog.md
@@ -1,6 +1,11 @@
### Development
[Full Changelog](http://github.com/rspec/rspec-support/compare/v3.12.0...main)

Bug Fixes:

* Fix `RSpec::Support.thread_local_data` to be Thread local but not Fiber local.
(Jon Rowe, #581)

### 3.12.0 / 2022-10-26
[Full Changelog](http://github.com/rspec/rspec-support/compare/v3.11.1...v3.12.0)
Enhancements:
Expand Down
10 changes: 8 additions & 2 deletions lib/rspec/support.rb
Expand Up @@ -89,8 +89,14 @@ def self.class_of(object)
end

# A single thread local variable so we don't excessively pollute that namespace.
def self.thread_local_data
Thread.current[:__rspec] ||= {}
if RUBY_VERSION.to_f >= 2
def self.thread_local_data
Thread.current.thread_variable_get(:__rspec) || Thread.current.thread_variable_set(:__rspec, {})
end
else
def self.thread_local_data
Thread.current[:__rspec] ||= {}
end
end

# @api private
Expand Down
20 changes: 20 additions & 0 deletions spec/rspec/support_spec.rb
Expand Up @@ -186,6 +186,26 @@ def object.some_method
end
end

describe ".thread_local_data" do
it "contains data local to the current thread" do
RSpec::Support.thread_local_data[:__for_test] = :oh_hai

Thread.new do
expect(RSpec::Support.thread_local_data).to eq({})
end.join
end

if defined?(Fiber) && RUBY_VERSION.to_f >= 2.0
it "shares data across fibres" do
RSpec::Support.thread_local_data[:__for_test] = :oh_hai

Fiber.new do
expect(RSpec::Support.thread_local_data[:__for_test]).to eq(:oh_hai)
end.resume
end
end
end

describe "failure notification" do
before { @failure_notifier = RSpec::Support.failure_notifier }
after { RSpec::Support.failure_notifier = @failure_notifier }
Expand Down

0 comments on commit 65addb0

Please sign in to comment.