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

Allow mocking Mutex.new #411

Merged
merged 1 commit into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/rspec/support/reentrant_mutex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions spec/rspec/support/mutex_spec.rb
Original file line number Diff line number Diff line change
@@ -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