debian-mirror-gitlab/spec/lib/gitlab/background_migration_spec.rb

302 lines
9 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::BackgroundMigration do
2017-09-10 17:25:29 +05:30
describe '.queue' do
it 'returns background migration worker queue' do
expect(described_class.queue)
.to eq BackgroundMigrationWorker.sidekiq_options['queue']
end
end
describe '.steal' do
context 'when there are enqueued jobs present' do
let(:queue) do
2021-10-27 15:23:28 +05:30
[
double(args: ['Foo', [10, 20]], klass: 'BackgroundMigrationWorker'),
double(args: ['Bar', [20, 30]], klass: 'BackgroundMigrationWorker'),
double(args: ['Foo', [20, 30]], klass: 'MergeWorker')
]
2017-09-10 17:25:29 +05:30
end
before do
allow(Sidekiq::Queue).to receive(:new)
.with(described_class.queue)
.and_return(queue)
end
context 'when queue contains unprocessed jobs' do
it 'steals jobs from a queue' do
expect(queue[0]).to receive(:delete).and_return(true)
expect(described_class).to receive(:perform)
.with('Foo', [10, 20])
described_class.steal('Foo')
end
it 'does not steal job that has already been taken' do
expect(queue[0]).to receive(:delete).and_return(false)
expect(described_class).not_to receive(:perform)
described_class.steal('Foo')
end
it 'does not steal jobs for a different migration' do
expect(described_class).not_to receive(:perform)
expect(queue[0]).not_to receive(:delete)
2021-10-27 15:23:28 +05:30
described_class.steal('Baz')
2017-09-10 17:25:29 +05:30
end
2020-07-28 23:09:34 +05:30
context 'when a custom predicate is given' do
it 'steals jobs that match the predicate' do
expect(queue[0]).to receive(:delete).and_return(true)
expect(described_class).to receive(:perform)
.with('Foo', [10, 20])
2021-03-11 19:13:27 +05:30
described_class.steal('Foo') { |job| job.args.second.first == 10 && job.args.second.second == 20 }
2020-07-28 23:09:34 +05:30
end
it 'does not steal jobs that do not match the predicate' do
expect(described_class).not_to receive(:perform)
expect(queue[0]).not_to receive(:delete)
described_class.steal('Foo') { |(arg1, _)| arg1 == 5 }
end
end
2017-09-10 17:25:29 +05:30
end
context 'when one of the jobs raises an error' do
let(:migration) { spy(:migration) }
let(:queue) do
2021-10-27 15:23:28 +05:30
[double(args: ['Foo', [10, 20]], klass: 'BackgroundMigrationWorker'),
double(args: ['Foo', [20, 30]], klass: 'BackgroundMigrationWorker')]
2017-09-10 17:25:29 +05:30
end
before do
stub_const("#{described_class}::Foo", migration)
allow(queue[0]).to receive(:delete).and_return(true)
allow(queue[1]).to receive(:delete).and_return(true)
end
it 'enqueues the migration again and re-raises the error' do
allow(migration).to receive(:perform).with(10, 20)
.and_raise(Exception, 'Migration error').once
expect(BackgroundMigrationWorker).to receive(:perform_async)
.with('Foo', [10, 20]).once
expect { described_class.steal('Foo') }.to raise_error(Exception)
end
end
end
2020-03-13 15:44:24 +05:30
context 'when there are scheduled jobs present', :redis do
2017-09-10 17:25:29 +05:30
it 'steals all jobs from the scheduled sets' do
Sidekiq::Testing.disable! do
BackgroundMigrationWorker.perform_in(10.minutes, 'Object')
expect(Sidekiq::ScheduledSet.new).to be_one
expect(described_class).to receive(:perform).with('Object', any_args)
described_class.steal('Object')
expect(Sidekiq::ScheduledSet.new).to be_none
end
end
end
2020-03-13 15:44:24 +05:30
context 'when there are enqueued and scheduled jobs present', :redis do
2017-09-10 17:25:29 +05:30
it 'steals from the scheduled sets queue first' do
Sidekiq::Testing.disable! do
expect(described_class).to receive(:perform)
.with('Object', [1]).ordered
expect(described_class).to receive(:perform)
.with('Object', [2]).ordered
BackgroundMigrationWorker.perform_async('Object', [2])
BackgroundMigrationWorker.perform_in(10.minutes, 'Object', [1])
described_class.steal('Object')
end
end
end
2019-03-02 22:35:43 +05:30
2020-03-13 15:44:24 +05:30
context 'when retry_dead_jobs is true', :redis do
2019-03-02 22:35:43 +05:30
let(:retry_queue) do
2021-10-27 15:23:28 +05:30
[double(args: ['Object', [3]], klass: 'BackgroundMigrationWorker', delete: true)]
2019-03-02 22:35:43 +05:30
end
2020-10-24 23:57:45 +05:30
2019-03-02 22:35:43 +05:30
let(:dead_queue) do
2021-10-27 15:23:28 +05:30
[double(args: ['Object', [4]], klass: 'BackgroundMigrationWorker', delete: true)]
2019-03-02 22:35:43 +05:30
end
before do
allow(Sidekiq::RetrySet).to receive(:new).and_return(retry_queue)
allow(Sidekiq::DeadSet).to receive(:new).and_return(dead_queue)
end
it 'steals from the dead and retry queue' do
Sidekiq::Testing.disable! do
expect(described_class).to receive(:perform)
.with('Object', [1]).ordered
expect(described_class).to receive(:perform)
.with('Object', [2]).ordered
expect(described_class).to receive(:perform)
.with('Object', [3]).ordered
expect(described_class).to receive(:perform)
.with('Object', [4]).ordered
BackgroundMigrationWorker.perform_async('Object', [2])
BackgroundMigrationWorker.perform_in(10.minutes, 'Object', [1])
described_class.steal('Object', retry_dead_jobs: true)
end
end
end
2017-09-10 17:25:29 +05:30
end
describe '.perform' do
let(:migration) { spy(:migration) }
before do
stub_const("#{described_class.name}::Foo", migration)
end
it 'performs a background migration' do
expect(migration).to receive(:perform).with(10, 20).once
described_class.perform('Foo', [10, 20])
end
2020-03-13 15:44:24 +05:30
context 'backward compatibility' do
it 'performs a background migration for fully-qualified job classes' do
expect(migration).to receive(:perform).with(10, 20).once
expect(Gitlab::ErrorTracking)
.to receive(:track_and_raise_for_dev_exception)
.with(instance_of(StandardError), hash_including(:class_name))
described_class.perform('Gitlab::BackgroundMigration::Foo', [10, 20])
end
end
end
describe '.remaining', :redis do
context 'when there are jobs remaining' do
before do
Sidekiq::Testing.disable! do
2021-10-27 15:23:28 +05:30
MergeWorker.perform_async('Foo')
MergeWorker.perform_in(10.minutes, 'Foo')
5.times do
BackgroundMigrationWorker.perform_async('Foo')
end
3.times do
BackgroundMigrationWorker.perform_in(10.minutes, 'Foo')
end
2020-03-13 15:44:24 +05:30
end
end
it 'returns the enqueued jobs plus the scheduled jobs' do
2021-10-27 15:23:28 +05:30
expect(described_class.remaining).to eq(8)
2020-03-13 15:44:24 +05:30
end
end
context 'when there are no jobs remaining' do
it 'returns zero' do
expect(described_class.remaining).to be_zero
end
end
2017-09-10 17:25:29 +05:30
end
2019-03-02 22:35:43 +05:30
2021-10-27 15:23:28 +05:30
describe '.exists?', :redis do
2019-03-02 22:35:43 +05:30
context 'when there are enqueued jobs present' do
before do
2021-10-27 15:23:28 +05:30
Sidekiq::Testing.disable! do
MergeWorker.perform_async('Bar')
BackgroundMigrationWorker.perform_async('Foo')
end
2019-03-02 22:35:43 +05:30
end
it 'returns true if specific job exists' do
expect(described_class.exists?('Foo')).to eq(true)
end
it 'returns false if specific job does not exist' do
expect(described_class.exists?('Bar')).to eq(false)
end
end
2021-10-27 15:23:28 +05:30
context 'when there are scheduled jobs present' do
2019-03-02 22:35:43 +05:30
before do
Sidekiq::Testing.disable! do
2021-10-27 15:23:28 +05:30
MergeWorker.perform_in(10.minutes, 'Bar')
2019-03-02 22:35:43 +05:30
BackgroundMigrationWorker.perform_in(10.minutes, 'Foo')
end
end
it 'returns true if specific job exists' do
expect(described_class.exists?('Foo')).to eq(true)
end
it 'returns false if specific job does not exist' do
expect(described_class.exists?('Bar')).to eq(false)
end
end
end
2019-07-31 22:56:46 +05:30
describe '.dead_jobs?' do
let(:queue) do
2021-10-27 15:23:28 +05:30
[
double(args: ['Foo', [10, 20]], klass: 'BackgroundMigrationWorker'),
double(args: ['Bar'], klass: 'MergeWorker')
]
2019-07-31 22:56:46 +05:30
end
context 'when there are dead jobs present' do
before do
allow(Sidekiq::DeadSet).to receive(:new).and_return(queue)
end
it 'returns true if specific job exists' do
expect(described_class.dead_jobs?('Foo')).to eq(true)
end
it 'returns false if specific job does not exist' do
expect(described_class.dead_jobs?('Bar')).to eq(false)
end
end
end
describe '.retrying_jobs?' do
let(:queue) do
2021-10-27 15:23:28 +05:30
[
double(args: ['Foo', [10, 20]], klass: 'BackgroundMigrationWorker'),
double(args: ['Bar'], klass: 'MergeWorker')
]
2019-07-31 22:56:46 +05:30
end
context 'when there are dead jobs present' do
before do
allow(Sidekiq::RetrySet).to receive(:new).and_return(queue)
end
it 'returns true if specific job exists' do
expect(described_class.retrying_jobs?('Foo')).to eq(true)
end
it 'returns false if specific job does not exist' do
expect(described_class.retrying_jobs?('Bar')).to eq(false)
end
end
end
2017-09-10 17:25:29 +05:30
end