2020-07-14 05:09:02 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Namespace::TraversalHierarchy, type: :model do
|
2021-10-05 11:12:53 -04:00
|
|
|
let!(:root) { create(:group, :with_hierarchy) }
|
2020-07-14 05:09:02 -04:00
|
|
|
|
|
|
|
describe '.for_namespace' do
|
2021-03-01 10:10:53 -05:00
|
|
|
let(:hierarchy) { described_class.for_namespace(group) }
|
2020-07-14 05:09:02 -04:00
|
|
|
|
|
|
|
context 'with root group' do
|
2021-03-01 10:10:53 -05:00
|
|
|
let(:group) { root }
|
2020-07-14 05:09:02 -04:00
|
|
|
|
|
|
|
it { expect(hierarchy.root).to eq root }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with child group' do
|
2021-03-01 10:10:53 -05:00
|
|
|
let(:group) { root.children.first.children.first }
|
2020-07-14 05:09:02 -04:00
|
|
|
|
|
|
|
it { expect(hierarchy.root).to eq root }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with group outside of hierarchy' do
|
2021-11-26 07:12:49 -05:00
|
|
|
let(:group) { create(:group) }
|
2020-07-14 05:09:02 -04:00
|
|
|
|
|
|
|
it { expect(hierarchy.root).not_to eq root }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.new' do
|
2021-03-01 10:10:53 -05:00
|
|
|
let(:hierarchy) { described_class.new(group) }
|
2020-07-14 05:09:02 -04:00
|
|
|
|
|
|
|
context 'with root group' do
|
2021-03-01 10:10:53 -05:00
|
|
|
let(:group) { root }
|
2020-07-14 05:09:02 -04:00
|
|
|
|
|
|
|
it { expect(hierarchy.root).to eq root }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with child group' do
|
2021-03-01 10:10:53 -05:00
|
|
|
let(:group) { root.children.first }
|
2020-07-14 05:09:02 -04:00
|
|
|
|
|
|
|
it { expect { hierarchy }.to raise_error(StandardError, 'Must specify a root node') }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#incorrect_traversal_ids' do
|
2021-03-19 17:08:58 -04:00
|
|
|
let!(:hierarchy) { described_class.new(root) }
|
|
|
|
|
|
|
|
subject { hierarchy.incorrect_traversal_ids }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Namespace.update_all(traversal_ids: [])
|
|
|
|
end
|
2020-07-14 05:09:02 -04:00
|
|
|
|
|
|
|
it { is_expected.to match_array Namespace.all }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#sync_traversal_ids!' do
|
2021-03-19 17:08:58 -04:00
|
|
|
let!(:hierarchy) { described_class.new(root) }
|
2020-07-14 05:09:02 -04:00
|
|
|
|
2021-03-19 17:08:58 -04:00
|
|
|
subject { hierarchy.sync_traversal_ids! }
|
2020-07-14 05:09:02 -04:00
|
|
|
|
|
|
|
it { expect(hierarchy.incorrect_traversal_ids).to be_empty }
|
2021-03-19 17:08:58 -04:00
|
|
|
|
2021-10-05 11:12:53 -04:00
|
|
|
it_behaves_like 'hierarchy with traversal_ids' do
|
|
|
|
before do
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-01 10:22:06 -05:00
|
|
|
it_behaves_like 'locked row' do
|
2021-05-07 08:10:27 -04:00
|
|
|
let(:recorded_queries) { ActiveRecord::QueryRecorder.new }
|
|
|
|
let(:row) { root }
|
2021-03-19 17:08:58 -04:00
|
|
|
|
|
|
|
before do
|
2021-05-07 08:10:27 -04:00
|
|
|
recorded_queries.record { subject }
|
|
|
|
end
|
|
|
|
end
|
2021-03-19 17:08:58 -04:00
|
|
|
|
2021-05-07 08:10:27 -04:00
|
|
|
context 'when deadlocked' do
|
|
|
|
before do
|
|
|
|
allow(root).to receive(:lock!) { raise ActiveRecord::Deadlocked }
|
2021-03-19 17:08:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it { expect { subject }.to raise_error(ActiveRecord::Deadlocked) }
|
|
|
|
|
|
|
|
it 'increment db_deadlock counter' do
|
|
|
|
expect { subject rescue nil }.to change { db_deadlock_total('Namespace#sync_traversal_ids!') }.by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def db_deadlock_total(source)
|
|
|
|
Gitlab::Metrics
|
|
|
|
.counter(:db_deadlock, 'Counts the times we have deadlocked in the database')
|
|
|
|
.get(source: source)
|
2020-07-14 05:09:02 -04:00
|
|
|
end
|
|
|
|
end
|