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

105 lines
3.1 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
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) }
2019-07-31 22:56:46 +05:30
let(:lease_key) { ["project_cache_worker", project.id, *statistics.sort].join(":") }
2018-11-08 19:23:39 +05:30
let(:lease_timeout) { ProjectCacheWorker::LEASE_TIMEOUT }
2019-07-31 22:56:46 +05:30
let(:statistics) { [] }
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 '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
2019-07-31 22:56:46 +05:30
context 'with statistics' do
let(:statistics) { %w(repository_size) }
it 'updates the project statistics' do
expect(worker).to receive(:update_statistics)
.with(kind_of(Project), statistics)
.and_call_original
worker.perform(project.id, [], statistics)
end
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
2019-07-31 22:56:46 +05:30
let(:statistics) { %w(repository_size) }
2017-08-17 22:00:37 +05:30
context 'when a lease could not be obtained' do
2019-07-31 22:56:46 +05:30
it 'does not update the project statistics' 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
2019-07-31 22:56:46 +05:30
expect(Projects::UpdateStatisticsService).not_to receive(:new)
2017-08-17 22:00:37 +05:30
2019-07-31 22:56:46 +05:30
expect(UpdateProjectStatisticsWorker).not_to receive(:perform_in)
worker.update_statistics(project, statistics)
2017-08-17 22:00:37 +05:30
end
end
context 'when a lease could be obtained' do
2019-07-31 22:56:46 +05:30
it 'updates the project statistics twice' 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
2019-07-31 22:56:46 +05:30
expect(Projects::UpdateStatisticsService).to receive(:new)
.with(project, nil, statistics: statistics)
.and_call_original
.twice
expect(UpdateProjectStatisticsWorker).to receive(:perform_in)
.with(lease_timeout, project.id, statistics)
2017-08-17 22:00:37 +05:30
.and_call_original
2016-06-02 11:05:42 +05:30
2019-07-31 22:56:46 +05:30
worker.update_statistics(project, statistics)
2016-11-03 12:29:30 +05:30
end
2016-06-02 11:05:42 +05:30
end
end
end