summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/reindexing/reindex_action_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/lib/gitlab/database/reindexing/reindex_action_spec.rb')
-rw-r--r--spec/lib/gitlab/database/reindexing/reindex_action_spec.rb112
1 files changed, 52 insertions, 60 deletions
diff --git a/spec/lib/gitlab/database/reindexing/reindex_action_spec.rb b/spec/lib/gitlab/database/reindexing/reindex_action_spec.rb
index 225f23d2135..a8f196d8f0e 100644
--- a/spec/lib/gitlab/database/reindexing/reindex_action_spec.rb
+++ b/spec/lib/gitlab/database/reindexing/reindex_action_spec.rb
@@ -2,91 +2,83 @@
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, bloat_size: 42) }
- let(:size_after) { 512 }
+RSpec.describe Gitlab::Database::Reindexing::ReindexAction do
+ include Database::DatabaseHelpers
- it 'yields to the caller' do
- expect { |b| described_class.keep_track_of(index, &b) }.to yield_control
- end
+ let(:index) { create(:postgres_index) }
- def find_record
- described_class.find_by(index_identifier: index.identifier)
+ before_all do
+ swapout_view_for_table(:postgres_indexes)
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)
+ describe '.create_for' do
+ subject { described_class.create_for(index) }
- travel(10.seconds)
- end
+ it 'creates a new record for the given index' do
+ freeze_time do
+ record = subject
- duration = find_record.action_end - find_record.action_start
+ 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(duration).to be_within(1.second).of(10.seconds)
+ expect(record).to be_persisted
+ end
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
+ describe '#finish' do
+ subject { action.finish }
- expect(find_record).to be_finished
- end
+ let(:action) { build(:reindex_action, index: index) }
- 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)
+ it 'sets #action_end' do
+ freeze_time do
+ subject
- expect(index).to receive(:reload).once
- allow(index).to receive(:ondisk_size_bytes).and_return(size_after)
+ expect(action.action_end).to eq(Time.zone.now)
+ end
end
- expect(find_record.ondisk_size_bytes_end).to eq(size_after)
- 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
- it 'creates the record with the indexes bloat estimate' do
- described_class.keep_track_of(index) do
- expect(find_record.bloat_estimate_bytes_start).to eq(index.bloat_size)
+ expect(action.ondisk_size_bytes_end).to eq(new_size)
end
- 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/)
+ context 'setting #state' do
+ it 'sets #state to finished if not given' do
+ action.state = nil
- expect(find_record).to be_failed
- end
+ subject
- 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(action).to be_finished
+ end
+
+ it 'sets #state to finished if not set to started' do
+ action.state = :started
- expect(find_record.action_end).to be_within(1.second).of(Time.zone.now)
+ subject
+
+ expect(action).to be_finished
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)
+ it 'does not change state if set to failed' do
+ action.state = :failed
+
+ expect { subject }.not_to change { action.state }
+ end
+ end
- expect do
- described_class.keep_track_of(index) do
- raise 'something went wrong'
- end
- end.to raise_error(/something went wrong/)
+ it 'saves the record' do
+ expect(action).to receive(:save!)
- expect(find_record.ondisk_size_bytes_end).to eq(size_after)
+ subject
end
end
end