Welcome to mirror list, hosted at ThFree Co, Russian Federation.

redis_keyed_spec.rb « json_caches « cache « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f408bbf8d25c44c79118d4b69e3be192656dbf1f (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Cache::JsonCaches::RedisKeyed, feature_category: :shared do
  let_it_be(:broadcast_message) { create(:broadcast_message) }

  let(:backend) { instance_double(ActiveSupport::Cache::RedisCacheStore).as_null_object }
  let(:namespace) { 'geo' }
  let(:key) { 'foo' }
  let(:cache_key_strategy) { :revision }
  let(:expanded_key) { "#{namespace}:#{key}:#{Gitlab.revision}" }

  subject(:cache) do
    described_class.new(namespace: namespace, backend: backend, cache_key_strategy: cache_key_strategy)
  end

  describe '#read' do
    context 'when the cached value is true' do
      it 'parses the cached value' do
        allow(backend).to receive(:read).with(expanded_key).and_return(true)

        expect(Gitlab::Json).to receive(:parse).with("true").and_call_original
        expect(cache.read(key, System::BroadcastMessage)).to eq(true)
      end
    end

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

        expect(Gitlab::Json).to receive(:parse).with("false").and_call_original
        expect(cache.read(key, System::BroadcastMessage)).to eq(false)
      end
    end
  end

  describe '#expire' do
    context 'with cache_key concerns' do
      using RSpec::Parameterized::TableSyntax

      where(:namespace, :cache_key_strategy, :expanded_key) do
        nil       | :revision | "#{key}:#{Gitlab.revision}"
        nil       | :version  | "#{key}:#{Gitlab::VERSION}:#{Rails.version}"
        namespace | :revision | "#{namespace}:#{key}:#{Gitlab.revision}"
        namespace | :version  | "#{namespace}:#{key}:#{Gitlab::VERSION}:#{Rails.version}"
      end

      with_them do
        specify do
          expect(backend).to receive(:delete).with(expanded_key)

          cache.expire(key)
        end
      end

      context 'when cache_key_strategy is unknown' do
        let(:cache_key_strategy) { 'unknown' }

        it 'raises KeyError' do
          expect { cache.expire(key) }.to raise_error(KeyError)
        end
      end
    end
  end

  it_behaves_like 'Json Cache class'

  def json_value(value)
    value.to_json
  end

  def version_json_value(value)
    value.to_json
  end
end