2019-03-30 03:15:48 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-27 09:17:49 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe ProjectCacheWorker do
|
2018-06-28 14:51:36 -04:00
|
|
|
include ExclusiveLeaseHelpers
|
|
|
|
|
2020-12-01 07:09:17 -05:00
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
|
|
|
|
2016-11-18 08:04:18 -05:00
|
|
|
let(:worker) { described_class.new }
|
2019-04-04 11:17:52 -04:00
|
|
|
let(:lease_key) { ["project_cache_worker", project.id, *statistics.sort].join(":") }
|
2018-06-28 14:51:36 -04:00
|
|
|
let(:lease_timeout) { ProjectCacheWorker::LEASE_TIMEOUT }
|
2019-04-01 02:11:08 -04:00
|
|
|
let(:statistics) { [] }
|
2016-03-27 09:17:49 -04:00
|
|
|
|
2018-06-22 09:31:04 -04:00
|
|
|
describe '#perform' do
|
2018-07-04 07:04:58 -04:00
|
|
|
before do
|
|
|
|
stub_exclusive_lease(lease_key, timeout: lease_timeout)
|
|
|
|
end
|
2018-06-22 09:31:04 -04:00
|
|
|
|
2018-07-04 07:04:58 -04:00
|
|
|
context 'with a non-existing project' do
|
|
|
|
it 'does nothing' do
|
|
|
|
expect(worker).not_to receive(:update_statistics)
|
2016-10-25 10:01:24 -04:00
|
|
|
|
2018-07-04 07:04:58 -04:00
|
|
|
worker.perform(-1)
|
2016-11-18 08:04:18 -05:00
|
|
|
end
|
2016-10-25 10:01:24 -04:00
|
|
|
end
|
|
|
|
|
2016-11-18 08:04:18 -05:00
|
|
|
context 'with an existing project without a repository' do
|
2019-02-13 17:38:11 -05:00
|
|
|
it 'updates statistics but does not refresh the method cashes' do
|
2018-07-04 07:04:58 -04:00
|
|
|
allow_any_instance_of(Repository).to receive(:exists?).and_return(false)
|
2016-10-25 10:01:24 -04:00
|
|
|
|
2019-02-13 17:38:11 -05:00
|
|
|
expect(worker).to receive(:update_statistics)
|
|
|
|
expect_any_instance_of(Repository).not_to receive(:refresh_method_caches)
|
2016-10-25 10:01:24 -04:00
|
|
|
|
2018-07-04 07:04:58 -04:00
|
|
|
worker.perform(project.id)
|
2016-11-18 08:04:18 -05:00
|
|
|
end
|
2016-10-25 10:01:24 -04:00
|
|
|
end
|
|
|
|
|
2016-11-18 08:04:18 -05:00
|
|
|
context 'with an existing project' do
|
2019-07-08 11:06:05 -04:00
|
|
|
before do
|
|
|
|
lease_key = "namespace:namespaces_root_statistics:#{project.namespace_id}"
|
|
|
|
stub_exclusive_lease_taken(lease_key, timeout: Namespace::AggregationSchedule::DEFAULT_LEASE_TIMEOUT)
|
|
|
|
end
|
|
|
|
|
2016-11-18 08:04:18 -05:00
|
|
|
it 'refreshes the method caches' do
|
2018-07-04 07:04:58 -04:00
|
|
|
expect_any_instance_of(Repository).to receive(:refresh_method_caches)
|
|
|
|
.with(%i(readme))
|
|
|
|
.and_call_original
|
2016-10-20 06:59:39 -04:00
|
|
|
|
2018-07-04 07:04:58 -04:00
|
|
|
worker.perform(project.id, %w(readme))
|
2016-10-20 06:59:39 -04:00
|
|
|
end
|
2017-05-18 15:06:38 -04:00
|
|
|
|
2019-08-16 15:53:56 -04:00
|
|
|
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-04-01 02:11:08 -04:00
|
|
|
context 'with statistics' do
|
|
|
|
let(:statistics) { %w(repository_size) }
|
|
|
|
|
|
|
|
it 'updates the project statistics' do
|
|
|
|
expect(worker).to receive(:update_statistics)
|
2019-04-05 09:07:09 -04:00
|
|
|
.with(kind_of(Project), statistics)
|
2019-04-01 02:11:08 -04:00
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
worker.perform(project.id, [], statistics)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-18 15:06:38 -04:00
|
|
|
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)
|
|
|
|
|
2018-07-04 07:04:58 -04:00
|
|
|
expect_any_instance_of(Repository).to receive(:refresh_method_caches)
|
|
|
|
.with(%i(readme))
|
|
|
|
.and_call_original
|
|
|
|
worker.perform(project.id, %w(readme))
|
2017-05-18 15:06:38 -04:00
|
|
|
end
|
|
|
|
end
|
2016-03-27 09:17:49 -04:00
|
|
|
end
|
2018-07-04 07:04:58 -04:00
|
|
|
end
|
2016-03-27 09:17:49 -04:00
|
|
|
|
2018-07-04 07:04:58 -04:00
|
|
|
describe '#update_statistics' do
|
2019-04-01 02:11:08 -04:00
|
|
|
let(:statistics) { %w(repository_size) }
|
|
|
|
|
2016-11-18 08:04:18 -05:00
|
|
|
context 'when a lease could not be obtained' do
|
2019-04-05 09:07:09 -04:00
|
|
|
it 'does not update the project statistics' do
|
2018-06-28 14:51:36 -04:00
|
|
|
stub_exclusive_lease_taken(lease_key, timeout: lease_timeout)
|
2016-10-20 06:59:39 -04:00
|
|
|
|
2019-04-05 09:07:09 -04:00
|
|
|
expect(Projects::UpdateStatisticsService).not_to receive(:new)
|
|
|
|
|
2019-04-01 02:11:08 -04:00
|
|
|
expect(UpdateProjectStatisticsWorker).not_to receive(:perform_in)
|
2016-11-18 08:04:18 -05:00
|
|
|
|
2019-07-08 11:06:05 -04:00
|
|
|
expect(Namespaces::ScheduleAggregationWorker)
|
|
|
|
.not_to receive(:perform_async)
|
|
|
|
.with(project.namespace_id)
|
|
|
|
|
2019-04-05 09:07:09 -04:00
|
|
|
worker.update_statistics(project, statistics)
|
2016-11-18 08:04:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a lease could be obtained' do
|
2019-10-23 05:06:03 -04:00
|
|
|
it 'updates the project statistics twice', :sidekiq_might_not_need_inline do
|
2018-06-28 14:51:36 -04:00
|
|
|
stub_exclusive_lease(lease_key, timeout: lease_timeout)
|
2016-11-18 08:04:18 -05:00
|
|
|
|
2019-04-05 09:07:09 -04:00
|
|
|
expect(Projects::UpdateStatisticsService).to receive(:new)
|
|
|
|
.with(project, nil, statistics: statistics)
|
2019-04-04 11:18:17 -04:00
|
|
|
.and_call_original
|
2019-04-05 09:07:09 -04:00
|
|
|
.twice
|
2019-04-04 11:18:17 -04:00
|
|
|
|
2019-04-01 02:11:08 -04:00
|
|
|
expect(UpdateProjectStatisticsWorker).to receive(:perform_in)
|
2022-07-26 05:09:02 -04:00
|
|
|
.with(lease_timeout, lease_key, project.id, statistics)
|
2018-07-04 07:04:58 -04:00
|
|
|
.and_call_original
|
2016-03-27 09:17:49 -04:00
|
|
|
|
2019-07-08 11:06:05 -04:00
|
|
|
expect(Namespaces::ScheduleAggregationWorker)
|
|
|
|
.to receive(:perform_async)
|
|
|
|
.with(project.namespace_id)
|
|
|
|
.twice
|
|
|
|
|
2019-04-05 09:07:09 -04:00
|
|
|
worker.update_statistics(project, statistics)
|
2016-10-20 06:59:39 -04:00
|
|
|
end
|
2016-03-27 09:17:49 -04:00
|
|
|
end
|
|
|
|
end
|
2020-12-01 07:09:17 -05:00
|
|
|
|
|
|
|
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-03-27 09:17:49 -04:00
|
|
|
end
|