summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/json_cache_spec.rb
blob: 2cae8ec031ae81d188d1d4f2888ac4ac9513402c (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::JsonCache do
  let(:backend) { double('backend').as_null_object }
  let(:namespace) { 'geo' }
  let(:key) { 'foo' }
  let(:expanded_key) { "#{namespace}:#{key}:#{Rails.version}" }
  let(:broadcast_message) { create(:broadcast_message) }

  subject(:cache) { described_class.new(namespace: namespace, backend: backend) }

  describe '#active?' do
    context 'when backend respond to active? method' do
      it 'delegates to the underlying cache implementation' do
        backend = double('backend', active?: false)

        cache = described_class.new(namespace: namespace, backend: backend)

        expect(cache.active?).to eq(false)
      end
    end

    context 'when backend does not respond to active? method' do
      it 'returns true' do
        backend = double('backend')

        cache = described_class.new(namespace: namespace, backend: backend)

        expect(cache.active?).to eq(true)
      end
    end
  end

  describe '#cache_key' do
    context 'when namespace is not defined' do
      it 'expands out the key with Rails version' do
        cache = described_class.new(cache_key_with_version: true)

        cache_key = cache.cache_key(key)

        expect(cache_key).to eq("#{key}:#{Rails.version}")
      end
    end

    context 'when cache_key_with_version is true' do
      it 'expands out the key with namespace and Rails version' do
        cache = described_class.new(namespace: namespace, cache_key_with_version: true)

        cache_key = cache.cache_key(key)

        expect(cache_key).to eq("#{namespace}:#{key}:#{Rails.version}")
      end
    end

    context 'when cache_key_with_version is false' do
      it 'expands out the key with namespace' do
        cache = described_class.new(namespace: namespace, cache_key_with_version: false)

        cache_key = cache.cache_key(key)

        expect(cache_key).to eq("#{namespace}:#{key}")
      end
    end

    context 'when namespace is nil, and cache_key_with_version is false' do
      it 'returns the key' do
        cache = described_class.new(namespace: nil, cache_key_with_version: false)

        cache_key = cache.cache_key(key)

        expect(cache_key).to eq(key)
      end
    end
  end

  describe '#expire' do
    it 'expires the given key from the cache' do
      cache.expire(key)

      expect(backend).to have_received(:delete).with(expanded_key)
    end
  end

  describe '#read' do
    it 'reads the given key from the cache' do
      cache.read(key)

      expect(backend).to have_received(:read).with(expanded_key)
    end

    it 'returns the cached value when there is data in the cache with the given key' do
      allow(backend).to receive(:read)
        .with(expanded_key)
        .and_return("true")

      expect(cache.read(key)).to eq(true)
    end

    it 'returns nil when there is no data in the cache with the given key' do
      allow(backend).to receive(:read)
        .with(expanded_key)
        .and_return(nil)

      expect(cache.read(key)).to be_nil
    end

    context 'when the cached value is a hash' do
      it 'parses the cached value' do
        allow(backend).to receive(:read)
          .with(expanded_key)
          .and_return(broadcast_message.to_json)

        expect(cache.read(key, BroadcastMessage)).to eq(broadcast_message)
      end

      it 'returns nil when klass is nil' do
        allow(backend).to receive(:read)
          .with(expanded_key)
          .and_return(broadcast_message.to_json)

        expect(cache.read(key)).to be_nil
      end

      it 'gracefully handles bad cached entry' do
        allow(backend).to receive(:read)
          .with(expanded_key)
          .and_return('{')

        expect(cache.read(key, BroadcastMessage)).to be_nil
      end

      it 'gracefully handles an empty hash' do
        allow(backend).to receive(:read)
          .with(expanded_key)
          .and_return('{}')

        expect(cache.read(key, BroadcastMessage)).to be_a(BroadcastMessage)
      end

      it 'gracefully handles unknown attributes' do
        allow(backend).to receive(:read)
          .with(expanded_key)
          .and_return(broadcast_message.attributes.merge(unknown_attribute: 1).to_json)

        expect(cache.read(key, BroadcastMessage)).to be_nil
      end
    end

    context 'when the cached value is an array' do
      it 'parses the cached value' do
        allow(backend).to receive(:read)
          .with(expanded_key)
          .and_return([broadcast_message].to_json)

        expect(cache.read(key, BroadcastMessage)).to eq([broadcast_message])
      end

      it 'returns an empty array when klass is nil' do
        allow(backend).to receive(:read)
          .with(expanded_key)
          .and_return([broadcast_message].to_json)

        expect(cache.read(key)).to eq([])
      end

      it 'gracefully handles bad cached entry' do
        allow(backend).to receive(:read)
          .with(expanded_key)
          .and_return('[')

        expect(cache.read(key, BroadcastMessage)).to be_nil
      end

      it 'gracefully handles an empty array' do
        allow(backend).to receive(:read)
          .with(expanded_key)
          .and_return('[]')

        expect(cache.read(key, BroadcastMessage)).to eq([])
      end

      it 'gracefully handles unknown attributes' do
        allow(backend).to receive(:read)
          .with(expanded_key)
          .and_return([{ unknown_attribute: 1 }, broadcast_message.attributes].to_json)

        expect(cache.read(key, BroadcastMessage)).to eq([broadcast_message])
      end
    end
  end

  describe '#write' do
    it 'writes value to the cache with the given key' do
      cache.write(key, true)

      expect(backend).to have_received(:write).with(expanded_key, "true", nil)
    end

    it 'writes a string containing a JSON representation of the value to the cache' do
      cache.write(key, broadcast_message)

      expect(backend).to have_received(:write)
        .with(expanded_key, broadcast_message.to_json, nil)
    end

    it 'passes options the underlying cache implementation' do
      cache.write(key, true, expires_in: 15.seconds)

      expect(backend).to have_received(:write)
        .with(expanded_key, "true", expires_in: 15.seconds)
    end

    it 'passes options the underlying cache implementation when options is empty' do
      cache.write(key, true, {})

      expect(backend).to have_received(:write)
        .with(expanded_key, "true", {})
    end

    it 'passes options the underlying cache implementation when options is nil' do
      cache.write(key, true, nil)

      expect(backend).to have_received(:write)
        .with(expanded_key, "true", nil)
    end
  end

  describe '#fetch', :use_clean_rails_memory_store_caching do
    let(:backend) { Rails.cache }

    it 'requires a block' do
      expect { cache.fetch(key) }.to raise_error(LocalJumpError)
    end

    it 'passes options the underlying cache implementation' do
      expect(backend).to receive(:write)
        .with(expanded_key, "true", expires_in: 15.seconds)

      cache.fetch(key, expires_in: 15.seconds) { true }
    end

    context 'when the given key does not exist in the cache' do
      context 'when the result of the block is truthy' do
        it 'returns the result of the block' do
          result = cache.fetch(key) { true }

          expect(result).to eq(true)
        end

        it 'caches the value' do
          expect(backend).to receive(:write).with(expanded_key, "true", {})

          cache.fetch(key) { true }
        end
      end

      context 'when the result of the block is false' do
        it 'returns the result of the block' do
          result = cache.fetch(key) { false }

          expect(result).to eq(false)
        end

        it 'caches the value' do
          expect(backend).to receive(:write).with(expanded_key, "false", {})

          cache.fetch(key) { false }
        end
      end

      context 'when the result of the block is nil' do
        it 'returns the result of the block' do
          result = cache.fetch(key) { nil }

          expect(result).to eq(nil)
        end

        it 'caches the value' do
          expect(backend).to receive(:write).with(expanded_key, "null", {})

          cache.fetch(key) { nil }
        end
      end
    end

    context 'when the given key exists in the cache' do
      context 'when the cached value is a hash' do
        before do
          backend.write(expanded_key, broadcast_message.to_json)
        end

        it 'parses the cached value' do
          result = cache.fetch(key, as: BroadcastMessage) { 'block result' }

          expect(result).to eq(broadcast_message)
        end

        context 'when the cached value is an instance of ActiveRecord::Base' do
          it 'returns a persisted record when id is set' do
            backend.write(expanded_key, broadcast_message.to_json)

            result = cache.fetch(key, as: BroadcastMessage) { 'block result' }

            expect(result).to be_persisted
          end

          it 'returns a new record when id is nil' do
            backend.write(expanded_key, build(:broadcast_message).to_json)

            result = cache.fetch(key, as: BroadcastMessage) { 'block result' }

            expect(result).to be_new_record
          end

          it 'returns a new record when id is missing' do
            backend.write(expanded_key, build(:broadcast_message).attributes.except('id').to_json)

            result = cache.fetch(key, as: BroadcastMessage) { 'block result' }

            expect(result).to be_new_record
          end
        end

        it "returns the result of the block when 'as' option is nil" do
          result = cache.fetch(key, as: nil) { 'block result' }

          expect(result).to eq('block result')
        end

        it "returns the result of the block when 'as' option is missing" do
          result = cache.fetch(key) { 'block result' }

          expect(result).to eq('block result')
        end
      end

      context 'when the cached value is a array' do
        before do
          backend.write(expanded_key, [broadcast_message].to_json)
        end

        it 'parses the cached value' do
          result = cache.fetch(key, as: BroadcastMessage) { 'block result' }

          expect(result).to eq([broadcast_message])
        end

        it "returns an empty array when 'as' option is nil" do
          result = cache.fetch(key, as: nil) { 'block result' }

          expect(result).to eq([])
        end

        it "returns an empty array when 'as' option is not informed" do
          result = cache.fetch(key) { 'block result' }

          expect(result).to eq([])
        end
      end

      context 'when the cached value is true' do
        before do
          backend.write(expanded_key, "true")
        end

        it 'returns the cached value' do
          result = cache.fetch(key) { 'block result' }

          expect(result).to eq(true)
        end

        it 'does not execute the block' do
          expect { |block| cache.fetch(key, &block) }.not_to yield_control
        end

        it 'does not write to the cache' do
          expect(backend).not_to receive(:write)

          cache.fetch(key) { 'block result' }
        end
      end

      context 'when the cached value is false' do
        before do
          backend.write(expanded_key, "false")
        end

        it 'returns the cached value' do
          result = cache.fetch(key) { 'block result' }

          expect(result).to eq(false)
        end

        it 'does not execute the block' do
          expect { |block| cache.fetch(key, &block) }.not_to yield_control
        end

        it 'does not write to the cache' do
          expect(backend).not_to receive(:write)

          cache.fetch(key) { 'block result' }
        end
      end

      context 'when the cached value is nil' do
        before do
          backend.write(expanded_key, "null")
        end

        it 'returns the result of the block' do
          result = cache.fetch(key) { 'block result' }

          expect(result).to eq('block result')
        end

        it 'writes the result of the block to the cache' do
          expect(backend).to receive(:write)
            .with(expanded_key, 'block result'.to_json, {})

          cache.fetch(key) { 'block result' }
        end
      end
    end
  end
end