summaryrefslogtreecommitdiff
path: root/spec/models/namespace/traversal_hierarchy_spec.rb
blob: b166d5411719824556c68956436260d6b9f58a29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Namespace::TraversalHierarchy, type: :model do
  let_it_be(:root, reload: true) { create(:group, :with_hierarchy) }

  describe '.for_namespace' do
    let(:hierarchy) { described_class.for_namespace(group) }

    context 'with root group' do
      let(:group) { root }

      it { expect(hierarchy.root).to eq root }
    end

    context 'with child group' do
      let(:group) { root.children.first.children.first }

      it { expect(hierarchy.root).to eq root }
    end

    context 'with group outside of hierarchy' do
      let(:group) { create(:namespace) }

      it { expect(hierarchy.root).not_to eq root }
    end
  end

  describe '.new' do
    let(:hierarchy) { described_class.new(group) }

    context 'with root group' do
      let(:group) { root }

      it { expect(hierarchy.root).to eq root }
    end

    context 'with child group' do
      let(:group) { root.children.first }

      it { expect { hierarchy }.to raise_error(StandardError, 'Must specify a root node') }
    end
  end

  shared_examples 'locked update query' do
    it 'locks query with FOR UPDATE' do
      qr = ActiveRecord::QueryRecorder.new do
        subject
      end
      expect(qr.count).to eq 1
      expect(qr.log.first).to match /FOR UPDATE/
    end
  end

  describe '#incorrect_traversal_ids' do
    let!(:hierarchy) { described_class.new(root) }

    subject { hierarchy.incorrect_traversal_ids }

    before do
      Namespace.update_all(traversal_ids: [])
    end

    it { is_expected.to match_array Namespace.all }

    context 'when lock is true' do
      subject { hierarchy.incorrect_traversal_ids(lock: true).load }

      it_behaves_like 'locked update query'
    end
  end

  describe '#sync_traversal_ids!' do
    let!(:hierarchy) { described_class.new(root) }

    subject { hierarchy.sync_traversal_ids! }

    it { expect(hierarchy.incorrect_traversal_ids).to be_empty }

    it_behaves_like 'hierarchy with traversal_ids'
    it_behaves_like 'locked update query'

    context 'when deadlocked' do
      before do
        connection_double = double(:connection)

        allow(Namespace).to receive(:connection).and_return(connection_double)
        allow(connection_double).to receive(:exec_query) { raise ActiveRecord::Deadlocked.new }
      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)
  end
end