diff options
| author | Pawel Chojnacki <pawel@chojnacki.ws> | 2018-01-18 13:37:53 +0100 |
|---|---|---|
| committer | Pawel Chojnacki <pawel@chojnacki.ws> | 2018-01-29 15:13:04 +0100 |
| commit | 67fcd0610cd19808089faac57562b19b407540be (patch) | |
| tree | 3d6c30e20d80e3650ed61bad3da78decb6d72e98 /spec | |
| parent | afeb7ce993f2106cb2a6c4e2741356e2ed039d7c (diff) | |
| download | gitlab-ce-67fcd0610cd19808089faac57562b19b407540be.tar.gz | |
Test if feature is respected
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/lib/gitlab/metrics/concern_spec.rb | 71 |
1 files changed, 52 insertions, 19 deletions
diff --git a/spec/lib/gitlab/metrics/concern_spec.rb b/spec/lib/gitlab/metrics/concern_spec.rb index de56ac4c275..fc5f19000ba 100644 --- a/spec/lib/gitlab/metrics/concern_spec.rb +++ b/spec/lib/gitlab/metrics/concern_spec.rb @@ -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 |
