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

Commit

Permalink
Switch rspec local data to a thread accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed Jul 17, 2024
1 parent d144907 commit 1a69d22
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
14 changes: 6 additions & 8 deletions lib/rspec/support.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# frozen_string_literal: true

class Thread
attr_accessor :__rspec_local_data
end

module RSpec
module Support
# @api private
Expand Down Expand Up @@ -91,14 +95,8 @@ def self.class_of(object)
end

# A single thread local variable so we don't excessively pollute that namespace.
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
def self.thread_local_data
Thread.current.__rspec_local_data ||= {}
end

# @api private
Expand Down
11 changes: 11 additions & 0 deletions spec/rspec/support_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ def object.some_method
end.resume
end
end

it "works when Thread#thread_variable_get and Thread#thread_variable_set are mocked" do
expect(Thread.current).to receive(:thread_variable_set).with(:test, true).once.and_return(true)
expect(Thread.current).to receive(:thread_variable_get).with(:test).once.and_return(true)

Thread.current.thread_variable_set(:test, true)
expect(Thread.current.thread_variable_get(:test)).to eq true

RSpec::Support.thread_local_data[:__for_test] = :oh_hai
expect(RSpec::Support.thread_local_data[:__for_test]).to eq :oh_hai
end
end

describe "failure notification" do
Expand Down

0 comments on commit 1a69d22

Please sign in to comment.