Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make RSpec::Mocks::Space more thread-safe.
It used to be possible for different threads to receive different proxy objects, potentially a source of bugs such as #380. This is non-trivial to write a spec for, but you can trivially demonstrate the problem by adding a `sleep` into the fetch blocks, and then running: o = Object.new t = Thread.new do RSpec::Mocks.space.proxy_for(o) end b = RSpec::Mocks.space.proxy_for(o) a = t.value expect(a).to eq(b)
- Loading branch information
1 parent
12510a9
commit 9674568
Showing
2 changed files
with
50 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
$LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__)) | ||
|
||
require 'benchmark' | ||
require 'rspec/mocks' | ||
|
||
Benchmark.bm do |bm| | ||
bm.report("fetching a proxy") do | ||
RSpec::Mocks.with_temporary_scope do | ||
o = Object.new | ||
100000.times { | ||
RSpec::Mocks.space.proxy_for(o) | ||
} | ||
end | ||
end | ||
end | ||
|
||
# Without synchronize (not thread-safe): | ||
# | ||
# user system total real | ||
# fetching a proxy 0.120000 0.000000 0.120000 ( 0.141333) | ||
# | ||
# With synchronize (thread-safe): | ||
# user system total real | ||
# fetching a proxy 0.180000 0.000000 0.180000 ( 0.189553) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters