gitlab-org--gitlab-foss/spec/lib/gitlab/metrics/methods_spec.rb

138 lines
4.1 KiB
Ruby
Raw Normal View History

2018-01-17 16:16:25 -05:00
require 'spec_helper'
2018-01-29 06:33:08 -05:00
describe Gitlab::Metrics::Methods do
subject { Class.new { include Gitlab::Metrics::Methods } }
2018-01-17 16:16:25 -05:00
shared_context 'metric' do |metric_type, *args|
let(:docstring) { 'description' }
2018-01-18 07:37:53 -05:00
let(:metric_name) { :sample_metric }
2018-01-17 16:16:25 -05:00
describe "#define_#{metric_type}" do
define_method(:call_define_metric_method) do |**args|
subject.__send__("define_#{metric_type}", metric_name, **args)
2018-01-19 08:54:40 -05:00
end
2018-01-17 16:16:25 -05:00
context 'metrics access method not defined' do
it "defines metrics accessing method" do
2018-01-18 07:37:53 -05:00
expect(subject).not_to respond_to(metric_name)
2018-01-17 16:16:25 -05:00
call_define_metric_method(docstring: docstring)
2018-01-17 16:16:25 -05:00
2018-01-18 07:37:53 -05:00
expect(subject).to respond_to(metric_name)
2018-01-17 16:16:25 -05:00
end
end
context 'metrics access method defined' do
before do
call_define_metric_method(docstring: docstring)
2018-01-17 16:16:25 -05:00
end
it 'raises error when trying to redefine method' do
expect { call_define_metric_method(docstring: docstring) }.to raise_error(ArgumentError)
2018-01-17 16:16:25 -05:00
end
context 'metric is not cached' do
it 'calls fetch_metric' do
2018-01-19 08:54:40 -05:00
expect(subject).to receive(:init_metric).with(metric_type, metric_name, docstring: docstring)
2018-01-17 16:16:25 -05:00
2018-01-22 14:53:14 -05:00
subject.public_send(metric_name)
2018-01-17 16:16:25 -05:00
end
end
context 'metric is cached' do
before do
2018-01-22 14:53:14 -05:00
subject.public_send(metric_name)
2018-01-17 16:16:25 -05:00
end
it 'returns cached metric' do
2018-01-19 08:54:40 -05:00
expect(subject).not_to receive(:init_metric)
2018-01-17 16:16:25 -05:00
2018-01-22 14:53:14 -05:00
subject.public_send(metric_name)
2018-01-17 16:16:25 -05:00
end
end
end
end
describe "#fetch_#{metric_type}" do
2018-01-19 08:54:06 -05:00
let(:null_metric) { Gitlab::Metrics::NullMetric.instance }
2018-01-17 16:16:25 -05:00
define_method(:call_fetch_metric_method) do |**args|
subject.__send__("fetch_#{metric_type}", metric_name, **args)
2018-01-19 08:54:40 -05:00
end
2018-01-18 07:37:53 -05:00
context "when #{metric_type} is not cached" do
2018-01-17 16:16:25 -05:00
it 'initializes counter metric' do
allow(Gitlab::Metrics).to receive(metric_type).and_return(null_metric)
call_fetch_metric_method(docstring: docstring)
2018-01-17 16:16:25 -05:00
2018-01-18 07:37:53 -05:00
expect(Gitlab::Metrics).to have_received(metric_type).with(metric_name, docstring, *args)
2018-01-17 16:16:25 -05:00
end
end
2018-01-18 07:37:53 -05:00
context "when #{metric_type} is cached" do
2018-01-17 16:16:25 -05:00
before do
call_fetch_metric_method(docstring: docstring)
2018-01-17 16:16:25 -05:00
end
it 'uses class metric cache' do
expect(Gitlab::Metrics).not_to receive(metric_type)
call_fetch_metric_method(docstring: docstring)
2018-01-17 16:16:25 -05:00
end
context 'when metric is reloaded' do
before do
2018-01-18 07:37:53 -05:00
subject.reload_metric!(metric_name)
2018-01-17 16:16:25 -05:00
end
it "initializes #{metric_type} metric" do
allow(Gitlab::Metrics).to receive(metric_type).and_return(null_metric)
call_fetch_metric_method(docstring: docstring)
2018-01-17 16:16:25 -05:00
2018-01-18 07:37:53 -05:00
expect(Gitlab::Metrics).to have_received(metric_type).with(metric_name, docstring, *args)
end
end
end
context 'when metric is configured with feature' do
let(:feature_name) { :some_metric_feature }
let(:metric) { call_fetch_metric_method(docstring: docstring, with_feature: feature_name) }
2018-01-18 07:37:53 -05:00
context 'when feature is enabled' do
before do
Feature.get(feature_name).enable
end
it "initializes #{metric_type} metric" do
allow(Gitlab::Metrics).to receive(metric_type).and_return(null_metric)
metric
expect(Gitlab::Metrics).to have_received(metric_type).with(metric_name, docstring, *args)
end
end
context 'when feature is disabled' do
before do
Feature.get(feature_name).disable
end
it "returns NullMetric" do
2018-01-19 08:54:40 -05:00
allow(Gitlab::Metrics).to receive(metric_type)
2018-01-18 07:37:53 -05:00
expect(metric).to be_instance_of(Gitlab::Metrics::NullMetric)
expect(Gitlab::Metrics).not_to have_received(metric_type)
2018-01-17 16:16:25 -05:00
end
end
end
end
end
include_examples 'metric', :counter, {}
include_examples 'metric', :gauge, {}, :all
2018-01-18 14:22:49 -05:00
include_examples 'metric', :histogram, {}, [0.005, 0.01, 0.1, 1, 10]
2018-01-17 16:16:25 -05:00
end