2019-08-29 13:17:30 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::RepositorySetCache, :clean_gitlab_redis_cache do
|
2019-12-27 10:08:16 -05:00
|
|
|
let_it_be(:project) { create(:project) }
|
2021-04-20 20:11:06 -04:00
|
|
|
|
2019-08-29 13:17:30 -04:00
|
|
|
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) }
|
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
shared_examples 'cache_key examples' do
|
|
|
|
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 '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-04-20 20:11:06 -04:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
let(:namespace) { repository.full_path }
|
|
|
|
|
|
|
|
it_behaves_like 'cache_key examples' do
|
|
|
|
let(:repository) { personal_snippet.repository }
|
|
|
|
end
|
2019-08-29 13:17:30 -04:00
|
|
|
end
|
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
describe 'project snippet repository' do
|
|
|
|
let_it_be(:project_snippet) { create(:project_snippet, project: project) }
|
2019-08-29 13:17:30 -04:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
it_behaves_like 'cache_key examples' do
|
|
|
|
let(:repository) { project_snippet.repository }
|
2019-08-29 13:17:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#expire' do
|
2020-03-16 17:09:21 -04:00
|
|
|
subject { cache.expire(*keys) }
|
|
|
|
|
|
|
|
before do
|
2019-08-29 13:17:30 -04:00
|
|
|
cache.write(:foo, ['value'])
|
2020-03-16 17:09:21 -04:00
|
|
|
cache.write(:bar, ['value2'])
|
|
|
|
end
|
2019-08-29 13:17:30 -04:00
|
|
|
|
2020-03-16 17:09:21 -04:00
|
|
|
it 'actually wrote the values' do
|
2019-08-29 13:17:30 -04:00
|
|
|
expect(cache.read(:foo)).to contain_exactly('value')
|
2020-03-16 17:09:21 -04:00
|
|
|
expect(cache.read(:bar)).to contain_exactly('value2')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'single key' do
|
|
|
|
let(:keys) { %w(foo) }
|
|
|
|
|
|
|
|
it { is_expected.to eq(1) }
|
|
|
|
|
|
|
|
it 'deletes the given key from the cache' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(cache.read(:foo)).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'multiple keys' do
|
|
|
|
let(:keys) { %w(foo bar) }
|
|
|
|
|
|
|
|
it { is_expected.to eq(2) }
|
|
|
|
|
|
|
|
it 'deletes the given keys from the cache' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(cache.read(:foo)).to be_empty
|
|
|
|
expect(cache.read(:bar)).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-18 11:09:45 -04:00
|
|
|
context 'no keys' do
|
|
|
|
let(:keys) { [] }
|
|
|
|
|
|
|
|
it { is_expected.to eq(0) }
|
|
|
|
end
|
2019-08-29 13:17:30 -04:00
|
|
|
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-07 14:09:45 -04:00
|
|
|
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-08-29 13:17:30 -04:00
|
|
|
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-03-26 20:09:34 -04:00
|
|
|
|
|
|
|
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-08-29 13:17:30 -04:00
|
|
|
end
|