summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/reindexing/reindex_action_spec.rb
blob: efb5b8463a1a5ab1bd9d8b78a88d3ae124c017f7 (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, '.keep_track_of' do
  let(:index) { double('index', identifier: 'public.something', ondisk_size_bytes: 10240, reload: nil) }
  let(:size_after) { 512 }

  it 'yields to the caller' do
    expect { |b| described_class.keep_track_of(index, &b) }.to yield_control
  end

  def find_record
    described_class.find_by(index_identifier: index.identifier)
  end

  it 'creates the record with a start time and updates its end time' do
    freeze_time do
      described_class.keep_track_of(index) do
        expect(find_record.action_start).to be_within(1.second).of(Time.zone.now)

        travel(10.seconds)
      end

      duration = find_record.action_end - find_record.action_start

      expect(duration).to be_within(1.second).of(10.seconds)
    end
  end

  it 'creates the record with its status set to :started and updates its state to :finished' do
    described_class.keep_track_of(index) do
      expect(find_record).to be_started
    end

    expect(find_record).to be_finished
  end

  it 'creates the record with the indexes start size and updates its end size' do
    described_class.keep_track_of(index) do
      expect(find_record.ondisk_size_bytes_start).to eq(index.ondisk_size_bytes)

      expect(index).to receive(:reload).once
      allow(index).to receive(:ondisk_size_bytes).and_return(size_after)
    end

    expect(find_record.ondisk_size_bytes_end).to eq(size_after)
  end

  context 'in case of errors' do
    it 'sets the state to failed' do
      expect do
        described_class.keep_track_of(index) do
          raise 'something went wrong'
        end
      end.to raise_error(/something went wrong/)

      expect(find_record).to be_failed
    end

    it 'records the end time' do
      freeze_time do
        expect do
          described_class.keep_track_of(index) do
            raise 'something went wrong'
          end
        end.to raise_error(/something went wrong/)

        expect(find_record.action_end).to be_within(1.second).of(Time.zone.now)
      end
    end

    it 'records the resulting index size' do
      expect(index).to receive(:reload).once
      allow(index).to receive(:ondisk_size_bytes).and_return(size_after)

      expect do
        described_class.keep_track_of(index) do
          raise 'something went wrong'
        end
      end.to raise_error(/something went wrong/)

      expect(find_record.ondisk_size_bytes_end).to eq(size_after)
    end
  end
end