project_cache_worker_spec.rb 1.8 KB
Newer Older
1 2 3 4 5 6 7
require 'spec_helper'

describe ProjectCacheWorker do
  let(:project) { create(:project) }

  subject { described_class.new }

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
  describe '.perform_async' do
    it 'schedules the job when no lease exists' do
      allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:exists?).
        and_return(false)

      expect_any_instance_of(described_class).to receive(:perform)

      described_class.perform_async(project.id)
    end

    it 'does not schedule the job when a lease exists' do
      allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:exists?).
        and_return(true)

      expect_any_instance_of(described_class).not_to receive(:perform)

      described_class.perform_async(project.id)
    end
  end

28
  describe '#perform' do
29 30 31 32 33
    context 'when an exclusive lease can be obtained' do
      before do
        allow(subject).to receive(:try_obtain_lease_for).with(project.id).
          and_return(true)
      end
34

35 36 37
      it 'updates project cache data' do
        expect_any_instance_of(Repository).to receive(:size)
        expect_any_instance_of(Repository).to receive(:commit_count)
38

39 40 41 42 43 44 45 46 47 48 49 50
        expect_any_instance_of(Project).to receive(:update_repository_size)
        expect_any_instance_of(Project).to receive(:update_commit_count)

        subject.perform(project.id)
      end

      it 'handles missing repository data' do
        expect_any_instance_of(Repository).to receive(:exists?).and_return(false)
        expect_any_instance_of(Repository).not_to receive(:size)

        subject.perform(project.id)
      end
51 52
    end

53 54 55 56 57 58
    context 'when an exclusive lease can not be obtained' do
      it 'does nothing' do
        allow(subject).to receive(:try_obtain_lease_for).with(project.id).
          and_return(false)

        expect(subject).not_to receive(:update_caches)
59

60 61
        subject.perform(project.id)
      end
62 63 64
    end
  end
end