2019-03-30 03:15:48 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-02 11:12:36 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2018-01-04 10:49:15 -05:00
|
|
|
describe BackgroundMigrationWorker, :sidekiq, :clean_gitlab_redis_shared_state do
|
|
|
|
let(:worker) { described_class.new }
|
|
|
|
|
2018-07-19 11:16:47 -04:00
|
|
|
describe '.minimum_interval' do
|
|
|
|
it 'returns 2 minutes' do
|
|
|
|
expect(described_class.minimum_interval).to eq(2.minutes.to_i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-02 11:12:36 -04:00
|
|
|
describe '.perform' do
|
|
|
|
it 'performs a background migration' do
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(Gitlab::BackgroundMigration)
|
|
|
|
.to receive(:perform)
|
|
|
|
.with('Foo', [10, 20])
|
2017-06-02 11:12:36 -04:00
|
|
|
|
2018-01-04 10:49:15 -05:00
|
|
|
worker.perform('Foo', [10, 20])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'reschedules a migration if it was performed recently' do
|
|
|
|
expect(worker)
|
|
|
|
.to receive(:always_perform?)
|
|
|
|
.and_return(false)
|
|
|
|
|
|
|
|
worker.lease_for('Foo').try_obtain
|
|
|
|
|
|
|
|
expect(Gitlab::BackgroundMigration)
|
|
|
|
.not_to receive(:perform)
|
|
|
|
|
|
|
|
expect(described_class)
|
|
|
|
.to receive(:perform_in)
|
|
|
|
.with(a_kind_of(Numeric), 'Foo', [10, 20])
|
|
|
|
|
|
|
|
worker.perform('Foo', [10, 20])
|
2017-06-02 11:12:36 -04:00
|
|
|
end
|
2018-07-19 11:16:47 -04:00
|
|
|
|
|
|
|
it 'reschedules a migration if the database is not healthy' do
|
|
|
|
allow(worker)
|
|
|
|
.to receive(:always_perform?)
|
|
|
|
.and_return(false)
|
|
|
|
|
|
|
|
allow(worker)
|
|
|
|
.to receive(:healthy_database?)
|
|
|
|
.and_return(false)
|
|
|
|
|
|
|
|
expect(described_class)
|
|
|
|
.to receive(:perform_in)
|
|
|
|
.with(a_kind_of(Numeric), 'Foo', [10, 20])
|
|
|
|
|
|
|
|
worker.perform('Foo', [10, 20])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#healthy_database?' do
|
|
|
|
context 'using MySQL', :mysql do
|
|
|
|
it 'returns true' do
|
|
|
|
expect(worker.healthy_database?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'using PostgreSQL', :postgresql do
|
|
|
|
context 'when replication lag is too great' do
|
|
|
|
it 'returns false' do
|
|
|
|
allow(Postgresql::ReplicationSlot)
|
|
|
|
.to receive(:lag_too_great?)
|
|
|
|
.and_return(true)
|
|
|
|
|
|
|
|
expect(worker.healthy_database?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when replication lag is small enough' do
|
|
|
|
it 'returns true' do
|
|
|
|
allow(Postgresql::ReplicationSlot)
|
|
|
|
.to receive(:lag_too_great?)
|
|
|
|
.and_return(false)
|
|
|
|
|
|
|
|
expect(worker.healthy_database?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-02 11:12:36 -04:00
|
|
|
end
|
|
|
|
end
|