2019-07-25 01:21:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-02 11:12:36 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::BackgroundMigration do
|
2017-07-11 09:42:00 -04:00
|
|
|
describe '.queue' do
|
|
|
|
it 'returns background migration worker queue' do
|
|
|
|
expect(described_class.queue)
|
|
|
|
.to eq BackgroundMigrationWorker.sidekiq_options['queue']
|
|
|
|
end
|
|
|
|
end
|
2017-06-02 11:12:36 -04:00
|
|
|
|
2017-07-11 09:42:00 -04:00
|
|
|
describe '.steal' do
|
|
|
|
context 'when there are enqueued jobs present' do
|
2017-07-14 09:40:51 -04:00
|
|
|
let(:queue) do
|
|
|
|
[double(args: ['Foo', [10, 20]], queue: described_class.queue)]
|
|
|
|
end
|
2017-06-02 11:12:36 -04:00
|
|
|
|
2017-07-11 09:42:00 -04:00
|
|
|
before do
|
|
|
|
allow(Sidekiq::Queue).to receive(:new)
|
|
|
|
.with(described_class.queue)
|
|
|
|
.and_return(queue)
|
|
|
|
end
|
2017-06-02 11:12:36 -04:00
|
|
|
|
2017-07-14 09:40:51 -04:00
|
|
|
context 'when queue contains unprocessed jobs' do
|
|
|
|
it 'steals jobs from a queue' do
|
|
|
|
expect(queue[0]).to receive(:delete).and_return(true)
|
2017-06-02 11:12:36 -04:00
|
|
|
|
2017-07-17 04:16:42 -04:00
|
|
|
expect(described_class).to receive(:perform)
|
2017-07-18 05:55:43 -04:00
|
|
|
.with('Foo', [10, 20])
|
2017-06-02 11:12:36 -04:00
|
|
|
|
2017-07-14 09:40:51 -04:00
|
|
|
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)
|
|
|
|
|
2017-07-17 04:16:42 -04:00
|
|
|
expect(described_class).not_to receive(:perform)
|
2017-06-02 11:12:36 -04:00
|
|
|
|
2017-07-14 09:40:51 -04:00
|
|
|
described_class.steal('Foo')
|
|
|
|
end
|
2017-07-14 06:48:11 -04:00
|
|
|
|
2017-07-14 09:40:51 -04:00
|
|
|
it 'does not steal jobs for a different migration' do
|
|
|
|
expect(described_class).not_to receive(:perform)
|
2017-07-14 06:48:11 -04:00
|
|
|
|
2017-07-14 09:40:51 -04:00
|
|
|
expect(queue[0]).not_to receive(:delete)
|
|
|
|
|
|
|
|
described_class.steal('Bar')
|
|
|
|
end
|
2017-07-14 06:48:11 -04:00
|
|
|
end
|
|
|
|
|
2017-07-14 09:40:51 -04:00
|
|
|
context 'when one of the jobs raises an error' do
|
|
|
|
let(:migration) { spy(:migration) }
|
|
|
|
|
|
|
|
let(:queue) do
|
|
|
|
[double(args: ['Foo', [10, 20]], queue: described_class.queue),
|
|
|
|
double(args: ['Foo', [20, 30]], queue: described_class.queue)]
|
|
|
|
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
|
2017-06-02 11:12:36 -04:00
|
|
|
|
2017-07-17 08:02:12 -04:00
|
|
|
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
|
2017-07-17 04:16:42 -04:00
|
|
|
|
2017-07-17 08:02:12 -04:00
|
|
|
expect(BackgroundMigrationWorker).to receive(:perform_async)
|
|
|
|
.with('Foo', [10, 20]).once
|
2017-07-17 04:16:42 -04:00
|
|
|
|
2017-07-17 08:02:12 -04:00
|
|
|
expect { described_class.steal('Foo') }.to raise_error(Exception)
|
2017-07-14 09:40:51 -04:00
|
|
|
end
|
2017-07-11 09:42:00 -04:00
|
|
|
end
|
2017-06-02 11:12:36 -04:00
|
|
|
end
|
2017-07-13 05:44:52 -04:00
|
|
|
|
|
|
|
context 'when there are scheduled jobs present', :sidekiq, :redis do
|
2017-07-13 06:00:23 -04:00
|
|
|
it 'steals all jobs from the scheduled sets' do
|
2017-07-13 05:44:52 -04:00
|
|
|
Sidekiq::Testing.disable! do
|
|
|
|
BackgroundMigrationWorker.perform_in(10.minutes, 'Object')
|
2017-07-13 06:00:23 -04:00
|
|
|
|
2017-07-13 05:44:52 -04:00
|
|
|
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
|
2017-07-13 06:00:23 -04:00
|
|
|
|
|
|
|
context 'when there are enqueued and scheduled jobs present', :sidekiq, :redis do
|
|
|
|
it 'steals from the scheduled sets queue first' do
|
|
|
|
Sidekiq::Testing.disable! do
|
|
|
|
expect(described_class).to receive(:perform)
|
2017-07-18 05:55:43 -04:00
|
|
|
.with('Object', [1]).ordered
|
2017-07-13 06:00:23 -04:00
|
|
|
expect(described_class).to receive(:perform)
|
2017-07-18 05:55:43 -04:00
|
|
|
.with('Object', [2]).ordered
|
2017-07-13 06:00:23 -04:00
|
|
|
|
|
|
|
BackgroundMigrationWorker.perform_async('Object', [2])
|
|
|
|
BackgroundMigrationWorker.perform_in(10.minutes, 'Object', [1])
|
|
|
|
|
|
|
|
described_class.steal('Object')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-01-30 04:03:23 -05:00
|
|
|
|
|
|
|
context 'when retry_dead_jobs is true', :sidekiq, :redis do
|
|
|
|
let(:retry_queue) do
|
|
|
|
[double(args: ['Object', [3]], queue: described_class.queue, delete: true)]
|
|
|
|
end
|
|
|
|
let(:dead_queue) do
|
|
|
|
[double(args: ['Object', [4]], queue: described_class.queue, delete: true)]
|
|
|
|
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-06-02 11:12:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.perform' do
|
2017-07-17 04:16:42 -04:00
|
|
|
let(:migration) { spy(:migration) }
|
2017-06-02 11:12:36 -04:00
|
|
|
|
2017-07-17 04:16:42 -04:00
|
|
|
before do
|
|
|
|
stub_const("#{described_class.name}::Foo", migration)
|
|
|
|
end
|
2017-06-02 11:12:36 -04:00
|
|
|
|
2017-07-17 08:02:12 -04:00
|
|
|
it 'performs a background migration' do
|
|
|
|
expect(migration).to receive(:perform).with(10, 20).once
|
2017-07-17 04:16:42 -04:00
|
|
|
|
2017-07-17 08:02:12 -04:00
|
|
|
described_class.perform('Foo', [10, 20])
|
2017-06-02 11:12:36 -04:00
|
|
|
end
|
|
|
|
end
|
2018-12-19 03:34:34 -05:00
|
|
|
|
|
|
|
describe '.exists?' do
|
|
|
|
context 'when there are enqueued jobs present' do
|
|
|
|
let(:queue) do
|
|
|
|
[double(args: ['Foo', [10, 20]], queue: described_class.queue)]
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Sidekiq::Queue).to receive(:new)
|
|
|
|
.with(described_class.queue)
|
|
|
|
.and_return(queue)
|
|
|
|
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
|
|
|
|
|
|
|
|
context 'when there are scheduled jobs present', :sidekiq, :redis do
|
|
|
|
before do
|
|
|
|
Sidekiq::Testing.disable! do
|
|
|
|
BackgroundMigrationWorker.perform_in(10.minutes, 'Foo')
|
|
|
|
|
|
|
|
expect(Sidekiq::ScheduledSet.new).to be_one
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Sidekiq::ScheduledSet.new.clear
|
|
|
|
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-04-08 18:47:23 -04:00
|
|
|
|
|
|
|
describe '.dead_jobs?' do
|
|
|
|
let(:queue) do
|
|
|
|
[double(args: ['Foo', [10, 20]], queue: described_class.queue)]
|
|
|
|
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
|
|
|
|
[double(args: ['Foo', [10, 20]], queue: described_class.queue)]
|
|
|
|
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-06-02 11:12:36 -04:00
|
|
|
end
|