debian-mirror-gitlab/spec/models/namespace/traversal_hierarchy_spec.rb

103 lines
2.4 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-04-23 21:23:45 +05:30
RSpec.describe Namespace::TraversalHierarchy, type: :model, feature_category: :subgroups do
2021-11-18 22:05:49 +05:30
let!(:root) { create(:group, :with_hierarchy) }
2020-07-28 23:09:34 +05:30
describe '.for_namespace' do
2021-04-17 20:07:23 +05:30
let(:hierarchy) { described_class.for_namespace(group) }
2020-07-28 23:09:34 +05:30
context 'with root group' do
2021-04-17 20:07:23 +05:30
let(:group) { root }
2020-07-28 23:09:34 +05:30
it { expect(hierarchy.root).to eq root }
end
context 'with child group' do
2021-04-17 20:07:23 +05:30
let(:group) { root.children.first.children.first }
2020-07-28 23:09:34 +05:30
it { expect(hierarchy.root).to eq root }
end
context 'with group outside of hierarchy' do
2022-01-26 12:08:38 +05:30
let(:group) { create(:group) }
2020-07-28 23:09:34 +05:30
it { expect(hierarchy.root).not_to eq root }
end
end
describe '.new' do
2021-04-17 20:07:23 +05:30
let(:hierarchy) { described_class.new(group) }
2020-07-28 23:09:34 +05:30
context 'with root group' do
2021-04-17 20:07:23 +05:30
let(:group) { root }
2020-07-28 23:09:34 +05:30
it { expect(hierarchy.root).to eq root }
end
context 'with child group' do
2021-04-17 20:07:23 +05:30
let(:group) { root.children.first }
2020-07-28 23:09:34 +05:30
it { expect { hierarchy }.to raise_error(StandardError, 'Must specify a root node') }
end
end
describe '#incorrect_traversal_ids' do
2021-04-29 21:17:54 +05:30
let!(:hierarchy) { described_class.new(root) }
subject { hierarchy.incorrect_traversal_ids }
before do
Namespace.update_all(traversal_ids: [])
end
2020-07-28 23:09:34 +05:30
it { is_expected.to match_array Namespace.all }
end
describe '#sync_traversal_ids!' do
2021-04-29 21:17:54 +05:30
let!(:hierarchy) { described_class.new(root) }
2020-07-28 23:09:34 +05:30
2021-04-29 21:17:54 +05:30
subject { hierarchy.sync_traversal_ids! }
2020-07-28 23:09:34 +05:30
it { expect(hierarchy.incorrect_traversal_ids).to be_empty }
2021-04-29 21:17:54 +05:30
2021-11-18 22:05:49 +05:30
it_behaves_like 'hierarchy with traversal_ids' do
before do
subject
end
end
2021-06-08 01:23:25 +05:30
it_behaves_like 'locked row' do
let(:recorded_queries) { ActiveRecord::QueryRecorder.new }
let(:row) { root }
2021-04-29 21:17:54 +05:30
before do
2021-06-08 01:23:25 +05:30
recorded_queries.record { subject }
end
end
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
context 'when deadlocked' do
before do
allow(root).to receive(:lock!) { raise ActiveRecord::Deadlocked }
2021-04-29 21:17:54 +05:30
end
it { expect { subject }.to raise_error(ActiveRecord::Deadlocked) }
it 'increment db_deadlock counter' do
2022-08-27 11:52:29 +05:30
expect do
subject
rescue StandardError
nil
end.to change { db_deadlock_total('Namespace#sync_traversal_ids!') }.by(1)
2021-04-29 21:17:54 +05:30
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-28 23:09:34 +05:30
end
end