2016-03-14 10:18:52 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Projects::HousekeepingService do
|
|
|
|
subject { Projects::HousekeepingService.new(project) }
|
|
|
|
let(:project) { create :project }
|
|
|
|
|
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
|
|
|
|
expect(subject).to receive(:try_obtain_lease).and_return(true)
|
2016-07-08 15:32:58 -04:00
|
|
|
expect(GitGarbageCollectWorker).to receive(:perform_async).with(project.id)
|
2016-03-14 10:18:52 -04:00
|
|
|
|
2016-03-15 06:03:43 -04:00
|
|
|
subject.execute
|
2016-07-12 05:58:06 -04:00
|
|
|
expect(project.reload.pushes_since_gc).to eq(0)
|
2016-03-14 10:18:52 -04:00
|
|
|
end
|
|
|
|
|
2016-07-12 05:58:06 -04:00
|
|
|
context 'when no lease can be obtained' do
|
|
|
|
before(:each) do
|
|
|
|
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
|
2016-03-14 10:18:52 -04:00
|
|
|
end
|
|
|
|
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
|
2016-07-12 05:58:06 -04:00
|
|
|
expect do
|
|
|
|
subject.increment!
|
|
|
|
end.to change { project.pushes_since_gc }.from(0).to(1)
|
|
|
|
end
|
2016-03-14 11:49:24 -04:00
|
|
|
end
|
|
|
|
end
|