Skip to content

Commit

Permalink
Merge pull request #1575 from nevinera/nev--1574--allow-mocking-mutex…
Browse files Browse the repository at this point in the history
…-synchronize

Allow successful mocking of Mutex#synchronize
  • Loading branch information
JonRowe committed May 8, 2024
2 parents 66c3dca + 1558a16 commit 4ff5847
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
9 changes: 3 additions & 6 deletions lib/rspec/mocks/proxy.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
RSpec::Support.require_rspec_support 'mutex'

module RSpec
module Mocks
# @private
Expand All @@ -9,11 +11,6 @@ def ==(expectation)
end
end

unless defined?(Mutex)
Support.require_rspec_support 'mutex'
Mutex = Support::Mutex
end

# @private
def ensure_implemented(*_args)
# noop for basic proxies, see VerifyingProxy for behaviour.
Expand All @@ -27,7 +24,7 @@ def initialize(object, order_group, options={})
@order_group = order_group
@error_generator = ErrorGenerator.new(object)
@messages_received = []
@messages_received_mutex = Mutex.new
@messages_received_mutex = Support::Mutex.new
@options = options
@null_object = false
@method_doubles = Hash.new { |h, k| h[k] = MethodDouble.new(@object, k, self) }
Expand Down
18 changes: 18 additions & 0 deletions spec/rspec/mocks/mutex_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module RSpec
module Mocks
RSpec.describe "Mocking Mutex" do
let(:mocked_mutex) { instance_double(Mutex) }
before do
allow(Mutex).to receive(:new).and_return(mocked_mutex)
allow(mocked_mutex).to receive(:synchronize).and_yield
end

it "successfully yields" do
called = false
mutex = Mutex.new
mutex.synchronize { called = true }
expect(called).to be_truthy
end
end
end
end

0 comments on commit 4ff5847

Please sign in to comment.