2016-03-27 09:17:49 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe ProjectCacheWorker do
|
2016-11-18 08:04:18 -05:00
|
|
|
let(:worker) { described_class.new }
|
2017-03-27 16:42:08 -04:00
|
|
|
let(:project) { create(:project, :repository) }
|
2016-11-22 11:58:10 -05:00
|
|
|
let(:statistics) { project.statistics }
|
2016-03-27 09:17:49 -04:00
|
|
|
|
2016-11-18 08:04:18 -05:00
|
|
|
describe '#perform' do
|
|
|
|
before do
|
2017-06-21 09:48:12 -04:00
|
|
|
allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain)
|
|
|
|
.and_return(true)
|
2016-11-18 08:04:18 -05:00
|
|
|
end
|
2016-10-25 10:01:24 -04:00
|
|
|
|
2016-11-18 08:04:18 -05:00
|
|
|
context 'with a non-existing project' do
|
|
|
|
it 'does nothing' do
|
2016-11-22 11:58:10 -05:00
|
|
|
expect(worker).not_to receive(:update_statistics)
|
2016-10-25 10:01:24 -04:00
|
|
|
|
2016-11-18 08:04:18 -05:00
|
|
|
worker.perform(-1)
|
|
|
|
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
|
|
|
|
it 'does nothing' do
|
|
|
|
allow_any_instance_of(Repository).to receive(:exists?).and_return(false)
|
2016-10-25 10:01:24 -04:00
|
|
|
|
2016-11-22 11:58:10 -05:00
|
|
|
expect(worker).not_to receive(:update_statistics)
|
2016-10-25 10:01:24 -04:00
|
|
|
|
2016-11-18 08:04:18 -05:00
|
|
|
worker.perform(project.id)
|
|
|
|
end
|
2016-10-25 10:01:24 -04:00
|
|
|
end
|
|
|
|
|
2016-11-18 08:04:18 -05:00
|
|
|
context 'with an existing project' do
|
2016-11-22 11:58:10 -05:00
|
|
|
it 'updates the project statistics' do
|
|
|
|
expect(worker).to receive(:update_statistics)
|
|
|
|
.with(kind_of(Project), %i(repository_size))
|
|
|
|
.and_call_original
|
2016-10-20 06:59:39 -04:00
|
|
|
|
2016-11-22 11:58:10 -05:00
|
|
|
worker.perform(project.id, [], %w(repository_size))
|
2016-10-20 06:59:39 -04:00
|
|
|
end
|
|
|
|
|
2016-11-18 08:04:18 -05:00
|
|
|
it 'refreshes the method caches' do
|
2017-06-21 09:48:12 -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
|
|
|
|
2016-11-22 11:58:10 -05:00
|
|
|
worker.perform(project.id, %w(readme))
|
2016-10-20 06:59:39 -04:00
|
|
|
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)
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect_any_instance_of(Repository).to receive(:refresh_method_caches)
|
|
|
|
.with(%i(readme))
|
|
|
|
.and_call_original
|
2017-05-18 15:06:38 -04:00
|
|
|
worker.perform(project.id, %w(readme))
|
|
|
|
end
|
|
|
|
end
|
2016-03-27 09:17:49 -04:00
|
|
|
end
|
2016-11-18 08:04:18 -05:00
|
|
|
end
|
2016-03-27 09:17:49 -04:00
|
|
|
|
2016-11-22 11:58:10 -05:00
|
|
|
describe '#update_statistics' do
|
2016-11-18 08:04:18 -05:00
|
|
|
context 'when a lease could not be obtained' do
|
|
|
|
it 'does not update the repository size' do
|
2017-06-21 09:48:12 -04:00
|
|
|
allow(worker).to receive(:try_obtain_lease_for)
|
|
|
|
.with(project.id, :update_statistics)
|
|
|
|
.and_return(false)
|
2016-10-20 06:59:39 -04:00
|
|
|
|
2016-11-22 11:58:10 -05:00
|
|
|
expect(statistics).not_to receive(:refresh!)
|
2016-11-18 08:04:18 -05:00
|
|
|
|
2016-11-22 11:58:10 -05:00
|
|
|
worker.update_statistics(project)
|
2016-11-18 08:04:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a lease could be obtained' do
|
2016-11-22 11:58:10 -05:00
|
|
|
it 'updates the project statistics' do
|
2017-06-21 09:48:12 -04:00
|
|
|
allow(worker).to receive(:try_obtain_lease_for)
|
|
|
|
.with(project.id, :update_statistics)
|
|
|
|
.and_return(true)
|
2016-11-18 08:04:18 -05:00
|
|
|
|
2016-11-22 11:58:10 -05:00
|
|
|
expect(statistics).to receive(:refresh!)
|
|
|
|
.with(only: %i(repository_size))
|
|
|
|
.and_call_original
|
2016-03-27 09:17:49 -04:00
|
|
|
|
2016-11-22 11:58:10 -05:00
|
|
|
worker.update_statistics(project, %i(repository_size))
|
2016-10-20 06:59:39 -04:00
|
|
|
end
|
2016-03-27 09:17:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|