Commit 47b2add4 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Add tests for optimistic locking

parent 5d7ee7a1
...@@ -66,6 +66,7 @@ Please view this file on the master branch, on stable branches it's out of date. ...@@ -66,6 +66,7 @@ Please view this file on the master branch, on stable branches it's out of date.
- Updating verbiage on git basics to be more intuitive - Updating verbiage on git basics to be more intuitive
- Fix project_feature record not generated on project creation - Fix project_feature record not generated on project creation
- Clarify documentation for Runners API (Gennady Trafimenkov) - Clarify documentation for Runners API (Gennady Trafimenkov)
- Use optimistic locking for pipelines and builds
- The instrumentation for Banzai::Renderer has been restored - The instrumentation for Banzai::Renderer has been restored
- Change user & group landing page routing from /u/:username to /:username - Change user & group landing page routing from /u/:username to /:username
- Added documentation for .gitattributes files - Added documentation for .gitattributes files
......
...@@ -260,7 +260,7 @@ module Ci ...@@ -260,7 +260,7 @@ module Ci
end end
def update_status def update_status
Gitlab::OptimisticLocking.retry_lock(build) do Gitlab::OptimisticLocking.retry_lock(self) do
case latest_builds_status case latest_builds_status
when 'pending' then enqueue when 'pending' then enqueue
when 'running' then run when 'running' then run
......
...@@ -35,7 +35,7 @@ module Ci ...@@ -35,7 +35,7 @@ module Ci
build build
rescue StateMachines::InvalidTransition, StaleObjectError rescue StateMachines::InvalidTransition, ActiveRecord::StaleObjectError
nil nil
end end
......
module Gitlab module Gitlab
module OptimisticLocking class OptimisticLocking
def retry_lock(subject, &block) def self.retry_lock(subject, &block)
while true do loop do
begin begin
return yield subject subject.transaction do
rescue StaleObjectError return block.call(subject)
end
rescue ActiveRecord::StaleObjectError
subject.reload subject.reload
end end
end end
......
require 'spec_helper'
describe Gitlab::OptimisticLocking, lib: true do
describe '#retry_lock' do
let!(:pipeline) { create(:ci_pipeline) }
let!(:pipeline2) { Ci::Pipeline.find(pipeline.id) }
it 'does not reload object if state changes' do
expect(pipeline).not_to receive(:reload)
expect(pipeline).to receive(:succeed).and_call_original
described_class.retry_lock(pipeline) do |subject|
subject.succeed
end
end
it 'retries action if exception is raised' do
pipeline.succeed
expect(pipeline2).to receive(:reload).and_call_original
expect(pipeline2).to receive(:drop).twice.and_call_original
described_class.retry_lock(pipeline2) do |subject|
subject.drop
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment