diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 21:42:06 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 21:42:06 +0300 |
commit | 6e4e1050d9dba2b7b2523fdd1768823ab85feef4 (patch) | |
tree | 78be5963ec075d80116a932011d695dd33910b4e /spec/lib/gitlab/repository_cache_adapter_spec.rb | |
parent | 1ce776de4ae122aba3f349c02c17cebeaa8ecf07 (diff) |
Add latest changes from gitlab-org/gitlab@13-3-stable-ee
Diffstat (limited to 'spec/lib/gitlab/repository_cache_adapter_spec.rb')
-rw-r--r-- | spec/lib/gitlab/repository_cache_adapter_spec.rb | 86 |
1 files changed, 84 insertions, 2 deletions
diff --git a/spec/lib/gitlab/repository_cache_adapter_spec.rb b/spec/lib/gitlab/repository_cache_adapter_spec.rb index 3727217203e..c9ad79234d3 100644 --- a/spec/lib/gitlab/repository_cache_adapter_spec.rb +++ b/spec/lib/gitlab/repository_cache_adapter_spec.rb @@ -9,6 +9,89 @@ RSpec.describe Gitlab::RepositoryCacheAdapter do let(:redis_set_cache) { repository.send(:redis_set_cache) } let(:redis_hash_cache) { repository.send(:redis_hash_cache) } + describe '.cache_method_output_as_redis_set', :clean_gitlab_redis_cache, :aggregate_failures do + let(:klass) do + Class.new do + include Gitlab::RepositoryCacheAdapter # can't use described_class here + + def letters + %w(b a c) + end + cache_method_as_redis_set(:letters) + + def redis_set_cache + @redis_set_cache ||= Gitlab::RepositorySetCache.new(self) + end + + def full_path + 'foo/bar' + end + + def project + end + end + end + + let(:fake_repository) { klass.new } + + context 'with an existing repository' do + it 'caches the output, sorting the results' do + expect(fake_repository).to receive(:_uncached_letters).once.and_call_original + + 2.times do + expect(fake_repository.letters).to eq(%w(a b c)) + end + + expect(fake_repository.redis_set_cache.exist?(:letters)).to eq(true) + expect(fake_repository.instance_variable_get(:@letters)).to eq(%w(a b c)) + end + + context 'membership checks' do + context 'when the cache key does not exist' do + it 'calls the original method and populates the cache' do + expect(fake_repository.redis_set_cache.exist?(:letters)).to eq(false) + expect(fake_repository).to receive(:_uncached_letters).once.and_call_original + + # This populates the cache and memoizes the full result + expect(fake_repository.letters_include?('a')).to eq(true) + expect(fake_repository.letters_include?('d')).to eq(false) + expect(fake_repository.redis_set_cache.exist?(:letters)).to eq(true) + end + end + + context 'when the cache key exists' do + before do + fake_repository.redis_set_cache.write(:letters, %w(b a c)) + end + + it 'calls #include? on the set cache' do + expect(fake_repository.redis_set_cache) + .to receive(:include?).with(:letters, 'a').and_call_original + expect(fake_repository.redis_set_cache) + .to receive(:include?).with(:letters, 'd').and_call_original + + expect(fake_repository.letters_include?('a')).to eq(true) + expect(fake_repository.letters_include?('d')).to eq(false) + end + + it 'memoizes the result' do + expect(fake_repository.redis_set_cache) + .to receive(:include?).once.and_call_original + + expect(fake_repository.letters_include?('a')).to eq(true) + expect(fake_repository.letters_include?('a')).to eq(true) + + expect(fake_repository.redis_set_cache) + .to receive(:include?).once.and_call_original + + expect(fake_repository.letters_include?('d')).to eq(false) + expect(fake_repository.letters_include?('d')).to eq(false) + end + end + end + end + end + describe '#cache_method_output', :use_clean_rails_memory_store_caching do let(:fallback) { 10 } @@ -212,8 +295,7 @@ RSpec.describe Gitlab::RepositoryCacheAdapter do expect(cache).to receive(:expire).with(:rendered_readme) expect(cache).to receive(:expire).with(:branch_names) expect(redis_set_cache).to receive(:expire).with(:rendered_readme, :branch_names) - expect(redis_hash_cache).to receive(:delete).with(:rendered_readme) - expect(redis_hash_cache).to receive(:delete).with(:branch_names) + expect(redis_hash_cache).to receive(:delete).with(:rendered_readme, :branch_names) repository.expire_method_caches(%i(rendered_readme branch_names)) end |