2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-14 10:18:52 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Projects::HousekeepingService do
|
2017-05-01 11:13:33 -04:00
|
|
|
subject { described_class.new(project) }
|
2019-10-18 07:11:44 -04:00
|
|
|
|
2018-04-25 19:18:22 -04:00
|
|
|
set(:project) { create(:project, :repository) }
|
2016-03-14 10:18:52 -04:00
|
|
|
|
2016-09-19 00:21:43 -04:00
|
|
|
before do
|
|
|
|
project.reset_pushes_since_gc
|
|
|
|
end
|
|
|
|
|
2016-09-13 12:28:45 -04:00
|
|
|
after do
|
|
|
|
project.reset_pushes_since_gc
|
|
|
|
end
|
2016-03-14 10:18:52 -04:00
|
|
|
|
2016-09-13 12:28:45 -04:00
|
|
|
describe '#execute' do
|
2016-03-14 10:18:52 -04:00
|
|
|
it 'enqueues a sidekiq job' do
|
2016-10-27 08:59:52 -04:00
|
|
|
expect(subject).to receive(:try_obtain_lease).and_return(:the_uuid)
|
|
|
|
expect(subject).to receive(:lease_key).and_return(:the_lease_key)
|
2018-04-25 19:18:22 -04:00
|
|
|
expect(subject).to receive(:task).and_return(:incremental_repack)
|
|
|
|
expect(GitGarbageCollectWorker).to receive(:perform_async).with(project.id, :incremental_repack, :the_lease_key, :the_uuid).and_call_original
|
2016-03-14 10:18:52 -04:00
|
|
|
|
2018-04-25 19:18:22 -04:00
|
|
|
Sidekiq::Testing.fake! do
|
|
|
|
expect { subject.execute }.to change(GitGarbageCollectWorker.jobs, :size).by(1)
|
|
|
|
end
|
2016-03-14 10:18:52 -04:00
|
|
|
end
|
|
|
|
|
2017-08-23 09:51:21 -04:00
|
|
|
it 'yields the block if given' do
|
2017-08-24 05:36:16 -04:00
|
|
|
expect do |block|
|
|
|
|
subject.execute(&block)
|
2017-08-23 09:51:21 -04:00
|
|
|
end.to yield_with_no_args
|
|
|
|
end
|
|
|
|
|
2018-04-25 19:18:22 -04:00
|
|
|
it 'resets counter after execution' do
|
|
|
|
expect(subject).to receive(:try_obtain_lease).and_return(:the_uuid)
|
|
|
|
allow(subject).to receive(:gc_period).and_return(1)
|
|
|
|
project.increment_pushes_since_gc
|
|
|
|
|
2018-07-23 00:34:54 -04:00
|
|
|
perform_enqueued_jobs do
|
2018-04-25 19:18:22 -04:00
|
|
|
expect { subject.execute }.to change { project.pushes_since_gc }.to(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-12 05:58:06 -04:00
|
|
|
context 'when no lease can be obtained' do
|
2017-08-10 18:31:42 -04:00
|
|
|
before do
|
2016-07-12 05:58:06 -04:00
|
|
|
expect(subject).to receive(:try_obtain_lease).and_return(false)
|
|
|
|
end
|
2016-03-14 10:18:52 -04:00
|
|
|
|
2016-07-12 05:58:06 -04:00
|
|
|
it 'does not enqueue a job' do
|
|
|
|
expect(GitGarbageCollectWorker).not_to receive(:perform_async)
|
|
|
|
|
|
|
|
expect { subject.execute }.to raise_error(Projects::HousekeepingService::LeaseTaken)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not reset pushes_since_gc' do
|
|
|
|
expect do
|
|
|
|
expect { subject.execute }.to raise_error(Projects::HousekeepingService::LeaseTaken)
|
2016-09-13 12:28:45 -04:00
|
|
|
end.not_to change { project.pushes_since_gc }
|
2016-07-12 05:58:06 -04:00
|
|
|
end
|
2017-08-23 09:51:21 -04:00
|
|
|
|
|
|
|
it 'does not yield' do
|
2017-08-24 05:36:16 -04:00
|
|
|
expect do |block|
|
|
|
|
expect { subject.execute(&block) }
|
2017-08-23 09:51:21 -04:00
|
|
|
.to raise_error(Projects::HousekeepingService::LeaseTaken)
|
|
|
|
end.not_to yield_with_no_args
|
|
|
|
end
|
2016-03-14 10:18:52 -04:00
|
|
|
end
|
2018-04-25 19:18:22 -04:00
|
|
|
|
|
|
|
context 'task type' do
|
|
|
|
it 'goes through all three housekeeping tasks, executing only the highest task when there is overlap' do
|
|
|
|
allow(subject).to receive(:try_obtain_lease).and_return(:the_uuid)
|
|
|
|
allow(subject).to receive(:lease_key).and_return(:the_lease_key)
|
|
|
|
|
|
|
|
# At push 200
|
|
|
|
expect(GitGarbageCollectWorker).to receive(:perform_async).with(project.id, :gc, :the_lease_key, :the_uuid)
|
|
|
|
.exactly(1).times
|
|
|
|
# At push 50, 100, 150
|
|
|
|
expect(GitGarbageCollectWorker).to receive(:perform_async).with(project.id, :full_repack, :the_lease_key, :the_uuid)
|
|
|
|
.exactly(3).times
|
|
|
|
# At push 10, 20, ... (except those above)
|
|
|
|
expect(GitGarbageCollectWorker).to receive(:perform_async).with(project.id, :incremental_repack, :the_lease_key, :the_uuid)
|
|
|
|
.exactly(16).times
|
2019-05-02 17:41:05 -04:00
|
|
|
# At push 6, 12, 18, ... (except those above)
|
|
|
|
expect(GitGarbageCollectWorker).to receive(:perform_async).with(project.id, :pack_refs, :the_lease_key, :the_uuid)
|
|
|
|
.exactly(27).times
|
2018-04-25 19:18:22 -04:00
|
|
|
|
|
|
|
201.times do
|
|
|
|
subject.increment!
|
|
|
|
subject.execute if subject.needed?
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(project.pushes_since_gc).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
2019-03-31 09:38:23 -04:00
|
|
|
|
|
|
|
it 'runs the task specifically requested' do
|
|
|
|
housekeeping = described_class.new(project, :gc)
|
|
|
|
|
|
|
|
allow(housekeeping).to receive(:try_obtain_lease).and_return(:gc_uuid)
|
|
|
|
allow(housekeeping).to receive(:lease_key).and_return(:gc_lease_key)
|
|
|
|
|
|
|
|
expect(GitGarbageCollectWorker).to receive(:perform_async).with(project.id, :gc, :gc_lease_key, :gc_uuid).twice
|
|
|
|
|
|
|
|
2.times do
|
|
|
|
housekeeping.execute
|
|
|
|
end
|
|
|
|
end
|
2016-03-14 10:18:52 -04:00
|
|
|
end
|
|
|
|
|
2016-09-13 12:28:45 -04:00
|
|
|
describe '#needed?' do
|
2016-03-14 10:18:52 -04:00
|
|
|
it 'when the count is low enough' do
|
|
|
|
expect(subject.needed?).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'when the count is high enough' do
|
|
|
|
allow(project).to receive(:pushes_since_gc).and_return(10)
|
|
|
|
expect(subject.needed?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2016-03-14 11:49:24 -04:00
|
|
|
|
2016-09-13 12:28:45 -04:00
|
|
|
describe '#increment!' do
|
2016-03-14 11:49:24 -04:00
|
|
|
it 'increments the pushes_since_gc counter' do
|
2018-04-25 19:18:22 -04:00
|
|
|
expect { subject.increment! }.to change { project.pushes_since_gc }.by(1)
|
2016-07-12 05:58:06 -04:00
|
|
|
end
|
2016-03-14 11:49:24 -04:00
|
|
|
end
|
|
|
|
end
|