Commit 67fcd061 authored by Pawel Chojnacki's avatar Pawel Chojnacki

Test if feature is respected

parent afeb7ce9
......@@ -44,9 +44,9 @@ module Gitlab
# Use feature toggle to control whether certain metric is enabled/disabled
def with_feature(name = nil)
@feature_name = name unless name.nil?
@with_feature = name unless name.nil?
@feature_name
@with_feature
end
def evaluate(&block)
......
......@@ -5,47 +5,47 @@ describe Gitlab::Metrics::Concern do
shared_context 'metric' do |metric_type, *args|
let(:docstring) { 'description' }
let(:metric) { :sample_metric }
let(:metric_name) { :sample_metric }
describe "#define_#{metric_type}" do
let(:define_method) { "define_#{metric_type}".to_sym }
context 'metrics access method not defined' do
it "defines metrics accessing method" do
expect(subject).not_to respond_to(metric)
expect(subject).not_to respond_to(metric_name)
subject.send(define_method, metric, docstring: docstring)
subject.send(define_method, metric_name, docstring: docstring)
expect(subject).to respond_to(metric)
expect(subject).to respond_to(metric_name)
end
end
context 'metrics access method defined' do
before do
subject.send(define_method, metric, docstring: docstring)
subject.send(define_method, metric_name, docstring: docstring)
end
it 'raises error when trying to redefine method' do
expect { subject.send(define_method, metric, docstring: docstring) }.to raise_error(ArgumentError)
expect { subject.send(define_method, metric_name, docstring: docstring) }.to raise_error(ArgumentError)
end
context 'metric is not cached' do
it 'calls fetch_metric' do
expect(subject).to receive(:fetch_metric).with(metric_type, metric, docstring: docstring)
expect(subject).to receive(:fetch_metric).with(metric_type, metric_name, docstring: docstring)
subject.send(metric)
subject.send(metric_name)
end
end
context 'metric is cached' do
before do
subject.send(metric)
subject.send(metric_name)
end
it 'returns cached metric' do
expect(subject).not_to receive(:fetch_metric)
subject.send(metric)
subject.send(metric_name)
end
end
end
......@@ -55,38 +55,71 @@ describe Gitlab::Metrics::Concern do
let(:fetch_method) { "fetch_#{metric_type}".to_sym }
let(:null_metric) { Gitlab::Metrics::NullMetric.new }
context "when #{metric_type} fetched first time" do
context "when #{metric_type} is not cached" do
it 'initializes counter metric' do
allow(Gitlab::Metrics).to receive(metric_type).and_return(null_metric)
subject.send(fetch_method, metric, docstring: docstring)
subject.send(fetch_method, metric_name, docstring: docstring)
expect(Gitlab::Metrics).to have_received(metric_type).with(metric, docstring, *args)
expect(Gitlab::Metrics).to have_received(metric_type).with(metric_name, docstring, *args)
end
end
context "when #{metric_type} is fetched second time" do
context "when #{metric_type} is cached" do
before do
subject.send(fetch_method, metric, docstring: docstring)
subject.send(fetch_method, metric_name, docstring: docstring)
end
it 'uses class metric cache' do
expect(Gitlab::Metrics).not_to receive(metric_type)
subject.send(fetch_method, metric, docstring: docstring)
subject.send(fetch_method, metric_name, docstring: docstring)
end
context 'when metric is reloaded' do
before do
subject.reload_metric!(metric)
subject.reload_metric!(metric_name)
end
it "initializes #{metric_type} metric" do
allow(Gitlab::Metrics).to receive(metric_type).and_return(null_metric)
subject.send(fetch_method, metric, docstring: docstring)
subject.send(fetch_method, metric_name, docstring: docstring)
expect(Gitlab::Metrics).to have_received(metric_type).with(metric, docstring, *args)
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) { subject.send(fetch_method, metric_name, docstring: docstring, with_feature: feature_name) }
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
allow(Gitlab::Metrics).to receive(metric_type).and_return(null_metric)
expect(metric).to be_instance_of(Gitlab::Metrics::NullMetric)
expect(Gitlab::Metrics).not_to have_received(metric_type)
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment