-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure we cache and synchronize appropriately so we don't merge more than once or do unsafe reads of the resultset. This has the added benefit of reducing the runtime of at_exit hooks by over 66%, since we now do less than 1/3 the work. See https://gist.github.com/jenseng/62465f674f8c02de09ef776f23d4dca4 for simple repro script. Basically there are 3 main problems when using merging: 1. `SimpleCov::ResultMerger.stored_data` doesn't synchronize its read, so it can see an empty file if another process is writing it. 2. `SimpleCov::ResultMerger.resultset` calls `.stored_data` twice and never caches the result. 3. `SimpleCov.result` doesn't cache the `@result`, so the default `at_exit` behavior causes it to store and merge 3 times. Due to 1. and 2., this is extra bad This can cause the formatter to miss out on coverage data and/or get the wrong values for covered percentages. Furthermore, if you use OJ, `JSON.parse("") -> nil`, which means `.resultset` can be nil, so this race condition causes exceptions as seen in #406. In addition to fixing the race condition, also add ` || {}` to make `.stored_data` a bit more robust and protect against an empty .resultset.json.
- Loading branch information
Showing
4 changed files
with
241 additions
and
26 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
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
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
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,109 @@ | ||
require "helper" | ||
|
||
if SimpleCov.usable? | ||
describe SimpleCov do | ||
describe ".result" do | ||
before do | ||
SimpleCov.clear_result | ||
allow(Coverage).to receive(:result).once.and_return({}) | ||
end | ||
|
||
context "with merging disabled" do | ||
before do | ||
allow(SimpleCov).to receive(:use_merging).once.and_return(false) | ||
end | ||
|
||
context "when not running" do | ||
before do | ||
allow(SimpleCov).to receive(:running).and_return(false) | ||
end | ||
|
||
it "returns nil" do | ||
expect(SimpleCov.result).to be_nil | ||
end | ||
end | ||
|
||
context "when running" do | ||
before do | ||
allow(SimpleCov).to receive(:running).and_return(true, false) | ||
end | ||
|
||
it "uses the result from Coverage" do | ||
expect(Coverage).to receive(:result).once.and_return(__FILE__ => [0, 1]) | ||
expect(SimpleCov.result.filenames).to eq [__FILE__] | ||
end | ||
|
||
it "adds not-loaded-files" do | ||
expect(SimpleCov).to receive(:add_not_loaded_files).once.and_return({}) | ||
SimpleCov.result | ||
end | ||
|
||
it "doesn't store the current coverage" do | ||
expect(SimpleCov::ResultMerger).not_to receive(:store_result) | ||
SimpleCov.result | ||
end | ||
|
||
it "doesn't merge the result" do | ||
expect(SimpleCov::ResultMerger).not_to receive(:merged_result) | ||
SimpleCov.result | ||
end | ||
|
||
it "caches its result" do | ||
result = SimpleCov.result | ||
expect(SimpleCov.result).to be(result) | ||
end | ||
end | ||
end | ||
|
||
context "with merging enabled" do | ||
let(:the_merged_result) { double } | ||
|
||
before do | ||
allow(SimpleCov).to receive(:use_merging).once.and_return(true) | ||
allow(SimpleCov::ResultMerger).to receive(:store_result).once | ||
allow(SimpleCov::ResultMerger).to receive(:merged_result).once.and_return(the_merged_result) | ||
end | ||
|
||
context "when not running" do | ||
before do | ||
allow(SimpleCov).to receive(:running).and_return(false) | ||
end | ||
|
||
it "merges the result" do | ||
expect(SimpleCov.result).to be(the_merged_result) | ||
end | ||
end | ||
|
||
context "when running" do | ||
before do | ||
allow(SimpleCov).to receive(:running).and_return(true, false) | ||
end | ||
|
||
it "uses the result from Coverage" do | ||
expect(Coverage).to receive(:result).once.and_return({}) | ||
SimpleCov.result | ||
end | ||
|
||
it "adds not-loaded-files" do | ||
expect(SimpleCov).to receive(:add_not_loaded_files).once.and_return({}) | ||
SimpleCov.result | ||
end | ||
|
||
it "stores the current coverage" do | ||
expect(SimpleCov::ResultMerger).to receive(:store_result).once | ||
SimpleCov.result | ||
end | ||
|
||
it "merges the result" do | ||
expect(SimpleCov.result).to be(the_merged_result) | ||
end | ||
|
||
it "caches its result" do | ||
result = SimpleCov.result | ||
expect(SimpleCov.result).to be(result) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |