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