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

142 lines
4.4 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2016-06-02 11:05:42 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe ProjectCacheWorker do
2018-11-08 19:23:39 +05:30
include ExclusiveLeaseHelpers
2021-02-22 17:27:13 +05:30
let_it_be(:project) { create(:project, :repository) }
2017-08-17 22:00:37 +05:30
let(:worker) { described_class.new }
2019-07-31 22:56:46 +05:30
let(:lease_key) { ["project_cache_worker", project.id, *statistics.sort].join(":") }
2018-11-08 19:23:39 +05:30
let(:lease_timeout) { ProjectCacheWorker::LEASE_TIMEOUT }
2019-07-31 22:56:46 +05:30
let(:statistics) { [] }
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
describe '#perform' do
before do
2018-11-08 19:23:39 +05:30
stub_exclusive_lease(lease_key, timeout: lease_timeout)
2017-08-17 22:00:37 +05:30
end
2016-11-03 12:29:30 +05:30
2017-08-17 22:00:37 +05:30
context 'with a non-existing project' do
it 'does nothing' do
expect(worker).not_to receive(:update_statistics)
2016-11-03 12:29:30 +05:30
2017-08-17 22:00:37 +05:30
worker.perform(-1)
end
2016-11-03 12:29:30 +05:30
end
2017-08-17 22:00:37 +05:30
context 'with an existing project without a repository' do
2019-09-04 21:01:54 +05:30
it 'updates statistics but does not refresh the method cashes' do
2017-08-17 22:00:37 +05:30
allow_any_instance_of(Repository).to receive(:exists?).and_return(false)
2016-11-03 12:29:30 +05:30
2019-09-04 21:01:54 +05:30
expect(worker).to receive(:update_statistics)
expect_any_instance_of(Repository).not_to receive(:refresh_method_caches)
2016-11-03 12:29:30 +05:30
2017-08-17 22:00:37 +05:30
worker.perform(project.id)
end
2016-11-03 12:29:30 +05:30
end
2017-08-17 22:00:37 +05:30
context 'with an existing project' do
2019-09-30 21:07:59 +05:30
before do
lease_key = "namespace:namespaces_root_statistics:#{project.namespace_id}"
stub_exclusive_lease_taken(lease_key, timeout: Namespace::AggregationSchedule::DEFAULT_LEASE_TIMEOUT)
end
2017-08-17 22:00:37 +05:30
it 'refreshes the method caches' do
2017-09-10 17:25:29 +05:30
expect_any_instance_of(Repository).to receive(:refresh_method_caches)
.with(%i(readme))
.and_call_original
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
worker.perform(project.id, %w(readme))
2016-11-03 12:29:30 +05:30
end
2019-10-12 21:52:04 +05:30
context 'with statistics disabled' do
let(:statistics) { [] }
it 'does not update the project statistics' do
expect(worker).not_to receive(:update_statistics)
worker.perform(project.id, [], [], false)
end
end
2019-07-31 22:56:46 +05:30
context 'with statistics' do
let(:statistics) { %w(repository_size) }
it 'updates the project statistics' do
expect(worker).to receive(:update_statistics)
.with(kind_of(Project), statistics)
.and_call_original
worker.perform(project.id, [], statistics)
end
end
2017-08-17 22:00:37 +05:30
context 'with plain readme' do
it 'refreshes the method caches' do
allow(MarkupHelper).to receive(:gitlab_markdown?).and_return(false)
allow(MarkupHelper).to receive(:plain?).and_return(true)
2016-11-03 12:29:30 +05:30
2017-09-10 17:25:29 +05:30
expect_any_instance_of(Repository).to receive(:refresh_method_caches)
.with(%i(readme))
.and_call_original
2017-08-17 22:00:37 +05:30
worker.perform(project.id, %w(readme))
end
2016-11-03 12:29:30 +05:30
end
2016-06-02 11:05:42 +05:30
end
2017-08-17 22:00:37 +05:30
end
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
describe '#update_statistics' do
2019-07-31 22:56:46 +05:30
let(:statistics) { %w(repository_size) }
2017-08-17 22:00:37 +05:30
context 'when a lease could not be obtained' do
2019-07-31 22:56:46 +05:30
it 'does not update the project statistics' do
2018-11-08 19:23:39 +05:30
stub_exclusive_lease_taken(lease_key, timeout: lease_timeout)
2016-11-03 12:29:30 +05:30
2019-07-31 22:56:46 +05:30
expect(Projects::UpdateStatisticsService).not_to receive(:new)
2017-08-17 22:00:37 +05:30
2019-07-31 22:56:46 +05:30
expect(UpdateProjectStatisticsWorker).not_to receive(:perform_in)
2019-09-30 21:07:59 +05:30
expect(Namespaces::ScheduleAggregationWorker)
.not_to receive(:perform_async)
.with(project.namespace_id)
2019-07-31 22:56:46 +05:30
worker.update_statistics(project, statistics)
2017-08-17 22:00:37 +05:30
end
end
context 'when a lease could be obtained' do
2019-12-26 22:10:19 +05:30
it 'updates the project statistics twice', :sidekiq_might_not_need_inline do
2018-11-08 19:23:39 +05:30
stub_exclusive_lease(lease_key, timeout: lease_timeout)
2017-08-17 22:00:37 +05:30
2019-07-31 22:56:46 +05:30
expect(Projects::UpdateStatisticsService).to receive(:new)
.with(project, nil, statistics: statistics)
.and_call_original
.twice
expect(UpdateProjectStatisticsWorker).to receive(:perform_in)
.with(lease_timeout, project.id, statistics)
2017-08-17 22:00:37 +05:30
.and_call_original
2016-06-02 11:05:42 +05:30
2019-09-30 21:07:59 +05:30
expect(Namespaces::ScheduleAggregationWorker)
.to receive(:perform_async)
.with(project.namespace_id)
.twice
2019-07-31 22:56:46 +05:30
worker.update_statistics(project, statistics)
2016-11-03 12:29:30 +05:30
end
2016-06-02 11:05:42 +05:30
end
end
2021-02-22 17:27:13 +05:30
it_behaves_like 'an idempotent worker' do
let(:job_args) { [project.id, %w(readme), %w(repository_size)] }
it 'calls Projects::UpdateStatisticsService service twice', :clean_gitlab_redis_shared_state do
expect(Projects::UpdateStatisticsService).to receive(:new).once.and_return(double(execute: true))
expect(UpdateProjectStatisticsWorker).to receive(:perform_in).once
subject
end
end
2016-06-02 11:05:42 +05:30
end