BigW Consortium Gitlab

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

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

8 9
  describe '#perform' do
    before do
10 11
      allow_any_instance_of(Gitlab::ExclusiveLease).to receive(:try_obtain)
        .and_return(true)
12
    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
      it 'refreshes the method caches' do
42 43 44
        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 49 50 51 52 53

      context 'with plain readme' do
        it 'refreshes the method caches' do
          allow(MarkupHelper).to receive(:gitlab_markdown?).and_return(false)
          allow(MarkupHelper).to receive(:plain?).and_return(true)

54 55 56
          expect_any_instance_of(Repository).to receive(:refresh_method_caches)
                                                  .with(%i(readme))
                                                  .and_call_original
57 58 59
          worker.perform(project.id, %w(readme))
        end
      end
60
    end
61
  end
62

63
  describe '#update_statistics' do
64 65
    context 'when a lease could not be obtained' do
      it 'does not update the repository size' do
66 67 68
        allow(worker).to receive(:try_obtain_lease_for)
          .with(project.id, :update_statistics)
          .and_return(false)
69

70
        expect(statistics).not_to receive(:refresh!)
71

72
        worker.update_statistics(project)
73 74 75 76
      end
    end

    context 'when a lease could be obtained' do
77
      it 'updates the project statistics' do
78 79 80
        allow(worker).to receive(:try_obtain_lease_for)
          .with(project.id, :update_statistics)
          .and_return(true)
81

82 83 84
        expect(statistics).to receive(:refresh!)
          .with(only: %i(repository_size))
          .and_call_original
85

86
        worker.update_statistics(project, %i(repository_size))
87
      end
88 89 90
    end
  end
end