housekeeping_service_spec.rb 2.95 KB
Newer Older
1 2 3
require 'spec_helper'

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

7 8 9 10
  before do
    project.reset_pushes_since_gc
  end

Yorick Peterse's avatar
Yorick Peterse committed
11 12 13
  after do
    project.reset_pushes_since_gc
  end
14

Yorick Peterse's avatar
Yorick Peterse committed
15
  describe '#execute' do
16
    it 'enqueues a sidekiq job' do
Jacob Vosmaer's avatar
Jacob Vosmaer committed
17 18 19 20
      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)
21

22
      subject.execute
23
      expect(project.reload.pushes_since_gc).to eq(0)
24 25
    end

Lin Jen-Shin's avatar
Lin Jen-Shin committed
26 27 28 29 30 31
    it 'yields the block if given' do
      expect do |b|
        subject.execute(&b)
      end.to yield_with_no_args
    end

32
    context 'when no lease can be obtained' do
33
      before do
34 35
        expect(subject).to receive(:try_obtain_lease).and_return(false)
      end
36

37 38 39 40 41 42 43 44 45
      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
46
        end.not_to change { project.pushes_since_gc }
47
      end
Lin Jen-Shin's avatar
Lin Jen-Shin committed
48 49 50 51 52 53 54

      it 'does not yield' do
        expect do |b|
          expect { subject.execute(&b) }
            .to raise_error(Projects::HousekeepingService::LeaseTaken)
        end.not_to yield_with_no_args
      end
55 56 57
    end
  end

Yorick Peterse's avatar
Yorick Peterse committed
58
  describe '#needed?' do
59 60 61 62 63 64 65 66 67
    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
68

Yorick Peterse's avatar
Yorick Peterse committed
69
  describe '#increment!' do
Jacob Vosmaer's avatar
Jacob Vosmaer committed
70
    it 'increments the pushes_since_gc counter' do
71 72 73 74
      expect do
        subject.increment!
      end.to change { project.pushes_since_gc }.from(0).to(1)
    end
Jacob Vosmaer's avatar
Jacob Vosmaer committed
75
  end
Jacob Vosmaer's avatar
Jacob Vosmaer committed
76 77 78 79 80 81

  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
82 83
    expect(GitGarbageCollectWorker).to receive(:perform_async).with(project.id, :gc, :the_lease_key, :the_uuid)
      .exactly(1).times
Jacob Vosmaer's avatar
Jacob Vosmaer committed
84
    # At push 50, 100, 150
85 86
    expect(GitGarbageCollectWorker).to receive(:perform_async).with(project.id, :full_repack, :the_lease_key, :the_uuid)
      .exactly(3).times
Jacob Vosmaer's avatar
Jacob Vosmaer committed
87
    # At push 10, 20, ... (except those above)
88 89
    expect(GitGarbageCollectWorker).to receive(:perform_async).with(project.id, :incremental_repack, :the_lease_key, :the_uuid)
      .exactly(16).times
Jacob Vosmaer's avatar
Jacob Vosmaer committed
90 91 92 93 94 95 96 97

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

    expect(project.pushes_since_gc).to eq(1)
  end
Jacob Vosmaer's avatar
Jacob Vosmaer committed
98
end