2016-03-14 14:18:52 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Projects::HousekeepingService do
|
2017-05-01 15:13:33 +00:00
|
|
|
subject { described_class.new(project) }
|
2017-03-27 21:14:01 +00:00
|
|
|
let(:project) { create(:project, :repository) }
|
2016-03-14 14:18:52 +00:00
|
|
|
|
2016-09-19 04:21:43 +00:00
|
|
|
before do
|
|
|
|
project.reset_pushes_since_gc
|
|
|
|
end
|
|
|
|
|
2016-09-13 16:28:45 +00:00
|
|
|
after do
|
|
|
|
project.reset_pushes_since_gc
|
|
|
|
end
|
2016-03-14 14:18:52 +00:00
|
|
|
|
2016-09-13 16:28:45 +00:00
|
|
|
describe '#execute' do
|
2016-03-14 14:18:52 +00:00
|
|
|
it 'enqueues a sidekiq job' do
|
2016-10-27 12:59:52 +00:00
|
|
|
expect(subject).to receive(:try_obtain_lease).and_return(:the_uuid)
|
|
|
|
expect(subject).to receive(:lease_key).and_return(:the_lease_key)
|
|
|
|
expect(subject).to receive(:task).and_return(:the_task)
|
|
|
|
expect(GitGarbageCollectWorker).to receive(:perform_async).with(project.id, :the_task, :the_lease_key, :the_uuid)
|
2016-03-14 14:18:52 +00:00
|
|
|
|
2016-03-15 10:03:43 +00:00
|
|
|
subject.execute
|
2016-07-12 09:58:06 +00:00
|
|
|
expect(project.reload.pushes_since_gc).to eq(0)
|
2016-03-14 14:18:52 +00:00
|
|
|
end
|
|
|
|
|
2016-07-12 09:58:06 +00:00
|
|
|
context 'when no lease can be obtained' do
|
2017-08-10 22:31:42 +00:00
|
|
|
before do
|
2016-07-12 09:58:06 +00:00
|
|
|
expect(subject).to receive(:try_obtain_lease).and_return(false)
|
|
|
|
end
|
2016-03-14 14:18:52 +00:00
|
|
|
|
2016-07-12 09:58:06 +00: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 16:28:45 +00:00
|
|
|
end.not_to change { project.pushes_since_gc }
|
2016-07-12 09:58:06 +00:00
|
|
|
end
|
2016-03-14 14:18:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-13 16:28:45 +00:00
|
|
|
describe '#needed?' do
|
2016-03-14 14:18:52 +00: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 15:49:24 +00:00
|
|
|
|
2016-09-13 16:28:45 +00:00
|
|
|
describe '#increment!' do
|
2016-03-14 15:49:24 +00:00
|
|
|
it 'increments the pushes_since_gc counter' do
|
2016-07-12 09:58:06 +00:00
|
|
|
expect do
|
|
|
|
subject.increment!
|
|
|
|
end.to change { project.pushes_since_gc }.from(0).to(1)
|
|
|
|
end
|
2016-03-14 15:49:24 +00:00
|
|
|
end
|
2016-10-27 12:59:52 +00:00
|
|
|
|
|
|
|
it 'uses all three kinds of housekeeping we offer' 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
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(GitGarbageCollectWorker).to receive(:perform_async).with(project.id, :gc, :the_lease_key, :the_uuid)
|
|
|
|
.exactly(1).times
|
2016-10-27 12:59:52 +00:00
|
|
|
# At push 50, 100, 150
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(GitGarbageCollectWorker).to receive(:perform_async).with(project.id, :full_repack, :the_lease_key, :the_uuid)
|
|
|
|
.exactly(3).times
|
2016-10-27 12:59:52 +00:00
|
|
|
# At push 10, 20, ... (except those above)
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(GitGarbageCollectWorker).to receive(:perform_async).with(project.id, :incremental_repack, :the_lease_key, :the_uuid)
|
|
|
|
.exactly(16).times
|
2016-10-27 12:59:52 +00:00
|
|
|
|
|
|
|
201.times do
|
|
|
|
subject.increment!
|
|
|
|
subject.execute if subject.needed?
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(project.pushes_since_gc).to eq(1)
|
|
|
|
end
|
2016-03-14 15:49:24 +00:00
|
|
|
end
|