Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PersistenceStrategy to wrap ModuleResult creation in transaction #218

Merged
merged 2 commits into from
Jul 19, 2016
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
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ KalibroProcessor is the processing web service for Mezuro.

== Unreleased

* Fix possible inconsistency in module result creation (lack of transaction)
* Improve acceptance tests by fixing small bugs and adding processing times
* Introduce performance tests for Aggregator
* Update KolektiMetricfu
Expand Down
18 changes: 10 additions & 8 deletions lib/metric_collector/persistence_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ def create_related_hotspot_metric_results(metric_configuration, results)
def find_or_create_module_result(module_name, granularity)
kalibro_module = new_kalibro_module(module_name, granularity)

module_result = ModuleResult.find_by_module_and_processing(kalibro_module, @processing)
return module_result unless module_result.nil?

module_result = ModuleResult.create!(processing: @processing)
kalibro_module.module_result = module_result
kalibro_module.save!
module_result.update!(kalibro_module: kalibro_module)
module_result
ModuleResult.transaction do
module_result = ModuleResult.find_by_module_and_processing(kalibro_module, @processing)
if !module_result.nil?
module_result
else
kalibro_module.build_module_result(processing: @processing)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice usage of ActiveRecord's builtin method.

kalibro_module.save!
kalibro_module.module_result
end
end
end

def new_kalibro_module(module_name, granularity)
Expand Down
19 changes: 10 additions & 9 deletions spec/lib/metric_collector/persistence_strategy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
describe MetricCollector::PersistenceStrategy do
let(:kalibro_module) { FactoryGirl.build(:kalibro_module) }
let(:processing) { FactoryGirl.build(:processing) }
let(:module_result) { FactoryGirl.build(:module_result) }
let(:module_result) { FactoryGirl.build(:module_result, processing: processing) }
let(:metric_configuration) { FactoryGirl.build(:metric_configuration, :with_id) }

subject { described_class.new(processing) }
Expand All @@ -24,12 +24,12 @@
before do
subject.expects(:new_kalibro_module).returns(kalibro_module)
ModuleResult.expects(:find_by_module_and_processing).with(kalibro_module, processing).returns module_result
end

it 'is expected to create a tree metric result' do
TreeMetricResult.expects(:create!).with(module_result: module_result,
metric_configuration_id: metric_configuration.id,
value: value).returns tree_metric_result
end

it 'is expected to create a tree metric result' do
subject.create_tree_metric_result(metric_configuration, kalibro_module.long_name, value, kalibro_module.granularity)
end
end
Expand All @@ -38,15 +38,16 @@
before do
subject.expects(:new_kalibro_module).returns(kalibro_module)
ModuleResult.expects(:find_by_module_and_processing).with(kalibro_module, processing).returns nil
ModuleResult.expects(:create!).with(processing: processing).returns module_result
kalibro_module.expects(:build_module_result).with(processing: processing) do
kalibro_module.module_result = module_result
end
kalibro_module.expects(:save!)
module_result.expects(:update!).with(kalibro_module: kalibro_module)
end

it 'is expected to create a tree metric result' do
TreeMetricResult.expects(:create!).with(module_result: module_result,
metric_configuration_id: metric_configuration.id,
value: value).returns tree_metric_result
end

it 'is expected to create a tree metric result' do
subject.create_tree_metric_result(metric_configuration, kalibro_module.long_name, value, kalibro_module.granularity)
end
end
Expand Down