diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-09-20 16:18:24 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-09-20 16:18:24 +0300 |
commit | 0653e08efd039a5905f3fa4f6e9cef9f5d2f799c (patch) | |
tree | 4dcc884cf6d81db44adae4aa99f8ec1233a41f55 /spec/lib/gitlab/repository_cache | |
parent | 744144d28e3e7fddc117924fef88de5d9674fe4c (diff) |
Add latest changes from gitlab-org/gitlab@14-3-stable-eev14.3.0-rc42
Diffstat (limited to 'spec/lib/gitlab/repository_cache')
-rw-r--r-- | spec/lib/gitlab/repository_cache/preloader_spec.rb | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/spec/lib/gitlab/repository_cache/preloader_spec.rb b/spec/lib/gitlab/repository_cache/preloader_spec.rb new file mode 100644 index 00000000000..8c6618c9f8f --- /dev/null +++ b/spec/lib/gitlab/repository_cache/preloader_spec.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::RepositoryCache::Preloader, :use_clean_rails_redis_caching do + let(:projects) { create_list(:project, 2, :repository) } + let(:repositories) { projects.map(&:repository) } + + describe '#preload' do + context 'when the values are already cached' do + before do + # Warm the cache but use a different model so they are not memoized + repos = Project.id_in(projects).order(:id).map(&:repository) + + allow(repos[0].head_tree).to receive(:readme_path).and_return('README.txt') + allow(repos[1].head_tree).to receive(:readme_path).and_return('README.md') + + repos.map(&:exists?) + repos.map(&:readme_path) + end + + it 'prevents individual cache reads for cached methods' do + expect(Rails.cache).to receive(:read_multi).once.and_call_original + + described_class.new(repositories).preload( + %i[exists? readme_path] + ) + + expect(Rails.cache).not_to receive(:read) + expect(Rails.cache).not_to receive(:write) + + expect(repositories[0].exists?).to eq(true) + expect(repositories[0].readme_path).to eq('README.txt') + + expect(repositories[1].exists?).to eq(true) + expect(repositories[1].readme_path).to eq('README.md') + end + end + + context 'when values are not cached' do + it 'reads and writes from cache individually' do + described_class.new(repositories).preload( + %i[exists? has_visible_content?] + ) + + expect(Rails.cache).to receive(:read).exactly(4).times + expect(Rails.cache).to receive(:write).exactly(4).times + + repositories.each(&:exists?) + repositories.each(&:has_visible_content?) + end + end + end +end |