housekeeping_service_spec.rb 2.11 KB
Newer Older
1 2 3 4 5 6
require 'spec_helper'

describe Projects::HousekeepingService do
  subject { Projects::HousekeepingService.new(project) }
  let(:project) { create :project }

Jacob Vosmaer's avatar
Jacob Vosmaer committed
7
  describe 'execute' do
8 9 10 11 12 13 14
    before do
      project.pushes_since_gc = 3
      project.save!
    end

    it 'enqueues a sidekiq job' do
      expect(subject).to receive(:try_obtain_lease).and_return(true)
15
      expect(GitGarbageCollectWorker).to receive(:perform_async).with(project.id)
16

17
      subject.execute
18
      expect(project.reload.pushes_since_gc).to eq(0)
19 20
    end

21 22 23 24
    context 'when no lease can be obtained' do
      before(:each) do
        expect(subject).to receive(:try_obtain_lease).and_return(false)
      end
25

26 27 28 29 30 31 32 33 34 35 36
      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)
        end.not_to change { project.pushes_since_gc }.from(3)
      end
37 38 39
    end
  end

Jacob Vosmaer's avatar
Jacob Vosmaer committed
40
  describe 'needed?' do
41 42 43 44 45 46 47 48 49
    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
Jacob Vosmaer's avatar
Jacob Vosmaer committed
50

Jacob Vosmaer's avatar
Jacob Vosmaer committed
51
  describe 'increment!' do
52 53
    let(:lease_key) { "project_housekeeping:increment!:#{project.id}" }

Jacob Vosmaer's avatar
Jacob Vosmaer committed
54
    it 'increments the pushes_since_gc counter' do
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
      lease = double(:lease, try_obtain: true)
      expect(Gitlab::ExclusiveLease).to receive(:new).with(lease_key, anything).and_return(lease)

      expect do
        subject.increment!
      end.to change { project.pushes_since_gc }.from(0).to(1)
    end

    it 'does not increment when no lease can be obtained' do
      lease = double(:lease, try_obtain: false)
      expect(Gitlab::ExclusiveLease).to receive(:new).with(lease_key, anything).and_return(lease)

      expect do
        subject.increment!
      end.not_to change { project.pushes_since_gc }
Jacob Vosmaer's avatar
Jacob Vosmaer committed
70 71 72
    end
  end
end