Welcome to mirror list, hosted at ThFree Co, Russian Federation.

async_indexes_spec.rb « database « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c6991bf4e061268c012def906461cc0e580c3704 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::AsyncIndexes, feature_category: :database do
  describe '.create_pending_indexes!' do
    subject { described_class.create_pending_indexes! }

    before do
      create_list(:postgres_async_index, 4)
    end

    it 'takes 2 pending indexes and creates those' do
      indexes = described_class::PostgresAsyncIndex.to_create.order(:id).limit(2).to_a

      expect_next_instances_of(described_class::IndexCreator, 2, indexes) do |creator|
        expect(creator).to receive(:perform)
      end

      subject
    end
  end

  describe '.drop_pending_indexes!' do
    subject { described_class.drop_pending_indexes! }

    before do
      create_list(:postgres_async_index, 4, :with_drop)
    end

    it 'takes 2 pending indexes and destroys those' do
      indexes = described_class::PostgresAsyncIndex.to_drop.order(:id).limit(2).to_a

      expect_next_instances_of(described_class::IndexDestructor, 2, indexes) do |destructor|
        expect(destructor).to receive(:perform)
      end

      subject
    end
  end

  describe '.execute_pending_actions!' do
    subject { described_class.execute_pending_actions!(how_many: how_many) }

    let_it_be(:failed_creation_entry) { create(:postgres_async_index, attempts: 5) }
    let_it_be(:failed_removal_entry) { create(:postgres_async_index, :with_drop, attempts: 1) }
    let_it_be(:creation_entry) { create(:postgres_async_index) }
    let_it_be(:removal_entry) { create(:postgres_async_index, :with_drop) }

    context 'with one entry' do
      let(:how_many) { 1 }

      it 'executes instructions ordered by attempts and ids' do
        expect { subject }
          .to change { queued_entries_exist?(creation_entry) }.to(false)
          .and change { described_class::PostgresAsyncIndex.count }.by(-how_many)
      end
    end

    context 'with two entries' do
      let(:how_many) { 2 }

      it 'executes instructions ordered by attempts' do
        expect { subject }
          .to change { queued_entries_exist?(creation_entry, removal_entry) }.to(false)
          .and change { described_class::PostgresAsyncIndex.count }.by(-how_many)
      end
    end

    context 'when the budget allows more instructions' do
      let(:how_many) { 3 }

      it 'retries failed attempts' do
        expect { subject }
          .to change { queued_entries_exist?(creation_entry, removal_entry, failed_removal_entry) }.to(false)
          .and change { described_class::PostgresAsyncIndex.count }.by(-how_many)
      end
    end

    def queued_entries_exist?(*records)
      described_class::PostgresAsyncIndex.where(id: records).exists?
    end
  end
end