summaryrefslogtreecommitdiff
path: root/spec/services/projects/housekeeping_service_spec.rb
blob: ebed802708d2b8c8a9d12bcf0f51369e75e343de (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
73
74
75
76
77
78
79
80
81
82
83
84
85
require 'spec_helper'

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

  before do
    project.reset_pushes_since_gc
  end

  after do
    project.reset_pushes_since_gc
  end

  describe '#execute' do
    it 'enqueues a sidekiq job' do
      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)

      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 }
      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
    it 'increments the pushes_since_gc counter' do
      expect do
        subject.increment!
      end.to change { project.pushes_since_gc }.from(0).to(1)
    end
  end

  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
    expect(GitGarbageCollectWorker).to receive(:perform_async).with(project.id, :gc, :the_lease_key, :the_uuid)
      .exactly(1).times
    # At push 50, 100, 150
    expect(GitGarbageCollectWorker).to receive(:perform_async).with(project.id, :full_repack, :the_lease_key, :the_uuid)
      .exactly(3).times
    # At push 10, 20, ... (except those above)
    expect(GitGarbageCollectWorker).to receive(:perform_async).with(project.id, :incremental_repack, :the_lease_key, :the_uuid)
      .exactly(16).times

    201.times do
      subject.increment!
      subject.execute if subject.needed?
    end

    expect(project.pushes_since_gc).to eq(1)
  end
end