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

154 lines
4.7 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
2022-01-26 12:08:38 +05:30
let(:default_tracking_database) { described_class::DEFAULT_TRACKING_DATABASE }
let(:coordinator) { described_class::JobCoordinator.for_tracking_database(default_tracking_database) }
2021-12-11 22:18:48 +05:30
before do
allow(described_class).to receive(:coordinator_for_database)
2022-01-26 12:08:38 +05:30
.with(default_tracking_database)
2021-12-11 22:18:48 +05:30
.and_return(coordinator)
end
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
2021-12-11 22:18:48 +05:30
context 'when the queue contains unprocessed jobs' do
2017-09-10 17:25:29 +05:30
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)
2021-12-11 22:18:48 +05:30
.with(coordinator.queue)
2017-09-10 17:25:29 +05:30
.and_return(queue)
end
2021-12-11 22:18:48 +05:30
it 'uses the coordinator to steal jobs' do
expect(queue[0]).to receive(:delete).and_return(true)
2017-09-10 17:25:29 +05:30
2021-12-11 22:18:48 +05:30
expect(coordinator).to receive(:steal).with('Foo', retry_dead_jobs: false).and_call_original
expect(coordinator).to receive(:perform).with('Foo', [10, 20])
2020-07-28 23:09:34 +05:30
2021-12-11 22:18:48 +05:30
described_class.steal('Foo')
2017-09-10 17:25:29 +05:30
end
2021-12-11 22:18:48 +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)
2017-09-10 17:25:29 +05:30
2021-12-11 22:18:48 +05:30
expect(coordinator).to receive(:perform).with('Foo', [10, 20])
2017-09-10 17:25:29 +05:30
2021-12-11 22:18:48 +05:30
described_class.steal('Foo') { |job| job.args.second.first == 10 && job.args.second.second == 20 }
2017-09-10 17:25:29 +05:30
end
2021-12-11 22:18:48 +05:30
it 'does not steal jobs that do not match the predicate' do
expect(coordinator).not_to receive(:perform)
2017-09-10 17:25:29 +05:30
2021-12-11 22:18:48 +05:30
expect(queue[0]).not_to receive(:delete)
2017-09-10 17:25:29 +05:30
2021-12-11 22:18:48 +05:30
described_class.steal('Foo') { |(arg1, _)| arg1 == 5 }
2017-09-10 17:25:29 +05:30
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
2021-12-11 22:18:48 +05:30
expect(coordinator).to receive(:perform).with('Object', [1]).ordered
expect(coordinator).to receive(:perform).with('Object', [2]).ordered
expect(coordinator).to receive(:perform).with('Object', [3]).ordered
expect(coordinator).to receive(:perform).with('Object', [4]).ordered
2019-03-02 22:35:43 +05:30
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
2021-12-11 22:18:48 +05:30
it 'uses the coordinator to perform a background migration' do
expect(coordinator).to receive(:perform).with('Foo', [10, 20]).and_call_original
2017-09-10 17:25:29 +05:30
expect(migration).to receive(:perform).with(10, 20).once
described_class.perform('Foo', [10, 20])
end
2021-12-11 22:18:48 +05:30
end
2020-03-13 15:44:24 +05:30
2021-12-11 22:18:48 +05:30
describe '.exists?', :redis do
before do
Sidekiq::Testing.disable! do
MergeWorker.perform_async('Bar')
BackgroundMigrationWorker.perform_async('Foo')
2020-03-13 15:44:24 +05:30
end
end
2021-12-11 22:18:48 +05:30
it 'uses the coordinator to find if a job exists' do
expect(coordinator).to receive(:exists?).with('Foo', []).and_call_original
2020-03-13 15:44:24 +05:30
2021-12-11 22:18:48 +05:30
expect(described_class.exists?('Foo')).to eq(true)
2020-03-13 15:44:24 +05:30
end
2021-12-11 22:18:48 +05:30
it 'uses the coordinator to find a job does not exist' do
expect(coordinator).to receive(:exists?).with('Bar', []).and_call_original
expect(described_class.exists?('Bar')).to eq(false)
2020-03-13 15:44:24 +05:30
end
2017-09-10 17:25:29 +05:30
end
2019-03-02 22:35:43 +05:30
2021-12-11 22:18:48 +05:30
describe '.remaining', :redis do
before do
Sidekiq::Testing.disable! do
MergeWorker.perform_async('Foo')
MergeWorker.perform_in(10.minutes, 'Foo')
5.times do
2021-10-27 15:23:28 +05:30
BackgroundMigrationWorker.perform_async('Foo')
end
2021-12-11 22:18:48 +05:30
3.times do
2019-03-02 22:35:43 +05:30
BackgroundMigrationWorker.perform_in(10.minutes, 'Foo')
end
end
2019-07-31 22:56:46 +05:30
end
2021-12-11 22:18:48 +05:30
it 'uses the coordinator to find the number of remaining jobs' do
expect(coordinator).to receive(:remaining).and_call_original
2019-07-31 22:56:46 +05:30
2021-12-11 22:18:48 +05:30
expect(described_class.remaining).to eq(8)
2019-07-31 22:56:46 +05:30
end
end
2017-09-10 17:25:29 +05:30
end