summaryrefslogtreecommitdiff
path: root/spec/services/projects/housekeeping_service_spec.rb
blob: ad0d58672b3df13fd90e703932cfbe30eeb469d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require 'spec_helper'

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

  describe 'execute' do
    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)
      expect(GitGarbageCollectWorker).to receive(:perform_async).with(project.id)

      subject.execute
      expect(project.reload.pushes_since_gc).to eq(0)
    end

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

      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
    end
  end

  describe 'needed?' do
    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

  describe 'increment!' do
    let(:lease_key) { "project_housekeeping:increment!:#{project.id}" }

    it 'increments the pushes_since_gc counter' do
      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 }
    end
  end
end