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

Commit 01eb9b2

Browse files
committed
Make RSpec::Support.thread_local_data thread but not fiber local
1 parent 05d835b commit 01eb9b2

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

Changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
### Development
22
[Full Changelog](http://github.com/rspec/rspec-support/compare/v3.12.0...main)
33

4+
Bug Fixes:
5+
6+
* Fix `RSpec::Support.thread_local_data` to be Thread local but not Fiber local.
7+
(Jon Rowe, #581)
8+
49
### 3.12.0 / 2022-10-26
510
[Full Changelog](http://github.com/rspec/rspec-support/compare/v3.11.1...v3.12.0)
611
Enhancements:

lib/rspec/support.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,14 @@ def self.class_of(object)
8989
end
9090

9191
# A single thread local variable so we don't excessively pollute that namespace.
92-
def self.thread_local_data
93-
Thread.current[:__rspec] ||= {}
92+
if RUBY_VERSION.to_f >= 2
93+
def self.thread_local_data
94+
Thread.current.thread_variable_get(:__rspec) || Thread.current.thread_variable_set(:__rspec, {})
95+
end
96+
else
97+
def self.thread_local_data
98+
Thread.current[:__rspec] ||= {}
99+
end
94100
end
95101

96102
# @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) && RUBY_VERSION.to_f >= 2.0
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)