diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-12 21:09:21 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-12 21:09:21 +0300 |
commit | 43e3dc2f95a25c600e08f65d4f1c406a1a63ed3d (patch) | |
tree | db5c72020c7c8916020c8aff7c1b7128028d650b /spec/lib | |
parent | 2c89e169769ead722394a79ed67fcd08e96863dd (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib')
-rw-r--r-- | spec/lib/gitlab/redis/boolean_spec.rb | 150 | ||||
-rw-r--r-- | spec/lib/gitlab/repository_cache_adapter_spec.rb | 3 | ||||
-rw-r--r-- | spec/lib/gitlab/repository_hash_cache_spec.rb | 184 | ||||
-rw-r--r-- | spec/lib/quality/test_level_spec.rb | 4 |
4 files changed, 339 insertions, 2 deletions
diff --git a/spec/lib/gitlab/redis/boolean_spec.rb b/spec/lib/gitlab/redis/boolean_spec.rb new file mode 100644 index 00000000000..bfacf0c448b --- /dev/null +++ b/spec/lib/gitlab/redis/boolean_spec.rb @@ -0,0 +1,150 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Gitlab::Redis::Boolean do + subject(:redis_boolean) { described_class.new(bool) } + + let(:bool) { true } + let(:label_section) { "#{described_class::LABEL}#{described_class::DELIMITER}" } + + describe "#to_s" do + subject { redis_boolean.to_s } + + context "true" do + let(:bool) { true } + + it { is_expected.to eq("#{label_section}#{described_class::TRUE_STR}") } + end + + context "false" do + let(:bool) { false } + + it { is_expected.to eq("#{label_section}#{described_class::FALSE_STR}") } + end + end + + describe ".encode" do + subject { redis_boolean.class.encode(bool) } + + context "true" do + let(:bool) { true } + + it { is_expected.to eq("#{label_section}#{described_class::TRUE_STR}") } + end + + context "false" do + let(:bool) { false } + + it { is_expected.to eq("#{label_section}#{described_class::FALSE_STR}") } + end + end + + describe ".decode" do + subject { redis_boolean.class.decode(str) } + + context "valid encoded bool" do + let(:str) { "#{label_section}#{bool_str}" } + + context "true" do + let(:bool_str) { described_class::TRUE_STR } + + it { is_expected.to be(true) } + end + + context "false" do + let(:bool_str) { described_class::FALSE_STR } + + it { is_expected.to be(false) } + end + end + + context "partially invalid bool" do + let(:str) { "#{label_section}whoops" } + + it "raises an error" do + expect { subject }.to raise_error(described_class::NotAnEncodedBooleanStringError) + end + end + + context "invalid encoded bool" do + let(:str) { "whoops" } + + it "raises an error" do + expect { subject }.to raise_error(described_class::NotAnEncodedBooleanStringError) + end + end + end + + describe ".true?" do + subject { redis_boolean.class.true?(str) } + + context "valid encoded bool" do + let(:str) { "#{label_section}#{bool_str}" } + + context "true" do + let(:bool_str) { described_class::TRUE_STR } + + it { is_expected.to be(true) } + end + + context "false" do + let(:bool_str) { described_class::FALSE_STR } + + it { is_expected.to be(false) } + end + end + + context "partially invalid bool" do + let(:str) { "#{label_section}whoops" } + + it "raises an error" do + expect { subject }.to raise_error(described_class::NotAnEncodedBooleanStringError) + end + end + + context "invalid encoded bool" do + let(:str) { "whoops" } + + it "raises an error" do + expect { subject }.to raise_error(described_class::NotAnEncodedBooleanStringError) + end + end + end + + describe ".false?" do + subject { redis_boolean.class.false?(str) } + + context "valid encoded bool" do + let(:str) { "#{label_section}#{bool_str}" } + + context "true" do + let(:bool_str) { described_class::TRUE_STR } + + it { is_expected.to be(false) } + end + + context "false" do + let(:bool_str) { described_class::FALSE_STR } + + it { is_expected.to be(true) } + end + end + + context "partially invalid bool" do + let(:str) { "#{label_section}whoops" } + + it "raises an error" do + expect { subject }.to raise_error(described_class::NotAnEncodedBooleanStringError) + end + end + + context "invalid encoded bool" do + let(:str) { "whoops" } + + it "raises an error" do + expect { subject }.to raise_error(described_class::NotAnEncodedBooleanStringError) + end + end + end +end diff --git a/spec/lib/gitlab/repository_cache_adapter_spec.rb b/spec/lib/gitlab/repository_cache_adapter_spec.rb index fd1338b55a6..b4fc504ea60 100644 --- a/spec/lib/gitlab/repository_cache_adapter_spec.rb +++ b/spec/lib/gitlab/repository_cache_adapter_spec.rb @@ -7,6 +7,7 @@ describe Gitlab::RepositoryCacheAdapter do let(:repository) { project.repository } let(:cache) { repository.send(:cache) } let(:redis_set_cache) { repository.send(:redis_set_cache) } + let(:redis_hash_cache) { repository.send(:redis_hash_cache) } describe '#cache_method_output', :use_clean_rails_memory_store_caching do let(:fallback) { 10 } @@ -212,6 +213,8 @@ describe Gitlab::RepositoryCacheAdapter do expect(cache).to receive(:expire).with(:branch_names) expect(redis_set_cache).to receive(:expire).with(:rendered_readme) expect(redis_set_cache).to receive(:expire).with(:branch_names) + expect(redis_hash_cache).to receive(:delete).with(:rendered_readme) + expect(redis_hash_cache).to receive(:delete).with(:branch_names) repository.expire_method_caches(%i(rendered_readme branch_names)) end diff --git a/spec/lib/gitlab/repository_hash_cache_spec.rb b/spec/lib/gitlab/repository_hash_cache_spec.rb new file mode 100644 index 00000000000..014a2f235b9 --- /dev/null +++ b/spec/lib/gitlab/repository_hash_cache_spec.rb @@ -0,0 +1,184 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Gitlab::RepositoryHashCache, :clean_gitlab_redis_cache do + let_it_be(:project) { create(:project) } + 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 + end + + describe "#key?" do + subject { cache.key?(:example, "test") } + + context "key exists" do + before do + cache.write(:example, test_hash) + end + + it { is_expected.to be(true) } + end + + context "key doesn't exist" do + it { is_expected.to be(false) } + end + 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 diff --git a/spec/lib/quality/test_level_spec.rb b/spec/lib/quality/test_level_spec.rb index 621a426a18d..757a003946b 100644 --- a/spec/lib/quality/test_level_spec.rb +++ b/spec/lib/quality/test_level_spec.rb @@ -21,7 +21,7 @@ RSpec.describe Quality::TestLevel do context 'when level is unit' do it 'returns a pattern' do expect(subject.pattern(:unit)) - .to eq("spec/{bin,config,db,dependencies,factories,finders,frontend,graphql,haml_lint,helpers,initializers,javascripts,lib,models,policies,presenters,rack_servers,replicators,routing,rubocop,serializers,services,sidekiq,tasks,uploaders,validators,views,workers,elastic_integration}{,/**/}*_spec.rb") + .to eq("spec/{bin,config,db,dependencies,factories,finders,frontend,graphql,haml_lint,helpers,initializers,javascripts,lib,models,policies,presenters,rack_servers,replicators,routing,rubocop,serializers,services,sidekiq,support_specs,tasks,uploaders,validators,views,workers,elastic_integration}{,/**/}*_spec.rb") end end @@ -82,7 +82,7 @@ RSpec.describe Quality::TestLevel do context 'when level is unit' do it 'returns a regexp' do expect(subject.regexp(:unit)) - .to eq(%r{spec/(bin|config|db|dependencies|factories|finders|frontend|graphql|haml_lint|helpers|initializers|javascripts|lib|models|policies|presenters|rack_servers|replicators|routing|rubocop|serializers|services|sidekiq|tasks|uploaders|validators|views|workers|elastic_integration)}) + .to eq(%r{spec/(bin|config|db|dependencies|factories|finders|frontend|graphql|haml_lint|helpers|initializers|javascripts|lib|models|policies|presenters|rack_servers|replicators|routing|rubocop|serializers|services|sidekiq|support_specs|tasks|uploaders|validators|views|workers|elastic_integration)}) end end |