2020-03-13 15:44:24 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "spec_helper"
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::RepositoryHashCache, :clean_gitlab_redis_cache do
|
2020-03-13 15:44:24 +05:30
|
|
|
let_it_be(:project) { create(:project) }
|
2021-06-08 01:23:25 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
let(:repository) { project.repository }
|
|
|
|
let(:namespace) { "#{repository.full_path}:#{project.id}" }
|
|
|
|
let(:cache) { described_class.new(repository) }
|
|
|
|
let(:test_hash) do
|
|
|
|
{ "test" => "value" }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#cache_key" do
|
|
|
|
subject { cache.cache_key(:example) }
|
|
|
|
|
|
|
|
it "includes the namespace" do
|
|
|
|
is_expected.to eq("example:#{namespace}:hash")
|
|
|
|
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("example:#{namespace}:#{extra_namespace}:hash")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#delete" do
|
|
|
|
subject { cache.delete(:example) }
|
|
|
|
|
|
|
|
context "key exists" do
|
|
|
|
before do
|
|
|
|
cache.write(:example, test_hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(1) }
|
|
|
|
|
|
|
|
it "deletes the given key from the cache" do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(cache.read_members(:example, ["test"])).to eq({ "test" => nil })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "key doesn't exist" do
|
|
|
|
it { is_expected.to eq(0) }
|
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
context "multiple keys" do
|
|
|
|
before do
|
|
|
|
cache.write(:test1, test_hash)
|
|
|
|
cache.write(:test2, test_hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes multiple keys" do
|
|
|
|
cache.delete(:test1, :test2)
|
|
|
|
|
|
|
|
expect(cache.read_members(:test1, ["test"])).to eq("test" => nil)
|
|
|
|
expect(cache.read_members(:test2, ["test"])).to eq("test" => nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns deleted key count" do
|
|
|
|
expect(cache.delete(:test1, :test2)).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
shared_examples "key?" do
|
|
|
|
describe "#key?" do
|
|
|
|
subject { cache.key?(:example, "test") }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
context "key exists" do
|
|
|
|
before do
|
|
|
|
cache.write(:example, test_hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be(true) }
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
context "key doesn't exist" do
|
|
|
|
it { is_expected.to be(false) }
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2023-03-17 16:20:25 +05:30
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
context "when both multistore FF is enabled" do
|
|
|
|
it_behaves_like "key?"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when both multistore FF is disabled" do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(use_primary_and_secondary_stores_for_repository_cache: false)
|
|
|
|
stub_feature_flags(use_primary_store_as_default_for_repository_cache: false)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2023-03-17 16:20:25 +05:30
|
|
|
|
|
|
|
it_behaves_like "key?"
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe "#read_members" do
|
|
|
|
subject { cache.read_members(:example, keys) }
|
|
|
|
|
|
|
|
let(:keys) { %w(test missing) }
|
|
|
|
|
|
|
|
context "all data is cached" do
|
|
|
|
before do
|
|
|
|
cache.write(:example, test_hash.merge({ "missing" => false }))
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq({ "test" => "value", "missing" => "false" }) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "partial data is cached" do
|
|
|
|
before do
|
|
|
|
cache.write(:example, test_hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq({ "test" => "value", "missing" => nil }) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "no data is cached" do
|
|
|
|
it { is_expected.to eq({ "test" => nil, "missing" => nil }) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "empty keys are passed for some reason" do
|
|
|
|
let(:keys) { [] }
|
|
|
|
|
|
|
|
it "raises an error" do
|
|
|
|
expect { subject }.to raise_error(Gitlab::RepositoryHashCache::InvalidKeysProvidedError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#write" do
|
|
|
|
subject { cache.write(:example, test_hash) }
|
|
|
|
|
|
|
|
it { is_expected.to be(true) }
|
|
|
|
|
|
|
|
it "actually writes stuff to Redis" do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(cache.read_members(:example, ["test"])).to eq(test_hash)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#fetch_and_add_missing" do
|
|
|
|
subject do
|
|
|
|
cache.fetch_and_add_missing(:example, keys) do |missing_keys, hash|
|
|
|
|
missing_keys.each do |key|
|
|
|
|
hash[key] = "was_missing"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:keys) { %w(test) }
|
|
|
|
|
|
|
|
it "records metrics" do
|
|
|
|
# Here we expect it to receive "test" as a missing key because we
|
|
|
|
# don't write to the cache before this test
|
|
|
|
expect(cache).to receive(:record_metrics).with(:example, { "test" => "was_missing" }, ["test"])
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
context "fully cached" do
|
|
|
|
let(:keys) { %w(test another) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
cache.write(:example, test_hash.merge({ "another" => "not_missing" }))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a hash" do
|
|
|
|
is_expected.to eq({ "test" => "value", "another" => "not_missing" })
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't write to the cache" do
|
|
|
|
expect(cache).not_to receive(:write)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "partially cached" do
|
|
|
|
let(:keys) { %w(test missing) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
cache.write(:example, test_hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a hash" do
|
|
|
|
is_expected.to eq({ "test" => "value", "missing" => "was_missing" })
|
|
|
|
end
|
|
|
|
|
|
|
|
it "writes to the cache" do
|
|
|
|
expect(cache).to receive(:write).with(:example, { "missing" => "was_missing" })
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "uncached" do
|
|
|
|
let(:keys) { %w(test missing) }
|
|
|
|
|
|
|
|
it "returns a hash" do
|
|
|
|
is_expected.to eq({ "test" => "was_missing", "missing" => "was_missing" })
|
|
|
|
end
|
|
|
|
|
|
|
|
it "writes to the cache" do
|
|
|
|
expect(cache).to receive(:write).with(:example, { "test" => "was_missing", "missing" => "was_missing" })
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|