BigW Consortium Gitlab

project_cache_worker_spec.rb 2.11 KB
Newer Older
1 2 3
require 'spec_helper'

describe ProjectCacheWorker do
4
  let(:worker) { described_class.new }
5 6
  let(:project) { create(:project) }
  let(:statistics) { project.statistics }
7

8 9 10 11 12
  describe '#perform' do
    before do
      allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain).
        and_return(true)
    end
13

14 15
    context 'with a non-existing project' do
      it 'does nothing' do
16
        expect(worker).not_to receive(:update_statistics)
17

18 19
        worker.perform(-1)
      end
20 21
    end

22 23 24
    context 'with an existing project without a repository' do
      it 'does nothing' do
        allow_any_instance_of(Repository).to receive(:exists?).and_return(false)
25

26
        expect(worker).not_to receive(:update_statistics)
27

28 29
        worker.perform(project.id)
      end
30 31
    end

32
    context 'with an existing project' do
33 34 35 36
      it 'updates the project statistics' do
        expect(worker).to receive(:update_statistics)
          .with(kind_of(Project), %i(repository_size))
          .and_call_original
37

38
        worker.perform(project.id, [], %w(repository_size))
39 40
      end

41 42 43 44
      it 'refreshes the method caches' do
        expect_any_instance_of(Repository).to receive(:refresh_method_caches).
          with(%i(readme)).
          and_call_original
45

46
        worker.perform(project.id, %w(readme))
47
      end
48
    end
49
  end
50

51
  describe '#update_statistics' do
52 53 54
    context 'when a lease could not be obtained' do
      it 'does not update the repository size' do
        allow(worker).to receive(:try_obtain_lease_for).
55
          with(project.id, :update_statistics).
56 57
          and_return(false)

58
        expect(statistics).not_to receive(:refresh!)
59

60
        worker.update_statistics(project)
61 62 63 64
      end
    end

    context 'when a lease could be obtained' do
65
      it 'updates the project statistics' do
66
        allow(worker).to receive(:try_obtain_lease_for).
67
          with(project.id, :update_statistics).
68 69
          and_return(true)

70 71 72
        expect(statistics).to receive(:refresh!)
          .with(only: %i(repository_size))
          .and_call_original
73

74
        worker.update_statistics(project, %i(repository_size))
75
      end
76 77 78
    end
  end
end