summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/lib/cache_helpers_shared_examples.rb
blob: 82a9e8130f7694f6a7c3ec8d725fa1ab98e4859c (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# frozen_string_literal: true

RSpec.shared_examples_for 'object cache helper' do
  it { is_expected.to be_a(Gitlab::Json::PrecompiledJson) }

  it "uses the presenter" do
    expect(presenter).to receive(:represent).with(presentable, project: project)

    subject
  end

  it "is valid JSON" do
    parsed = Gitlab::Json.parse(subject.to_s)

    expect(parsed).to be_a(Hash)
    expect(parsed["id"]).to eq(presentable.id)
  end

  it "fetches from the cache" do
    expect(instance.cache).to receive(:fetch).with("#{presenter.class.name}:#{presentable.cache_key}:#{user.cache_key}", expires_in: described_class::DEFAULT_EXPIRY).once

    subject
  end

  context "when a cache context is supplied" do
    before do
      kwargs[:cache_context] = -> (item) { item.project.cache_key }
    end

    it "uses the context to augment the cache key" do
      expect(instance.cache).to receive(:fetch).with("#{presenter.class.name}:#{presentable.cache_key}:#{project.cache_key}", expires_in: described_class::DEFAULT_EXPIRY).once

      subject
    end
  end

  context "when expires_in is supplied" do
    it "sets the expiry when accessing the cache" do
      kwargs[:expires_in] = 7.days

      expect(instance.cache).to receive(:fetch).with("#{presenter.class.name}:#{presentable.cache_key}:#{user.cache_key}", expires_in: 7.days).once

      subject
    end
  end

  context 'when a caller id is present' do
    let(:transaction) { Gitlab::Metrics::WebTransaction.new({}) }
    let(:caller_id) { 'caller_id' }

    before do
      allow(::Gitlab::Metrics::WebTransaction).to receive(:current).and_return(transaction)
      allow(transaction).to receive(:increment)
      allow(Gitlab::ApplicationContext).to receive(:current_context_attribute).with(:caller_id).and_return(caller_id)
    end

    context 'when feature flag is off' do
      before do
        stub_feature_flags(add_timing_to_certain_cache_actions: false)
      end

      it 'does not call increment' do
        expect(transaction).not_to receive(:increment).with(:cached_object_operations_total, any_args)

        subject
      end

      it 'does not call histogram' do
        expect(Gitlab::Metrics).not_to receive(:histogram)

        subject
      end

      it "is valid JSON" do
        parsed = Gitlab::Json.parse(subject.to_s)

        expect(parsed).to be_a(Hash)
        expect(parsed["id"]).to eq(presentable.id)
      end
    end

    it 'increments the counter' do
      expect(transaction)
        .to receive(:increment)
        .with(:cached_object_operations_total, 1, { caller_id: caller_id, render_type: :object, cache_hit: false }).once

      expect(transaction)
        .to receive(:increment)
        .with(:cached_object_operations_total, 0, { caller_id: caller_id, render_type: :object, cache_hit: true }).once

      subject
    end
  end
end

RSpec.shared_examples_for 'collection cache helper' do
  it { is_expected.to be_an(Gitlab::Json::PrecompiledJson) }

  it "uses the presenter" do
    presentable.each do |item|
      expect(presenter).to receive(:represent).with(item, project: project)
    end

    subject
  end

  it "is valid JSON" do
    parsed = Gitlab::Json.parse(subject.to_s)

    expect(parsed).to be_an(Array)

    presentable.each_with_index do |item, i|
      expect(parsed[i]["id"]).to eq(item.id)
    end
  end

  it "fetches from the cache" do
    keys = presentable.map { |item| "#{presenter.class.name}:#{item.cache_key}:#{user.cache_key}" }

    expect(instance.cache).to receive(:fetch_multi).with(*keys, expires_in: described_class::DEFAULT_EXPIRY).once.and_call_original

    subject
  end

  context "when a cache context is supplied" do
    before do
      kwargs[:cache_context] = -> (item) { item.project.cache_key }
    end

    it "uses the context to augment the cache key" do
      keys = presentable.map { |item| "#{presenter.class.name}:#{item.cache_key}:#{project.cache_key}" }

      expect(instance.cache).to receive(:fetch_multi).with(*keys, expires_in: described_class::DEFAULT_EXPIRY).once.and_call_original

      subject
    end
  end

  context "expires_in is supplied" do
    it "sets the expiry when accessing the cache" do
      keys = presentable.map { |item| "#{presenter.class.name}:#{item.cache_key}:#{user.cache_key}" }
      kwargs[:expires_in] = 7.days

      expect(instance.cache).to receive(:fetch_multi).with(*keys, expires_in: 7.days).once.and_call_original

      subject
    end
  end

  context 'when a caller id is present' do
    let(:transaction) { Gitlab::Metrics::WebTransaction.new({}) }
    let(:caller_id) { 'caller_id' }

    before do
      allow(::Gitlab::Metrics::WebTransaction).to receive(:current).and_return(transaction)
      allow(transaction).to receive(:increment)
      allow(Gitlab::ApplicationContext).to receive(:current_context_attribute).with(:caller_id).and_return(caller_id)
    end

    context 'when feature flag is off' do
      before do
        stub_feature_flags(add_timing_to_certain_cache_actions: false)
      end

      it 'does not call increment' do
        expect(transaction).not_to receive(:increment).with(:cached_object_operations_total, any_args)

        subject
      end

      it 'does not call histogram' do
        expect(Gitlab::Metrics).not_to receive(:histogram)

        subject
      end

      it "is valid JSON" do
        parsed = Gitlab::Json.parse(subject.to_s)

        expect(parsed).to be_an(Array)

        presentable.each_with_index do |item, i|
          expect(parsed[i]["id"]).to eq(item.id)
        end
      end
    end

    context 'when presentable has a group by clause' do
      let(:presentable) { MergeRequest.group(:id) }

      it "returns the presentables" do
        expect(transaction)
          .to receive(:increment)
          .with(:cached_object_operations_total, 0, { caller_id: caller_id, render_type: :collection, cache_hit: true }).once

        expect(transaction)
          .to receive(:increment)
          .with(:cached_object_operations_total, MergeRequest.count, { caller_id: caller_id, render_type: :collection, cache_hit: false }).once

        parsed = Gitlab::Json.parse(subject.to_s)

        expect(parsed).to be_an(Array)

        presentable.each_with_index do |item, i|
          expect(parsed[i]["id"]).to eq(item.id)
        end
      end
    end

    context 'when the presentables all miss' do
      it 'increments the counters' do
        expect(transaction)
          .to receive(:increment)
          .with(:cached_object_operations_total, 0, { caller_id: caller_id, render_type: :collection, cache_hit: true }).once

        expect(transaction)
          .to receive(:increment)
          .with(:cached_object_operations_total, presentable.size, { caller_id: caller_id, render_type: :collection, cache_hit: false }).once

        subject
      end
    end

    context 'when the presents hit' do
      it 'increments the counters' do
        subject

        expect(transaction)
          .to receive(:increment)
          .with(:cached_object_operations_total, presentable.size, { caller_id: caller_id, render_type: :collection, cache_hit: true }).once

        expect(transaction)
          .to receive(:increment)
          .with(:cached_object_operations_total, 0, { caller_id: caller_id, render_type: :collection, cache_hit: false }).once

        instance.public_send(method, presentable, **kwargs)
      end
    end
  end
end