Skip to content

Commit

Permalink
Make RSpec::Support.thread_local_data thread but not fiber local
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed Jun 26, 2023
1 parent 05d835b commit afb75a5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
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)
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 afb75a5

Please sign in to comment.