diff options
Diffstat (limited to 'spec/lib/gitlab/database/reindexing/coordinator_spec.rb')
-rw-r--r-- | spec/lib/gitlab/database/reindexing/coordinator_spec.rb | 82 |
1 files changed, 48 insertions, 34 deletions
diff --git a/spec/lib/gitlab/database/reindexing/coordinator_spec.rb b/spec/lib/gitlab/database/reindexing/coordinator_spec.rb index f45d959c0de..ae6362ba812 100644 --- a/spec/lib/gitlab/database/reindexing/coordinator_spec.rb +++ b/spec/lib/gitlab/database/reindexing/coordinator_spec.rb @@ -3,65 +3,79 @@ require 'spec_helper' RSpec.describe Gitlab::Database::Reindexing::Coordinator do + include Database::DatabaseHelpers include ExclusiveLeaseHelpers describe '.perform' do - subject { described_class.new(indexes).perform } + subject { described_class.new(index, notifier).perform } - let(:indexes) { [instance_double(Gitlab::Database::PostgresIndex), instance_double(Gitlab::Database::PostgresIndex)] } - let(:reindexers) { [instance_double(Gitlab::Database::Reindexing::ConcurrentReindex), instance_double(Gitlab::Database::Reindexing::ConcurrentReindex)] } + before do + swapout_view_for_table(:postgres_indexes) + + allow(Gitlab::Database::Reindexing::ConcurrentReindex).to receive(:new).with(index).and_return(reindexer) + allow(Gitlab::Database::Reindexing::ReindexAction).to receive(:create_for).with(index).and_return(action) + end + + let(:index) { create(:postgres_index) } + let(:notifier) { instance_double(Gitlab::Database::Reindexing::GrafanaNotifier, notify_start: nil, notify_end: nil) } + let(:reindexer) { instance_double(Gitlab::Database::Reindexing::ConcurrentReindex, perform: nil) } + let(:action) { create(:reindex_action, index: index) } let!(:lease) { stub_exclusive_lease(lease_key, uuid, timeout: lease_timeout) } let(:lease_key) { 'gitlab/database/reindexing/coordinator' } let(:lease_timeout) { 1.day } let(:uuid) { 'uuid' } - before do - allow(Gitlab::Database::Reindexing::ReindexAction).to receive(:keep_track_of).and_yield + context 'locking' do + it 'acquires a lock while reindexing' do + expect(lease).to receive(:try_obtain).ordered.and_return(uuid) - indexes.zip(reindexers).each do |index, reindexer| - allow(Gitlab::Database::Reindexing::ConcurrentReindex).to receive(:new).with(index).and_return(reindexer) - allow(reindexer).to receive(:perform) - end - end + expect(reindexer).to receive(:perform).ordered - it 'performs concurrent reindexing for each index' do - indexes.zip(reindexers).each do |index, reindexer| - expect(Gitlab::Database::Reindexing::ConcurrentReindex).to receive(:new).with(index).ordered.and_return(reindexer) - expect(reindexer).to receive(:perform) + expect(Gitlab::ExclusiveLease).to receive(:cancel).ordered.with(lease_key, uuid) + + subject end - subject + it 'does not perform reindexing actions if lease is not granted' do + expect(lease).to receive(:try_obtain).ordered.and_return(false) + expect(Gitlab::Database::Reindexing::ConcurrentReindex).not_to receive(:new) + + subject + end end - it 'keeps track of actions and creates ReindexAction records' do - indexes.each do |index| - expect(Gitlab::Database::Reindexing::ReindexAction).to receive(:keep_track_of).with(index).and_yield + context 'notifications' do + it 'sends #notify_start before reindexing' do + expect(notifier).to receive(:notify_start).with(action).ordered + expect(reindexer).to receive(:perform).ordered + + subject end - subject + it 'sends #notify_end after reindexing and updating the action is done' do + expect(action).to receive(:finish).ordered + expect(notifier).to receive(:notify_end).with(action).ordered + + subject + end end - context 'locking' do - it 'acquires a lock while reindexing' do - indexes.each do |index| - expect(lease).to receive(:try_obtain).ordered.and_return(uuid) - action = instance_double(Gitlab::Database::Reindexing::ConcurrentReindex) - expect(Gitlab::Database::Reindexing::ConcurrentReindex).to receive(:new).ordered.with(index).and_return(action) - expect(action).to receive(:perform).ordered - expect(Gitlab::ExclusiveLease).to receive(:cancel).ordered.with(lease_key, uuid) - end + context 'action tracking' do + it 'calls #finish on the action' do + expect(reindexer).to receive(:perform).ordered + expect(action).to receive(:finish).ordered subject end - it 'does does not perform reindexing actions if lease is not granted' do - indexes.each do |index| - expect(lease).to receive(:try_obtain).ordered.and_return(false) - expect(Gitlab::Database::Reindexing::ConcurrentReindex).not_to receive(:new) - end + it 'upon error, it still calls finish and raises the error' do + expect(reindexer).to receive(:perform).ordered.and_raise('something went wrong') + expect(action).to receive(:finish).ordered - subject + expect { subject }.to raise_error(/something went wrong/) + + expect(action).to be_failed end end end |