debian-mirror-gitlab/spec/lib/gitlab/database/reindexing/reindex_action_spec.rb

87 lines
2 KiB
Ruby
Raw Normal View History

2021-01-03 14:25:43 +05:30
# frozen_string_literal: true
require 'spec_helper'
2021-03-08 18:12:59 +05:30
RSpec.describe Gitlab::Database::Reindexing::ReindexAction do
include Database::DatabaseHelpers
2021-01-03 14:25:43 +05:30
2021-03-08 18:12:59 +05:30
let(:index) { create(:postgres_index) }
2021-01-03 14:25:43 +05:30
2021-03-08 18:12:59 +05:30
before_all do
swapout_view_for_table(:postgres_indexes)
2021-01-03 14:25:43 +05:30
end
2021-12-11 22:18:48 +05:30
it { is_expected.to be_a Gitlab::Database::SharedModel }
2021-03-08 18:12:59 +05:30
describe '.create_for' do
subject { described_class.create_for(index) }
2021-01-03 14:25:43 +05:30
2021-03-08 18:12:59 +05:30
it 'creates a new record for the given index' do
freeze_time do
record = subject
2021-01-03 14:25:43 +05:30
2021-03-08 18:12:59 +05:30
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)
2021-01-03 14:25:43 +05:30
2021-03-08 18:12:59 +05:30
expect(record).to be_persisted
end
2021-01-03 14:25:43 +05:30
end
end
2021-03-08 18:12:59 +05:30
describe '#finish' do
subject { action.finish }
2021-01-03 14:25:43 +05:30
2021-03-08 18:12:59 +05:30
let(:action) { build(:reindex_action, index: index) }
2021-01-03 14:25:43 +05:30
2021-03-08 18:12:59 +05:30
it 'sets #action_end' do
freeze_time do
subject
2021-01-03 14:25:43 +05:30
2021-03-08 18:12:59 +05:30
expect(action.action_end).to eq(Time.zone.now)
end
2021-01-03 14:25:43 +05:30
end
2021-03-08 18:12:59 +05:30
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
2021-01-03 14:25:43 +05:30
2021-03-08 18:12:59 +05:30
expect(action.ondisk_size_bytes_end).to eq(new_size)
2021-02-22 17:27:13 +05:30
end
2021-03-08 18:12:59 +05:30
context 'setting #state' do
it 'sets #state to finished if not given' do
action.state = nil
2021-01-03 14:25:43 +05:30
2021-03-08 18:12:59 +05:30
subject
2021-01-03 14:25:43 +05:30
2021-03-08 18:12:59 +05:30
expect(action).to be_finished
end
it 'sets #state to finished if not set to started' do
action.state = :started
2021-01-03 14:25:43 +05:30
2021-03-08 18:12:59 +05:30
subject
expect(action).to be_finished
2021-01-03 14:25:43 +05:30
end
2021-03-08 18:12:59 +05:30
it 'does not change state if set to failed' do
action.state = :failed
expect { subject }.not_to change { action.state }
end
end
2021-01-03 14:25:43 +05:30
2021-03-08 18:12:59 +05:30
it 'saves the record' do
expect(action).to receive(:save!)
2021-01-03 14:25:43 +05:30
2021-03-08 18:12:59 +05:30
subject
2021-01-03 14:25:43 +05:30
end
end
end