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

Commit 1a69d22

Browse files
committed
Switch rspec local data to a thread accessor
1 parent d144907 commit 1a69d22

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

lib/rspec/support.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# frozen_string_literal: true
22

3+
class Thread
4+
attr_accessor :__rspec_local_data
5+
end
6+
37
module RSpec
48
module Support
59
# @api private
@@ -91,14 +95,8 @@ def self.class_of(object)
9195
end
9296

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

104102
# @api private

spec/rspec/support_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,17 @@ def object.some_method
204204
end.resume
205205
end
206206
end
207+
208+
it "works when Thread#thread_variable_get and Thread#thread_variable_set are mocked" do
209+
expect(Thread.current).to receive(:thread_variable_set).with(:test, true).once.and_return(true)
210+
expect(Thread.current).to receive(:thread_variable_get).with(:test).once.and_return(true)
211+
212+
Thread.current.thread_variable_set(:test, true)
213+
expect(Thread.current.thread_variable_get(:test)).to eq true
214+
215+
RSpec::Support.thread_local_data[:__for_test] = :oh_hai
216+
expect(RSpec::Support.thread_local_data[:__for_test]).to eq :oh_hai
217+
end
207218
end
208219

209220
describe "failure notification" do

0 commit comments

Comments
 (0)