diff --git a/rspec-support/lib/rspec/support/reentrant_mutex.rb b/rspec-support/lib/rspec/support/reentrant_mutex.rb index 13ee22bd5..361135942 100644 --- a/rspec-support/lib/rspec/support/reentrant_mutex.rb +++ b/rspec-support/lib/rspec/support/reentrant_mutex.rb @@ -43,7 +43,15 @@ def exit if defined? ::Mutex # On 1.9 and up, this is in core, so we just use the real one - Mutex = ::Mutex + class Mutex < ::Mutex + # If you mock Mutex.new you break our usage of Mutex, so + # instead we capture the original method to return Mutexs. + NEW_MUTEX_METHOD = Mutex.method(:new) + + def self.new + NEW_MUTEX_METHOD.call + end + end else # For 1.8.7 # :nocov: RSpec::Support.require_rspec_support "mutex" diff --git a/rspec-support/spec/rspec/support/mutex_spec.rb b/rspec-support/spec/rspec/support/mutex_spec.rb new file mode 100644 index 000000000..a600ed695 --- /dev/null +++ b/rspec-support/spec/rspec/support/mutex_spec.rb @@ -0,0 +1,8 @@ +require 'rspec/support/mutex' + +RSpec.describe RSpec::Support::Mutex do + it "allows ::Mutex to be mocked", :if => defined?(::Mutex) do + expect(Mutex).to receive(:new) + ::Mutex.new + end +end