debian-mirror-gitlab/spec/lib/gitlab/repository_set_cache_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

192 lines
4.9 KiB
Ruby
Raw Permalink Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::RepositorySetCache, :clean_gitlab_redis_cache do
2020-03-13 15:44:24 +05:30
let_it_be(:project) { create(:project) }
2021-06-08 01:23:25 +05:30
2019-12-04 20:38:33 +05:30
let(:repository) { project.repository }
let(:namespace) { "#{repository.full_path}:#{project.id}" }
2021-09-04 01:27:46 +05:30
let(:gitlab_cache_namespace) { Gitlab::Redis::Cache::CACHE_NAMESPACE }
2019-12-04 20:38:33 +05:30
let(:cache) { described_class.new(repository) }
describe '#cache_key' do
subject { cache.cache_key(:foo) }
2020-04-08 14:13:33 +05:30
shared_examples 'cache_key examples' do
it 'includes the namespace' do
2021-09-30 23:02:18 +05:30
is_expected.to eq("#{gitlab_cache_namespace}:foo:#{namespace}:set")
2020-04-08 14:13:33 +05:30
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
2021-09-30 23:02:18 +05:30
is_expected.to eq("#{gitlab_cache_namespace}:foo:#{namespace}:#{extra_namespace}:set")
2020-04-08 14:13:33 +05:30
end
end
2019-12-04 20:38:33 +05:30
end
2020-04-08 14:13:33 +05:30
describe 'project repository' do
it_behaves_like 'cache_key examples' do
let(:repository) { project.repository }
end
end
describe 'personal snippet repository' do
let_it_be(:personal_snippet) { create(:personal_snippet) }
2021-06-08 01:23:25 +05:30
2020-04-08 14:13:33 +05:30
let(:namespace) { repository.full_path }
2019-12-04 20:38:33 +05:30
2020-04-08 14:13:33 +05:30
it_behaves_like 'cache_key examples' do
let(:repository) { personal_snippet.repository }
end
end
describe 'project snippet repository' do
let_it_be(:project_snippet) { create(:project_snippet, project: project) }
it_behaves_like 'cache_key examples' do
let(:repository) { project_snippet.repository }
2019-12-04 20:38:33 +05:30
end
end
end
2021-09-04 01:27:46 +05:30
describe '#write' do
subject(:write_cache) { cache.write('branch_names', ['main']) }
it 'writes the value to the cache' do
write_cache
redis_keys = Gitlab::Redis::Cache.with { |redis| redis.scan(0, match: "*") }.last
2021-09-30 23:02:18 +05:30
expect(redis_keys).to include("#{gitlab_cache_namespace}:branch_names:#{namespace}:set")
2021-09-04 01:27:46 +05:30
expect(cache.fetch('branch_names')).to contain_exactly('main')
end
it 'sets the expiry of the set' do
write_cache
expect(cache.ttl('branch_names')).to be_within(1).of(cache.expires_in.seconds)
end
end
2019-12-04 20:38:33 +05:30
describe '#expire' do
2023-07-09 08:55:56 +05:30
shared_examples 'expires varying amount of keys' do
subject { cache.expire(*keys) }
2020-04-08 14:13:33 +05:30
2023-07-09 08:55:56 +05:30
before do
cache.write(:foo, ['value'])
cache.write(:bar, ['value2'])
end
2019-12-04 20:38:33 +05:30
2023-07-09 08:55:56 +05:30
it 'actually wrote the values' do
expect(cache.read(:foo)).to contain_exactly('value')
expect(cache.read(:bar)).to contain_exactly('value2')
end
2020-04-08 14:13:33 +05:30
2023-07-09 08:55:56 +05:30
context 'single key' do
let(:keys) { %w(foo) }
2020-04-08 14:13:33 +05:30
2023-07-09 08:55:56 +05:30
it { is_expected.to eq(1) }
2020-04-08 14:13:33 +05:30
2023-07-09 08:55:56 +05:30
it 'deletes the given key from the cache' do
subject
2020-04-08 14:13:33 +05:30
2023-07-09 08:55:56 +05:30
expect(cache.read(:foo)).to be_empty
end
2020-04-08 14:13:33 +05:30
end
2023-07-09 08:55:56 +05:30
context 'multiple keys' do
let(:keys) { %w(foo bar) }
2020-04-08 14:13:33 +05:30
2023-07-09 08:55:56 +05:30
it { is_expected.to eq(2) }
2020-04-08 14:13:33 +05:30
2023-07-09 08:55:56 +05:30
it 'deletes the given keys from the cache' do
subject
2020-04-08 14:13:33 +05:30
2023-07-09 08:55:56 +05:30
expect(cache.read(:foo)).to be_empty
expect(cache.read(:bar)).to be_empty
end
end
context 'no keys' do
let(:keys) { [] }
it { is_expected.to eq(0) }
2020-04-08 14:13:33 +05:30
end
end
2023-07-09 08:55:56 +05:30
context 'when feature flag is disabled' do
before do
stub_feature_flags(use_pipeline_over_multikey: false)
end
2020-04-08 14:13:33 +05:30
2023-07-09 08:55:56 +05:30
it_behaves_like 'expires varying amount of keys'
2020-04-08 14:13:33 +05:30
end
2023-07-09 08:55:56 +05:30
it_behaves_like 'expires varying amount of keys'
2019-12-04 20:38:33 +05:30
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
2021-04-29 21:17:54 +05:30
describe '#search' do
subject do
cache.search(:foo, 'val*') do
%w[value helloworld notvalmatch]
end
end
it 'returns search pattern matches from the key' do
is_expected.to contain_exactly('value')
end
end
2019-12-04 20:38:33 +05:30
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
2021-04-29 21:17:54 +05:30
describe '#try_include?' do
it 'checks existence of the redis set and inclusion' do
expect(cache.try_include?(:foo, 'value')).to eq([false, false])
cache.write(:foo, ['value'])
expect(cache.try_include?(:foo, 'value')).to eq([true, true])
expect(cache.try_include?(:foo, 'bar')).to eq([false, true])
end
end
2019-12-04 20:38:33 +05:30
end