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/metrics/dashboard/cache_spec.rb | |
parent | 1ce776de4ae122aba3f349c02c17cebeaa8ecf07 (diff) |
Add latest changes from gitlab-org/gitlab@13-3-stable-ee
Diffstat (limited to 'spec/lib/gitlab/metrics/dashboard/cache_spec.rb')
-rw-r--r-- | spec/lib/gitlab/metrics/dashboard/cache_spec.rb | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/spec/lib/gitlab/metrics/dashboard/cache_spec.rb b/spec/lib/gitlab/metrics/dashboard/cache_spec.rb new file mode 100644 index 00000000000..9467d441ae1 --- /dev/null +++ b/spec/lib/gitlab/metrics/dashboard/cache_spec.rb @@ -0,0 +1,86 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Metrics::Dashboard::Cache, :use_clean_rails_memory_store_caching do + let_it_be(:project1) { build_stubbed(:project) } + let_it_be(:project2) { build_stubbed(:project) } + + let(:project1_key1) { "#{project1.id}_key1" } + let(:project1_key2) { "#{project1.id}_key2" } + let(:project2_key1) { "#{project2.id}_key1" } + + let(:cache1) { described_class.for(project1) } + let(:cache2) { described_class.for(project2) } + + before do + cache1.fetch(project1_key1) { 'data1' } + cache1.fetch(project1_key2) { 'data2' } + cache2.fetch(project2_key1) { 'data3' } + end + + describe '.fetch' do + it 'stores data correctly' do + described_class.fetch('key1') { 'data1' } + described_class.fetch('key2') { 'data2' } + + expect(described_class.fetch('key1')).to eq('data1') + expect(described_class.fetch('key2')).to eq('data2') + end + end + + describe '.for' do + it 'returns a new instance' do + expect(described_class.for(project1)).to be_instance_of(described_class) + end + end + + describe '#fetch' do + it 'stores data correctly' do + expect(cache1.fetch(project1_key1)).to eq('data1') + expect(cache1.fetch(project1_key2)).to eq('data2') + expect(cache2.fetch(project2_key1)).to eq('data3') + end + end + + describe '#delete_all!' do + it 'deletes keys of the given project', :aggregate_failures do + cache1.delete_all! + + expect(Rails.cache.exist?(project1_key1)).to be(false) + expect(Rails.cache.exist?(project1_key2)).to be(false) + expect(cache2.fetch(project2_key1)).to eq('data3') + + cache2.delete_all! + + expect(Rails.cache.exist?(project2_key1)).to be(false) + end + + it 'does not fail when nothing to delete' do + project3 = build_stubbed(:project) + cache3 = described_class.for(project3) + + expect { cache3.delete_all! }.not_to raise_error + end + end + + context 'multiple fetches and deletes' do + specify :aggregate_failures do + cache1.delete_all! + + expect(Rails.cache.exist?(project1_key1)).to be(false) + expect(Rails.cache.exist?(project1_key2)).to be(false) + + cache1.fetch("#{project1.id}_key3") { 'data1' } + cache1.fetch("#{project1.id}_key4") { 'data2' } + + expect(cache1.fetch("#{project1.id}_key3")).to eq('data1') + expect(cache1.fetch("#{project1.id}_key4")).to eq('data2') + + cache1.delete_all! + + expect(Rails.cache.exist?("#{project1.id}_key3")).to be(false) + expect(Rails.cache.exist?("#{project1.id}_key4")).to be(false) + end + end +end |