debian-mirror-gitlab/spec/lib/gitlab/repository_cache/preloader_spec.rb

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

57 lines
1.8 KiB
Ruby
Raw Permalink Normal View History

2021-11-11 11:23:49 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-03-17 16:20:25 +05:30
RSpec.describe Gitlab::RepositoryCache::Preloader, :use_clean_rails_redis_caching,
feature_category: :source_code_management do
2021-11-11 11:23:49 +05:30
let(:projects) { create_list(:project, 2, :repository) }
let(:repositories) { projects.map(&:repository) }
2023-04-23 21:23:45 +05:30
let(:cache) { Gitlab::RepositoryCache.store }
2021-11-11 11:23:49 +05:30
2023-04-23 21:23:45 +05:30
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)
2021-11-11 11:23:49 +05:30
2023-04-23 21:23:45 +05:30
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')
2023-03-17 16:20:25 +05:30
2023-04-23 21:23:45 +05:30
repos.map(&:exists?)
repos.map(&:readme_path)
2021-11-11 11:23:49 +05:30
end
2023-04-23 21:23:45 +05:30
it 'prevents individual cache reads for cached methods' do
expect(cache).to receive(:read_multi).once.and_call_original
2021-11-11 11:23:49 +05:30
2023-04-23 21:23:45 +05:30
described_class.new(repositories).preload(
%i[exists? readme_path]
)
2021-11-11 11:23:49 +05:30
2023-04-23 21:23:45 +05:30
expect(cache).not_to receive(:read)
expect(cache).not_to receive(:write)
2023-03-17 16:20:25 +05:30
2023-04-23 21:23:45 +05:30
expect(repositories[0].exists?).to eq(true)
expect(repositories[0].readme_path).to eq('README.txt')
2023-03-17 16:20:25 +05:30
2023-04-23 21:23:45 +05:30
expect(repositories[1].exists?).to eq(true)
expect(repositories[1].readme_path).to eq('README.md')
end
2023-03-17 16:20:25 +05:30
end
2023-04-23 21:23:45 +05:30
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?]
)
2023-03-17 16:20:25 +05:30
2023-04-23 21:23:45 +05:30
expect(cache).to receive(:read).exactly(4).times
expect(cache).to receive(:write).exactly(4).times
2023-03-17 16:20:25 +05:30
2023-04-23 21:23:45 +05:30
repositories.each(&:exists?)
repositories.each(&:has_visible_content?)
end
2023-03-17 16:20:25 +05:30
end
end
2021-11-11 11:23:49 +05:30
end