debian-mirror-gitlab/spec/lib/gitlab/metrics/subscribers/rails_cache_spec.rb

181 lines
4.9 KiB
Ruby
Raw Normal View History

2016-06-02 11:05:42 +05:30
require 'spec_helper'
describe Gitlab::Metrics::Subscribers::RailsCache do
let(:transaction) { Gitlab::Metrics::Transaction.new }
let(:subscriber) { described_class.new }
let(:event) { double(:event, duration: 15.2) }
describe '#cache_read' do
it 'increments the cache_read duration' do
2017-09-10 17:25:29 +05:30
expect(subscriber).to receive(:increment)
.with(:cache_read, event.duration)
2016-06-02 11:05:42 +05:30
subscriber.cache_read(event)
end
2016-08-24 12:49:21 +05:30
context 'with a transaction' do
before do
2017-09-10 17:25:29 +05:30
allow(subscriber).to receive(:current_transaction)
.and_return(transaction)
2016-08-24 12:49:21 +05:30
end
context 'with hit event' do
let(:event) { double(:event, duration: 15.2, payload: { hit: true }) }
it 'increments the cache_read_hit count' do
2017-09-10 17:25:29 +05:30
expect(transaction).to receive(:increment)
.with(:cache_read_hit_count, 1)
expect(transaction).to receive(:increment)
.with(any_args).at_least(1) # Other calls
2016-08-24 12:49:21 +05:30
subscriber.cache_read(event)
end
context 'when super operation is fetch' do
let(:event) { double(:event, duration: 15.2, payload: { hit: true, super_operation: :fetch }) }
it 'does not increment cache read miss' do
2017-09-10 17:25:29 +05:30
expect(transaction).not_to receive(:increment)
.with(:cache_read_hit_count, 1)
2016-08-24 12:49:21 +05:30
subscriber.cache_read(event)
end
end
end
context 'with miss event' do
let(:event) { double(:event, duration: 15.2, payload: { hit: false }) }
it 'increments the cache_read_miss count' do
2017-09-10 17:25:29 +05:30
expect(transaction).to receive(:increment)
.with(:cache_read_miss_count, 1)
expect(transaction).to receive(:increment)
.with(any_args).at_least(1) # Other calls
2016-08-24 12:49:21 +05:30
subscriber.cache_read(event)
end
context 'when super operation is fetch' do
let(:event) { double(:event, duration: 15.2, payload: { hit: false, super_operation: :fetch }) }
it 'does not increment cache read miss' do
2017-09-10 17:25:29 +05:30
expect(transaction).not_to receive(:increment)
.with(:cache_read_miss_count, 1)
2016-08-24 12:49:21 +05:30
subscriber.cache_read(event)
end
end
end
end
2016-06-02 11:05:42 +05:30
end
describe '#cache_write' do
it 'increments the cache_write duration' do
2017-09-10 17:25:29 +05:30
expect(subscriber).to receive(:increment)
.with(:cache_write, event.duration)
2016-06-02 11:05:42 +05:30
subscriber.cache_write(event)
end
end
describe '#cache_delete' do
it 'increments the cache_delete duration' do
2017-09-10 17:25:29 +05:30
expect(subscriber).to receive(:increment)
.with(:cache_delete, event.duration)
2016-06-02 11:05:42 +05:30
subscriber.cache_delete(event)
end
end
describe '#cache_exist?' do
it 'increments the cache_exists duration' do
2017-09-10 17:25:29 +05:30
expect(subscriber).to receive(:increment)
.with(:cache_exists, event.duration)
2016-06-02 11:05:42 +05:30
subscriber.cache_exist?(event)
end
end
2016-08-24 12:49:21 +05:30
describe '#cache_fetch_hit' do
context 'without a transaction' do
it 'returns' do
expect(transaction).not_to receive(:increment)
subscriber.cache_fetch_hit(event)
end
end
context 'with a transaction' do
before do
2017-09-10 17:25:29 +05:30
allow(subscriber).to receive(:current_transaction)
.and_return(transaction)
2016-08-24 12:49:21 +05:30
end
it 'increments the cache_read_hit count' do
2017-09-10 17:25:29 +05:30
expect(transaction).to receive(:increment)
.with(:cache_read_hit_count, 1)
2016-08-24 12:49:21 +05:30
subscriber.cache_fetch_hit(event)
end
end
end
describe '#cache_generate' do
context 'without a transaction' do
it 'returns' do
expect(transaction).not_to receive(:increment)
subscriber.cache_generate(event)
end
end
context 'with a transaction' do
before do
2017-09-10 17:25:29 +05:30
allow(subscriber).to receive(:current_transaction)
.and_return(transaction)
2016-08-24 12:49:21 +05:30
end
it 'increments the cache_fetch_miss count' do
2017-09-10 17:25:29 +05:30
expect(transaction).to receive(:increment)
.with(:cache_read_miss_count, 1)
2016-08-24 12:49:21 +05:30
subscriber.cache_generate(event)
end
end
end
2016-06-02 11:05:42 +05:30
describe '#increment' do
context 'without a transaction' do
it 'returns' do
expect(transaction).not_to receive(:increment)
subscriber.increment(:foo, 15.2)
end
end
context 'with a transaction' do
before do
2017-09-10 17:25:29 +05:30
allow(subscriber).to receive(:current_transaction)
.and_return(transaction)
2016-06-02 11:05:42 +05:30
end
it 'increments the total and specific cache duration' do
2017-09-10 17:25:29 +05:30
expect(transaction).to receive(:increment)
.with(:cache_duration, event.duration)
2016-06-02 11:05:42 +05:30
2017-09-10 17:25:29 +05:30
expect(transaction).to receive(:increment)
.with(:cache_count, 1)
2016-06-02 11:05:42 +05:30
2017-09-10 17:25:29 +05:30
expect(transaction).to receive(:increment)
.with(:cache_delete_duration, event.duration)
2016-06-02 11:05:42 +05:30
2017-09-10 17:25:29 +05:30
expect(transaction).to receive(:increment)
.with(:cache_delete_count, 1)
2016-06-02 11:05:42 +05:30
subscriber.increment(:cache_delete, event.duration)
end
end
end
end