diff options
Diffstat (limited to 'spec/workers/background_migration_worker_spec.rb')
-rw-r--r-- | spec/workers/background_migration_worker_spec.rb | 100 |
1 files changed, 73 insertions, 27 deletions
diff --git a/spec/workers/background_migration_worker_spec.rb b/spec/workers/background_migration_worker_spec.rb index 15e93d62c7d..8094efcaf04 100644 --- a/spec/workers/background_migration_worker_spec.rb +++ b/spec/workers/background_migration_worker_spec.rb @@ -12,45 +12,91 @@ RSpec.describe BackgroundMigrationWorker, :clean_gitlab_redis_shared_state do end describe '#perform' do - it 'performs a background migration' do - expect(Gitlab::BackgroundMigration) - .to receive(:perform) - .with('Foo', [10, 20]) + before do + allow(worker).to receive(:jid).and_return(1) + expect(worker).to receive(:always_perform?).and_return(false) + end - worker.perform('Foo', [10, 20]) + context 'when lease can be obtained' do + before do + expect(Gitlab::BackgroundMigration) + .to receive(:perform) + .with('Foo', [10, 20]) + end + + it 'performs a background migration' do + worker.perform('Foo', [10, 20]) + end + + context 'when lease_attempts is 1' do + it 'performs a background migration' do + worker.perform('Foo', [10, 20], 1) + end + end end - it 'reschedules a migration if it was performed recently' do - expect(worker) - .to receive(:always_perform?) - .and_return(false) + context 'when lease not obtained (migration of same class was performed recently)' do + before do + expect(Gitlab::BackgroundMigration).not_to receive(:perform) + + worker.lease_for('Foo').try_obtain + end - worker.lease_for('Foo').try_obtain + it 'reschedules the migration and decrements the lease_attempts' do + expect(described_class) + .to receive(:perform_in) + .with(a_kind_of(Numeric), 'Foo', [10, 20], 4) - expect(Gitlab::BackgroundMigration) - .not_to receive(:perform) + worker.perform('Foo', [10, 20], 5) + end - expect(described_class) - .to receive(:perform_in) - .with(a_kind_of(Numeric), 'Foo', [10, 20]) + context 'when lease_attempts is 1' do + it 'reschedules the migration and decrements the lease_attempts' do + expect(described_class) + .to receive(:perform_in) + .with(a_kind_of(Numeric), 'Foo', [10, 20], 0) - worker.perform('Foo', [10, 20]) + worker.perform('Foo', [10, 20], 1) + end + end + + context 'when lease_attempts is 0' do + it 'gives up performing the migration' do + expect(described_class).not_to receive(:perform_in) + expect(Sidekiq.logger).to receive(:warn).with( + class: 'Foo', + message: 'Job could not get an exclusive lease after several tries. Giving up.', + job_id: 1) + + worker.perform('Foo', [10, 20], 0) + end + end end - it 'reschedules a migration if the database is not healthy' do - allow(worker) - .to receive(:always_perform?) - .and_return(false) + context 'when database is not healthy' do + before do + allow(worker).to receive(:healthy_database?).and_return(false) + end - allow(worker) - .to receive(:healthy_database?) - .and_return(false) + it 'reschedules a migration if the database is not healthy' do + expect(described_class) + .to receive(:perform_in) + .with(a_kind_of(Numeric), 'Foo', [10, 20], 4) - expect(described_class) - .to receive(:perform_in) - .with(a_kind_of(Numeric), 'Foo', [10, 20]) + worker.perform('Foo', [10, 20]) + end - worker.perform('Foo', [10, 20]) + context 'when lease_attempts is 0' do + it 'gives up performing the migration' do + expect(described_class).not_to receive(:perform_in) + expect(Sidekiq.logger).to receive(:warn).with( + class: 'Foo', + message: 'Database was unhealthy after several tries. Giving up.', + job_id: 1) + + worker.perform('Foo', [10, 20], 0) + end + end end it 'sets the class that will be executed as the caller_id' do |