summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/repository_cache_adapter_spec.rb
blob: 703e73f1a9b82f85220112d1da9f2dba1abf04a6 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
require 'spec_helper'

describe Gitlab::RepositoryCacheAdapter do
  let(:project) { create(:project, :repository) }
  let(:repository) { project.repository }
  let(:cache) { repository.send(:cache) }

  describe '#cache_method_output', :use_clean_rails_memory_store_caching do
    let(:fallback) { 10 }

    context 'with a non-existing repository' do
      let(:project) { create(:project) } # No repository

      subject do
        repository.cache_method_output(:cats, fallback: fallback) do
          repository.cats_call_stub
        end
      end

      it 'returns the fallback value' do
        expect(subject).to eq(fallback)
      end

      it 'avoids calling the original method' do
        expect(repository).not_to receive(:cats_call_stub)

        subject
      end
    end

    context 'with a method throwing a non-existing-repository error' do
      subject do
        repository.cache_method_output(:cats, fallback: fallback) do
          raise Gitlab::Git::Repository::NoRepository
        end
      end

      it 'returns the fallback value' do
        expect(subject).to eq(fallback)
      end

      it 'does not cache the data' do
        subject

        expect(repository.instance_variable_defined?(:@cats)).to eq(false)
        expect(cache.exist?(:cats)).to eq(false)
      end
    end

    context 'with an existing repository' do
      it 'caches the output' do
        object = double

        expect(object).to receive(:number).once.and_return(10)

        2.times do
          val = repository.cache_method_output(:cats) { object.number }

          expect(val).to eq(10)
        end

        expect(repository.send(:cache).exist?(:cats)).to eq(true)
        expect(repository.instance_variable_get(:@cats)).to eq(10)
      end
    end
  end

  describe '#memoize_method_output' do
    let(:fallback) { 10 }

    context 'with a non-existing repository' do
      let(:project) { create(:project) } # No repository

      subject do
        repository.memoize_method_output(:cats, fallback: fallback) do
          repository.cats_call_stub
        end
      end

      it 'returns the fallback value' do
        expect(subject).to eq(fallback)
      end

      it 'avoids calling the original method' do
        expect(repository).not_to receive(:cats_call_stub)

        subject
      end

      it 'does not set the instance variable' do
        subject

        expect(repository.instance_variable_defined?(:@cats)).to eq(false)
      end
    end

    context 'with a method throwing a non-existing-repository error' do
      subject do
        repository.memoize_method_output(:cats, fallback: fallback) do
          raise Gitlab::Git::Repository::NoRepository
        end
      end

      it 'returns the fallback value' do
        expect(subject).to eq(fallback)
      end

      it 'does not set the instance variable' do
        subject

        expect(repository.instance_variable_defined?(:@cats)).to eq(false)
      end
    end

    context 'with an existing repository' do
      it 'sets the instance variable' do
        repository.memoize_method_output(:cats, fallback: fallback) do
          'block output'
        end

        expect(repository.instance_variable_get(:@cats)).to eq('block output')
      end
    end
  end

  describe '#expire_method_caches' do
    it 'expires the caches of the given methods' do
      expect(cache).to receive(:expire).with(:rendered_readme)
      expect(cache).to receive(:expire).with(:gitignore)

      repository.expire_method_caches(%i(rendered_readme gitignore))
    end

    it 'does not expire caches for non-existent methods' do
      expect(cache).not_to receive(:expire).with(:nonexistent)
      expect(Rails.logger).to(
        receive(:error).with("Requested to expire non-existent method 'nonexistent' for Repository"))

      repository.expire_method_caches(%i(nonexistent))
    end
  end
end