diff options
Diffstat (limited to 'spec/support/shared_examples/lib/cache_helpers_shared_examples.rb')
-rw-r--r-- | spec/support/shared_examples/lib/cache_helpers_shared_examples.rb | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/spec/support/shared_examples/lib/cache_helpers_shared_examples.rb b/spec/support/shared_examples/lib/cache_helpers_shared_examples.rb index 845fa78a827..82a9e8130f7 100644 --- a/spec/support/shared_examples/lib/cache_helpers_shared_examples.rb +++ b/spec/support/shared_examples/lib/cache_helpers_shared_examples.rb @@ -43,6 +43,54 @@ RSpec.shared_examples_for 'object cache helper' do subject end end + + context 'when a caller id is present' do + let(:transaction) { Gitlab::Metrics::WebTransaction.new({}) } + let(:caller_id) { 'caller_id' } + + before do + allow(::Gitlab::Metrics::WebTransaction).to receive(:current).and_return(transaction) + allow(transaction).to receive(:increment) + allow(Gitlab::ApplicationContext).to receive(:current_context_attribute).with(:caller_id).and_return(caller_id) + end + + context 'when feature flag is off' do + before do + stub_feature_flags(add_timing_to_certain_cache_actions: false) + end + + it 'does not call increment' do + expect(transaction).not_to receive(:increment).with(:cached_object_operations_total, any_args) + + subject + end + + it 'does not call histogram' do + expect(Gitlab::Metrics).not_to receive(:histogram) + + subject + end + + it "is valid JSON" do + parsed = Gitlab::Json.parse(subject.to_s) + + expect(parsed).to be_a(Hash) + expect(parsed["id"]).to eq(presentable.id) + end + end + + it 'increments the counter' do + expect(transaction) + .to receive(:increment) + .with(:cached_object_operations_total, 1, { caller_id: caller_id, render_type: :object, cache_hit: false }).once + + expect(transaction) + .to receive(:increment) + .with(:cached_object_operations_total, 0, { caller_id: caller_id, render_type: :object, cache_hit: true }).once + + subject + end + end end RSpec.shared_examples_for 'collection cache helper' do @@ -98,4 +146,95 @@ RSpec.shared_examples_for 'collection cache helper' do subject end end + + context 'when a caller id is present' do + let(:transaction) { Gitlab::Metrics::WebTransaction.new({}) } + let(:caller_id) { 'caller_id' } + + before do + allow(::Gitlab::Metrics::WebTransaction).to receive(:current).and_return(transaction) + allow(transaction).to receive(:increment) + allow(Gitlab::ApplicationContext).to receive(:current_context_attribute).with(:caller_id).and_return(caller_id) + end + + context 'when feature flag is off' do + before do + stub_feature_flags(add_timing_to_certain_cache_actions: false) + end + + it 'does not call increment' do + expect(transaction).not_to receive(:increment).with(:cached_object_operations_total, any_args) + + subject + end + + it 'does not call histogram' do + expect(Gitlab::Metrics).not_to receive(:histogram) + + subject + end + + it "is valid JSON" do + parsed = Gitlab::Json.parse(subject.to_s) + + expect(parsed).to be_an(Array) + + presentable.each_with_index do |item, i| + expect(parsed[i]["id"]).to eq(item.id) + end + end + end + + context 'when presentable has a group by clause' do + let(:presentable) { MergeRequest.group(:id) } + + it "returns the presentables" do + expect(transaction) + .to receive(:increment) + .with(:cached_object_operations_total, 0, { caller_id: caller_id, render_type: :collection, cache_hit: true }).once + + expect(transaction) + .to receive(:increment) + .with(:cached_object_operations_total, MergeRequest.count, { caller_id: caller_id, render_type: :collection, cache_hit: false }).once + + parsed = Gitlab::Json.parse(subject.to_s) + + expect(parsed).to be_an(Array) + + presentable.each_with_index do |item, i| + expect(parsed[i]["id"]).to eq(item.id) + end + end + end + + context 'when the presentables all miss' do + it 'increments the counters' do + expect(transaction) + .to receive(:increment) + .with(:cached_object_operations_total, 0, { caller_id: caller_id, render_type: :collection, cache_hit: true }).once + + expect(transaction) + .to receive(:increment) + .with(:cached_object_operations_total, presentable.size, { caller_id: caller_id, render_type: :collection, cache_hit: false }).once + + subject + end + end + + context 'when the presents hit' do + it 'increments the counters' do + subject + + expect(transaction) + .to receive(:increment) + .with(:cached_object_operations_total, presentable.size, { caller_id: caller_id, render_type: :collection, cache_hit: true }).once + + expect(transaction) + .to receive(:increment) + .with(:cached_object_operations_total, 0, { caller_id: caller_id, render_type: :collection, cache_hit: false }).once + + instance.public_send(method, presentable, **kwargs) + end + end + end end |