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

90 lines
2.6 KiB
Ruby
Raw Normal View History

2016-06-02 11:05:42 +05:30
require 'spec_helper'
describe ProjectCacheWorker do
2018-11-08 19:23:39 +05:30
include ExclusiveLeaseHelpers
2017-08-17 22:00:37 +05:30
let(:worker) { described_class.new }
let(:project) { create(:project, :repository) }
let(:statistics) { project.statistics }
2018-11-08 19:23:39 +05:30
let(:lease_key) { "project_cache_worker:#{project.id}:update_statistics" }
let(:lease_timeout) { ProjectCacheWorker::LEASE_TIMEOUT }
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
it 'does nothing' do
allow_any_instance_of(Repository).to receive(:exists?).and_return(false)
2016-11-03 12:29:30 +05:30
2017-08-17 22:00:37 +05:30
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(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
it 'updates the project statistics' do
expect(worker).to receive(:update_statistics)
.with(kind_of(Project), %i(repository_size))
.and_call_original
2016-11-03 12:29:30 +05:30
2017-08-17 22:00:37 +05:30
worker.perform(project.id, [], %w(repository_size))
end
2016-06-02 11:05:42 +05:30
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
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
context 'when a lease could not be obtained' do
it 'does not update the repository size' 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
2017-08-17 22:00:37 +05:30
expect(statistics).not_to receive(:refresh!)
worker.update_statistics(project)
end
end
context 'when a lease could be obtained' do
it 'updates the project statistics' 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
expect(statistics).to receive(:refresh!)
.with(only: %i(repository_size))
.and_call_original
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
worker.update_statistics(project, %i(repository_size))
2016-11-03 12:29:30 +05:30
end
2016-06-02 11:05:42 +05:30
end
end
end