summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/repository_set_cache_spec.rb
blob: 87e51f801e5d42d7508d7340b4cb568b542ddddc (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
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::RepositorySetCache, :clean_gitlab_redis_cache do
  let(:project) { create(:project) }
  let(:repository) { project.repository }
  let(:namespace) { "#{repository.full_path}:#{project.id}" }
  let(:cache) { described_class.new(repository) }

  describe '#cache_key' do
    subject { cache.cache_key(:foo) }

    it 'includes the namespace' do
      is_expected.to eq("foo:#{namespace}:set")
    end

    context 'with a given namespace' do
      let(:extra_namespace) { 'my:data' }
      let(:cache) { described_class.new(repository, extra_namespace: extra_namespace) }

      it 'includes the full namespace' do
        is_expected.to eq("foo:#{namespace}:#{extra_namespace}:set")
      end
    end
  end

  describe '#expire' do
    it 'expires the given key from the cache' do
      cache.write(:foo, ['value'])

      expect(cache.read(:foo)).to contain_exactly('value')
      expect(cache.expire(:foo)).to eq(1)
      expect(cache.read(:foo)).to be_empty
    end
  end

  describe '#exist?' do
    it 'checks whether the key exists' do
      expect(cache.exist?(:foo)).to be(false)

      cache.write(:foo, ['value'])

      expect(cache.exist?(:foo)).to be(true)
    end
  end

  describe '#fetch' do
    let(:blk) { -> { ['block value'] } }

    subject { cache.fetch(:foo, &blk) }

    it 'fetches the key from the cache when filled' do
      cache.write(:foo, ['value'])

      is_expected.to contain_exactly('value')
    end

    it 'writes the value of the provided block when empty' do
      cache.expire(:foo)

      is_expected.to contain_exactly('block value')
      expect(cache.read(:foo)).to contain_exactly('block value')
    end
  end

  describe '#include?' do
    it 'checks inclusion in the Redis set' do
      cache.write(:foo, ['value'])

      expect(cache.include?(:foo, 'value')).to be(true)
      expect(cache.include?(:foo, 'bar')).to be(false)
    end
  end
end