housekeeping_service_spec.rb 1.56 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 }

Yorick Peterse's avatar
Yorick Peterse committed
7 8 9
  after do
    project.reset_pushes_since_gc
  end
10

Yorick Peterse's avatar
Yorick Peterse committed
11
  describe '#execute' do
12 13
    it 'enqueues a sidekiq job' do
      expect(subject).to receive(:try_obtain_lease).and_return(true)
14
      expect(GitGarbageCollectWorker).to receive(:perform_async).with(project.id)
15

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

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

25 26 27 28 29 30 31 32 33
      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)
Yorick Peterse's avatar
Yorick Peterse committed
34
        end.not_to change { project.pushes_since_gc }
35
      end
36 37 38
    end
  end

Yorick Peterse's avatar
Yorick Peterse committed
39
  describe '#needed?' do
40 41 42 43 44 45 46 47 48
    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
49

Yorick Peterse's avatar
Yorick Peterse committed
50
  describe '#increment!' do
Jacob Vosmaer's avatar
Jacob Vosmaer committed
51
    it 'increments the pushes_since_gc counter' do
52 53 54 55
      expect do
        subject.increment!
      end.to change { project.pushes_since_gc }.from(0).to(1)
    end
Jacob Vosmaer's avatar
Jacob Vosmaer committed
56 57
  end
end