2016-03-27 09:17:49 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe ProjectCacheWorker do
|
2018-06-28 14:51:36 -04:00
|
|
|
include ExclusiveLeaseHelpers
|
|
|
|
|
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 }
|
2018-06-28 14:51:36 -04:00
|
|
|
let(:lease_key) { "project_cache_worker:#{project.id}:update_statistics" }
|
|
|
|
let(:lease_timeout) { ProjectCacheWorker::LEASE_TIMEOUT }
|
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
|
2018-07-04 07:04:58 -04:00
|
|
|
it 'does nothing' do
|
|
|
|
allow_any_instance_of(Repository).to receive(:exists?).and_return(false)
|
2016-10-25 10:01:24 -04:00
|
|
|
|
2018-07-04 07:04:58 -04:00
|
|
|
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(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
|
2016-11-22 11:58:10 -05:00
|
|
|
it 'updates the project statistics' do
|
2018-07-04 07:04:58 -04:00
|
|
|
expect(worker).to receive(:update_statistics)
|
|
|
|
.with(kind_of(Project), %i(repository_size))
|
|
|
|
.and_call_original
|
2016-10-20 06:59:39 -04:00
|
|
|
|
2018-07-04 07:04:58 -04: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
|
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
|
|
|
|
|
|
|
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
|
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
|
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
|
|
|
|
2018-07-04 07:04:58 -04:00
|
|
|
expect(statistics).not_to receive(:refresh!)
|
2016-11-18 08:04:18 -05:00
|
|
|
|
2018-07-04 07:04:58 -04: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
|
2018-06-28 14:51:36 -04:00
|
|
|
stub_exclusive_lease(lease_key, timeout: lease_timeout)
|
2016-11-18 08:04:18 -05:00
|
|
|
|
2018-07-04 07:04:58 -04:00
|
|
|
expect(statistics).to receive(:refresh!)
|
|
|
|
.with(only: %i(repository_size))
|
|
|
|
.and_call_original
|
2016-03-27 09:17:49 -04:00
|
|
|
|
2018-07-04 07:04:58 -04: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
|