Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 986ac43

Browse files
committed
Make RSpec::Support.thread_local_data thread but not fiber local
1 parent 6d33eaa commit 986ac43

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/rspec/support.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def self.class_of(object)
9090

9191
# A single thread local variable so we don't excessively pollute that namespace.
9292
def self.thread_local_data
93-
Thread.current[:__rspec] ||= {}
93+
Thread.current.thread_variable_get(:__rspec) || Thread.current.thread_variable_set(:__rspec, {})
9494
end
9595

9696
# @api private

spec/rspec/support_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,26 @@ def object.some_method
186186
end
187187
end
188188

189+
describe ".thread_local_data" do
190+
it "contains data local to the current thread" do
191+
RSpec::Support.thread_local_data[:__for_test] = :oh_hai
192+
193+
Thread.new do
194+
expect(RSpec::Support.thread_local_data).to eq({})
195+
end.join
196+
end
197+
198+
if defined?(Fiber)
199+
it "shares data across fibres" do
200+
RSpec::Support.thread_local_data[:__for_test] = :oh_hai
201+
202+
Fiber.new do
203+
expect(RSpec::Support.thread_local_data[:__for_test]).to eq(:oh_hai)
204+
end.resume
205+
end
206+
end
207+
end
208+
189209
describe "failure notification" do
190210
before { @failure_notifier = RSpec::Support.failure_notifier }
191211
after { RSpec::Support.failure_notifier = @failure_notifier }

0 commit comments

Comments
 (0)