debian-mirror-gitlab/spec/workers/repository_update_remote_mirror_worker_spec.rb

77 lines
2.8 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2019-12-04 20:38:33 +05:30
require 'spec_helper'
2018-10-15 14:42:47 +05:30
2020-07-28 23:09:34 +05:30
RSpec.describe RepositoryUpdateRemoteMirrorWorker, :clean_gitlab_redis_shared_state do
2021-02-22 17:27:13 +05:30
let_it_be(:remote_mirror) { create(:remote_mirror) }
2018-10-15 14:42:47 +05:30
2020-07-28 23:09:34 +05:30
let(:scheduled_time) { Time.current - 5.minutes }
2018-10-15 14:42:47 +05:30
around do |example|
2020-11-24 15:15:51 +05:30
freeze_time { example.run }
2018-10-15 14:42:47 +05:30
end
2019-10-12 21:52:04 +05:30
def expect_mirror_service_to_return(mirror, result, tries = 0)
expect_next_instance_of(Projects::UpdateRemoteMirrorService) do |service|
expect(service).to receive(:execute).with(mirror, tries).and_return(result)
end
end
2019-02-15 15:39:39 +05:30
2019-10-12 21:52:04 +05:30
describe '#perform' do
2021-02-22 17:27:13 +05:30
subject { described_class.new }
2019-10-12 21:52:04 +05:30
it 'calls out to the service to perform the update' do
expect_mirror_service_to_return(remote_mirror, status: :success)
2019-02-15 15:39:39 +05:30
2019-10-12 21:52:04 +05:30
subject.perform(remote_mirror.id, scheduled_time)
end
2018-10-15 14:42:47 +05:30
2019-10-12 21:52:04 +05:30
it 'does not do anything if the mirror was already updated' do
2020-10-24 23:57:45 +05:30
remote_mirror.update!(last_update_started_at: Time.current, update_status: :finished)
2018-10-15 14:42:47 +05:30
2019-10-12 21:52:04 +05:30
expect(Projects::UpdateRemoteMirrorService).not_to receive(:new)
2018-10-15 14:42:47 +05:30
2019-10-12 21:52:04 +05:30
subject.perform(remote_mirror.id, scheduled_time)
2018-10-15 14:42:47 +05:30
end
2019-10-12 21:52:04 +05:30
it 'schedules a retry when the mirror is marked for retrying' do
remote_mirror = create(:remote_mirror, update_status: :to_retry)
expect_mirror_service_to_return(remote_mirror, status: :error, message: 'Retry!')
2018-10-15 14:42:47 +05:30
2019-10-12 21:52:04 +05:30
expect(described_class)
.to receive(:perform_in)
.with(remote_mirror.backoff_delay, remote_mirror.id, scheduled_time, 1)
2018-10-15 14:42:47 +05:30
2019-10-12 21:52:04 +05:30
subject.perform(remote_mirror.id, scheduled_time)
2018-10-15 14:42:47 +05:30
end
2019-10-12 21:52:04 +05:30
it 'clears the lease if there was an unexpected exception' do
expect_next_instance_of(Projects::UpdateRemoteMirrorService) do |service|
expect(service).to receive(:execute).with(remote_mirror, 1).and_raise('Unexpected!')
2018-10-15 14:42:47 +05:30
end
2020-07-28 23:09:34 +05:30
expect { subject.perform(remote_mirror.id, Time.current, 1) }.to raise_error('Unexpected!')
2018-10-15 14:42:47 +05:30
2019-10-12 21:52:04 +05:30
lease = Gitlab::ExclusiveLease.new("#{described_class.name}:#{remote_mirror.id}", timeout: 1.second)
2018-10-15 14:42:47 +05:30
2019-10-12 21:52:04 +05:30
expect(lease.try_obtain).not_to be_nil
end
2018-10-15 14:42:47 +05:30
2019-10-12 21:52:04 +05:30
it 'retries 3 times for the worker to finish before rescheduling' do
expect(subject).to receive(:in_lock)
.with("#{described_class.name}:#{remote_mirror.id}",
retries: 3,
ttl: remote_mirror.max_runtime,
sleep_sec: described_class::LOCK_WAIT_TIME)
.and_raise(Gitlab::ExclusiveLeaseHelpers::FailedToObtainLockError)
expect(described_class).to receive(:perform_in)
.with(remote_mirror.backoff_delay, remote_mirror.id, scheduled_time, 0)
subject.perform(remote_mirror.id, scheduled_time)
2018-10-15 14:42:47 +05:30
end
end
2021-02-22 17:27:13 +05:30
include_examples 'an idempotent worker' do
let(:job_args) { [remote_mirror.id, scheduled_time] }
end
2018-10-15 14:42:47 +05:30
end