summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/reindexing/reindex_action_spec.rb
blob: 1b409924acc2409b513cc0f57a54f19554e23d0a (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::Reindexing::ReindexAction do
  include Database::DatabaseHelpers

  let(:index) { create(:postgres_index) }

  before_all do
    swapout_view_for_table(:postgres_indexes)
  end

  it { is_expected.to be_a Gitlab::Database::SharedModel }

  describe '.create_for' do
    subject { described_class.create_for(index) }

    it 'creates a new record for the given index' do
      freeze_time do
        record = subject

        expect(record.index_identifier).to eq(index.identifier)
        expect(record.action_start).to eq(Time.zone.now)
        expect(record.ondisk_size_bytes_start).to eq(index.ondisk_size_bytes)
        expect(subject.bloat_estimate_bytes_start).to eq(index.bloat_size)

        expect(record).to be_persisted
      end
    end
  end

  describe '#finish' do
    subject { action.finish }

    let(:action) { build(:reindex_action, index: index) }

    it 'sets #action_end' do
      freeze_time do
        subject

        expect(action.action_end).to eq(Time.zone.now)
      end
    end

    it 'sets #ondisk_size_bytes_end after reloading the index record' do
      new_size = 4711
      expect(action.index).to receive(:reload).ordered
      expect(action.index).to receive(:ondisk_size_bytes).and_return(new_size).ordered

      subject

      expect(action.ondisk_size_bytes_end).to eq(new_size)
    end

    context 'setting #state' do
      it 'sets #state to finished if not given' do
        action.state = nil

        subject

        expect(action).to be_finished
      end

      it 'sets #state to finished if not set to started' do
        action.state = :started

        subject

        expect(action).to be_finished
      end

      it 'does not change state if set to failed' do
        action.state = :failed

        expect { subject }.not_to change { action.state }
      end
    end

    it 'saves the record' do
      expect(action).to receive(:save!)

      subject
    end
  end
end