2020-09-23 08:09:58 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Gitlab::Checks::MatchingMergeRequest do
|
|
|
|
describe '#match?' do
|
|
|
|
let_it_be(:newrev) { '012345678' }
|
|
|
|
let_it_be(:target_branch) { 'feature' }
|
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
|
|
|
let_it_be(:locked_merge_request) do
|
|
|
|
create(:merge_request,
|
|
|
|
:locked,
|
|
|
|
source_project: project,
|
|
|
|
target_project: project,
|
|
|
|
target_branch: target_branch,
|
|
|
|
in_progress_merge_commit_sha: newrev)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.new(newrev, target_branch, project) }
|
|
|
|
|
2021-06-04 08:10:17 -04:00
|
|
|
let(:total_counter) { subject.send(:total_counter) }
|
|
|
|
let(:stale_counter) { subject.send(:stale_counter) }
|
|
|
|
|
2020-09-23 08:09:58 -04:00
|
|
|
it 'matches a merge request' do
|
|
|
|
expect(subject.match?).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not match any merge request' do
|
|
|
|
matcher = described_class.new(newrev, 'test', project)
|
|
|
|
|
|
|
|
expect(matcher.match?).to be false
|
|
|
|
end
|
2021-06-04 08:10:17 -04:00
|
|
|
|
2021-09-21 14:11:18 -04:00
|
|
|
context 'with load balancing enabled' do
|
2021-06-04 08:10:17 -04:00
|
|
|
let(:session) { ::Gitlab::Database::LoadBalancing::Session.current }
|
|
|
|
let(:all_caught_up) { true }
|
|
|
|
|
|
|
|
before do
|
2021-09-21 14:11:18 -04:00
|
|
|
Gitlab::Database::LoadBalancing::Session.clear_session
|
2021-06-09 02:10:29 -04:00
|
|
|
|
2021-10-11 11:13:01 -04:00
|
|
|
allow(::ApplicationRecord.sticking)
|
|
|
|
.to receive(:all_caught_up?)
|
|
|
|
.and_return(all_caught_up)
|
|
|
|
|
|
|
|
expect(::ApplicationRecord.sticking)
|
|
|
|
.to receive(:select_valid_host)
|
|
|
|
.with(:project, project.id)
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
allow(::ApplicationRecord.sticking)
|
|
|
|
.to receive(:select_caught_up_replicas)
|
|
|
|
.with(:project, project.id)
|
|
|
|
.and_return(all_caught_up)
|
2021-06-04 08:10:17 -04:00
|
|
|
end
|
|
|
|
|
2021-09-21 14:11:18 -04:00
|
|
|
after do
|
|
|
|
Gitlab::Database::LoadBalancing::Session.clear_session
|
|
|
|
end
|
|
|
|
|
2021-06-04 08:10:17 -04:00
|
|
|
shared_examples 'secondary that has caught up to a primary' do
|
|
|
|
it 'continues to use the secondary' do
|
|
|
|
expect(session.use_primary?).to be false
|
|
|
|
expect(subject.match?).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'only increments total counter' do
|
|
|
|
expect { subject.match? }
|
|
|
|
.to change { total_counter.get }.by(1)
|
|
|
|
.and change { stale_counter.get }.by(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'secondary that is lagging primary' do
|
|
|
|
it 'sticks to the primary' do
|
|
|
|
expect(subject.match?).to be true
|
|
|
|
expect(session.use_primary?).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'increments both total and stale counters' do
|
|
|
|
expect { subject.match? }
|
|
|
|
.to change { total_counter.get }.by(1)
|
|
|
|
.and change { stale_counter.get }.by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-09 02:10:29 -04:00
|
|
|
it_behaves_like 'secondary that has caught up to a primary'
|
2021-06-04 08:10:17 -04:00
|
|
|
|
2021-06-09 02:10:29 -04:00
|
|
|
context 'on secondary behind primary' do
|
|
|
|
let(:all_caught_up) { false }
|
2021-06-04 08:10:17 -04:00
|
|
|
|
2021-06-09 02:10:29 -04:00
|
|
|
it_behaves_like 'secondary that is lagging primary'
|
2021-06-04 08:10:17 -04:00
|
|
|
end
|
|
|
|
end
|
2020-09-23 08:09:58 -04:00
|
|
|
end
|
|
|
|
end
|