Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make RSpec::Support.thread_local_data thread but not fiber local #581

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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