debian-mirror-gitlab/spec/lib/gitlab/optimistic_locking_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

139 lines
3.7 KiB
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::OptimisticLocking do
2017-08-17 22:00:37 +05:30
let!(:pipeline) { create(:ci_pipeline) }
let!(:pipeline2) { Ci::Pipeline.find(pipeline.id) }
2021-04-17 20:07:23 +05:30
let(:histogram) { spy('prometheus metric') }
before do
allow(described_class)
.to receive(:retry_lock_histogram)
.and_return(histogram)
end
2017-08-17 22:00:37 +05:30
describe '#retry_lock' do
2021-04-17 20:07:23 +05:30
let(:name) { 'optimistic_locking_spec' }
2017-08-17 22:00:37 +05:30
2023-05-27 22:25:52 +05:30
it 'does not change current_scope', :aggregate_failures do
instance = Class.new { include Gitlab::OptimisticLocking }.new
relation = pipeline.cancelable_statuses
expected_scope = Ci::Build.current_scope&.to_sql
instance.send(:retry_lock, relation, name: :test) do
expect(Ci::Build.current_scope&.to_sql).to eq(expected_scope)
end
expect(Ci::Build.current_scope&.to_sql).to eq(expected_scope)
end
2021-04-17 20:07:23 +05:30
context 'when state changed successfully without retries' do
subject do
described_class.retry_lock(pipeline, name: name) do |lock_subject|
lock_subject.succeed
end
2017-08-17 22:00:37 +05:30
end
2021-04-17 20:07:23 +05:30
it 'does not reload object' do
expect(pipeline).not_to receive(:reset)
expect(pipeline).to receive(:succeed).and_call_original
subject
end
it 'does not create log record' do
expect(described_class.retry_lock_logger).not_to receive(:info)
subject
end
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
it 'adds number of retries to histogram' do
subject
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
expect(histogram).to have_received(:observe).with({}, 0)
2017-08-17 22:00:37 +05:30
end
end
2021-04-17 20:07:23 +05:30
context 'when at least one retry happened, the change succeeded' do
subject do
described_class.retry_lock(pipeline2, name: 'optimistic_locking_spec') do |lock_subject|
lock_subject.drop
end
end
before do
pipeline.succeed
end
it 'completes the action' do
expect(pipeline2).to receive(:reset).and_call_original
expect(pipeline2).to receive(:drop).twice.and_call_original
subject
end
it 'creates a single log record' do
expect(described_class.retry_lock_logger)
.to receive(:info)
.once
.with(hash_including(:time_s, name: name, retries: 1))
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
subject
end
it 'adds number of retries to histogram' do
subject
expect(histogram).to have_received(:observe).with({}, 1)
end
end
context 'when MAX_RETRIES attempts exceeded' do
subject do
described_class.retry_lock(pipeline, max_retries, name: name) do |lock_subject|
lock_subject.lock_version = 100
lock_subject.drop
2017-08-17 22:00:37 +05:30
end
2021-04-17 20:07:23 +05:30
end
let(:max_retries) { 2 }
it 'raises an exception' do
expect(pipeline).to receive(:drop).exactly(max_retries + 1).times.and_call_original
expect { subject }.to raise_error(ActiveRecord::StaleObjectError)
end
it 'creates a single log record' do
expect(described_class.retry_lock_logger)
.to receive(:info)
.once
.with(hash_including(:time_s, name: name, retries: max_retries))
expect { subject }.to raise_error(ActiveRecord::StaleObjectError)
end
it 'adds number of retries to histogram' do
expect { subject }.to raise_error(ActiveRecord::StaleObjectError)
expect(histogram).to have_received(:observe).with({}, max_retries)
end
2017-08-17 22:00:37 +05:30
end
end
describe '#retry_optimistic_lock' do
context 'when locking module is mixed in' do
let(:unlockable) do
Class.new.include(described_class).new
end
it 'is an alias for retry_lock' do
expect(unlockable.method(:retry_optimistic_lock))
.to eq unlockable.method(:retry_lock)
end
end
end
end