2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-24 13:39:28 -04:00
|
|
|
require 'spec_helper'
|
2015-01-28 23:08:28 -05:00
|
|
|
|
2018-03-06 19:12:29 -05:00
|
|
|
describe Gitlab::RepositoryCache do
|
2019-12-27 10:08:16 -05:00
|
|
|
let_it_be(:project) { create(:project) }
|
2015-01-28 23:08:28 -05:00
|
|
|
let(:backend) { double('backend').as_null_object }
|
2018-03-06 19:12:29 -05:00
|
|
|
let(:repository) { project.repository }
|
2019-01-08 14:37:54 -05:00
|
|
|
let(:namespace) { "#{repository.full_path}:#{project.id}" }
|
2018-03-06 20:25:12 -05:00
|
|
|
let(:cache) { described_class.new(repository, backend: backend) }
|
2015-01-28 23:08:28 -05:00
|
|
|
|
|
|
|
describe '#cache_key' do
|
2018-03-06 20:25:12 -05:00
|
|
|
subject { cache.cache_key(:foo) }
|
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
shared_examples 'cache_key examples' do
|
|
|
|
it 'includes the namespace' do
|
|
|
|
expect(subject).to eq "foo:#{namespace}"
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a given namespace' do
|
|
|
|
let(:extra_namespace) { 'my:data' }
|
|
|
|
let(:cache) do
|
|
|
|
described_class.new(repository, extra_namespace: extra_namespace,
|
|
|
|
backend: backend)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the full namespace' do
|
|
|
|
expect(subject).to eq "foo:#{namespace}:#{extra_namespace}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'project repository' do
|
|
|
|
it_behaves_like 'cache_key examples' do
|
|
|
|
let(:repository) { project.repository }
|
|
|
|
end
|
2018-03-06 20:25:12 -05:00
|
|
|
end
|
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
describe 'personal snippet repository' do
|
|
|
|
let_it_be(:personal_snippet) { create(:personal_snippet) }
|
|
|
|
let(:namespace) { repository.full_path }
|
|
|
|
|
|
|
|
it_behaves_like 'cache_key examples' do
|
|
|
|
let(:repository) { personal_snippet.repository }
|
2018-03-06 20:25:12 -05:00
|
|
|
end
|
2020-03-04 19:07:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'project snippet repository' do
|
|
|
|
let_it_be(:project_snippet) { create(:project_snippet, project: project) }
|
2018-03-06 20:25:12 -05:00
|
|
|
|
2020-03-04 19:07:49 -05:00
|
|
|
it_behaves_like 'cache_key examples' do
|
|
|
|
let(:repository) { project_snippet.repository }
|
2018-03-06 20:25:12 -05:00
|
|
|
end
|
2015-01-28 23:08:28 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#expire' do
|
|
|
|
it 'expires the given key from the cache' do
|
|
|
|
cache.expire(:foo)
|
2019-01-08 14:37:54 -05:00
|
|
|
expect(backend).to have_received(:delete).with("foo:#{namespace}")
|
2015-01-28 23:08:28 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#fetch' do
|
|
|
|
it 'fetches the given key from the cache' do
|
|
|
|
cache.fetch(:bar)
|
2019-01-08 14:37:54 -05:00
|
|
|
expect(backend).to have_received(:fetch).with("bar:#{namespace}")
|
2015-01-28 23:08:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts a block' do
|
|
|
|
p = -> {}
|
|
|
|
|
|
|
|
cache.fetch(:baz, &p)
|
2019-01-08 14:37:54 -05:00
|
|
|
expect(backend).to have_received(:fetch).with("baz:#{namespace}", &p)
|
2015-01-28 23:08:28 -05:00
|
|
|
end
|
|
|
|
end
|
2018-09-25 13:12:51 -04:00
|
|
|
|
2020-02-03 13:08:46 -05:00
|
|
|
describe '#write' do
|
|
|
|
it 'writes the given key and value to the cache' do
|
|
|
|
cache.write(:test, 'test')
|
|
|
|
expect(backend).to have_received(:write).with("test:#{namespace}", 'test')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'passes additional options to the backend' do
|
|
|
|
cache.write(:test, 'test', expires_in: 10.minutes)
|
|
|
|
expect(backend).to have_received(:write).with("test:#{namespace}", 'test', expires_in: 10.minutes)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-25 13:12:51 -04:00
|
|
|
describe '#fetch_without_caching_false', :use_clean_rails_memory_store_caching do
|
|
|
|
let(:key) { :foo }
|
|
|
|
let(:backend) { Rails.cache }
|
|
|
|
|
|
|
|
it 'requires a block' do
|
|
|
|
expect do
|
|
|
|
cache.fetch_without_caching_false(key)
|
|
|
|
end.to raise_error(LocalJumpError)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the 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_without_caching_false(key) { true }
|
|
|
|
|
|
|
|
expect(result).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'caches the value' do
|
2019-01-08 14:37:54 -05:00
|
|
|
expect(backend).to receive(:write).with("#{key}:#{namespace}", true)
|
2018-09-25 13:12:51 -04:00
|
|
|
|
|
|
|
cache.fetch_without_caching_false(key) { true }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the result of the block is falsey' do
|
|
|
|
let(:p) { -> { false } }
|
|
|
|
|
|
|
|
it 'returns the result of the block' do
|
|
|
|
result = cache.fetch_without_caching_false(key, &p)
|
|
|
|
|
|
|
|
expect(result).to be false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not cache the value' do
|
2019-01-08 14:37:54 -05:00
|
|
|
expect(backend).not_to receive(:write).with("#{key}:#{namespace}", true)
|
2018-09-25 13:12:51 -04:00
|
|
|
|
|
|
|
cache.fetch_without_caching_false(key, &p)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the cached value is truthy' do
|
|
|
|
before do
|
2019-01-08 14:37:54 -05:00
|
|
|
backend.write("#{key}:#{namespace}", true)
|
2018-09-25 13:12:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the cached value' do
|
|
|
|
result = cache.fetch_without_caching_false(key) { 'block result' }
|
|
|
|
|
|
|
|
expect(result).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not execute the block' do
|
|
|
|
expect do |b|
|
|
|
|
cache.fetch_without_caching_false(key, &b)
|
|
|
|
end.not_to yield_control
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not write to the cache' do
|
|
|
|
expect(backend).not_to receive(:write)
|
|
|
|
|
|
|
|
cache.fetch_without_caching_false(key) { 'block result' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the cached value is falsey' do
|
|
|
|
before do
|
2019-01-08 14:37:54 -05:00
|
|
|
backend.write("#{key}:#{namespace}", false)
|
2018-09-25 13:12:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the result of the block' do
|
|
|
|
result = cache.fetch_without_caching_false(key) { 'block result' }
|
|
|
|
|
|
|
|
expect(result).to eq 'block result'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'writes the truthy value to the cache' do
|
2019-01-08 14:37:54 -05:00
|
|
|
expect(backend).to receive(:write).with("#{key}:#{namespace}", 'block result')
|
2018-09-25 13:12:51 -04:00
|
|
|
|
|
|
|
cache.fetch_without_caching_false(key) { 'block result' }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-01-28 23:08:28 -05:00
|
|
|
end
|