Commit 83ae1721 authored by Pawel Chojnacki's avatar Pawel Chojnacki

Cleanup rspec

parent f5383578
...@@ -13,23 +13,19 @@ module Gitlab ...@@ -13,23 +13,19 @@ module Gitlab
def define_metric(type, name, opts = {}, &block) def define_metric(type, name, opts = {}, &block)
if respond_to?(name) if respond_to?(name)
raise ArgumentError, "metrics method #{name} already exists" raise ArgumentError, "method #{name} already exists"
end end
define_singleton_method(name) do define_singleton_method(name) do
# avoid unnecessary method call to speed up metric access @_metrics_provider_cache&.[](name) || init_metric(type, name, opts, &block)
metric = @_metrics_provider_cache&.[](name)
return metric if metric
fetch_metric(type, name, opts, &block)
end end
end end
def fetch_metric(type, name, opts = {}, &block) def fetch_metric(type, name, opts = {}, &block)
# avoid synchronization to speed up metrics access @_metrics_provider_cache&.[](name) || init_metric(type, name, opts, &block)
metric = @_metrics_provider_cache&.[](name) end
return metric if metric
def init_metric(type, name, opts = {}, &block)
options = MetricOptions.new(opts) options = MetricOptions.new(opts)
options.evaluate(&block) options.evaluate(&block)
...@@ -73,7 +69,7 @@ module Gitlab ...@@ -73,7 +69,7 @@ module Gitlab
fetch_metric(:counter, name, opts, &block) fetch_metric(:counter, name, opts, &block)
end end
# DFetch and/or initialize gauge metric # Fetch and/or initialize gauge metric
# @param [Symbol] name # @param [Symbol] name
# @param [Hash] opts # @param [Hash] opts
def fetch_gauge(name, opts = {}, &block) def fetch_gauge(name, opts = {}, &block)
......
...@@ -8,13 +8,17 @@ describe Gitlab::Metrics::Concern do ...@@ -8,13 +8,17 @@ describe Gitlab::Metrics::Concern do
let(:metric_name) { :sample_metric } let(:metric_name) { :sample_metric }
describe "#define_#{metric_type}" do describe "#define_#{metric_type}" do
let(:define_method) { "define_#{metric_type}".to_sym } let(:_metric_type) { metric_type }
def define_metric_method(**args)
subject.send("define_#{_metric_type}", metric_name, **args)
end
context 'metrics access method not defined' do context 'metrics access method not defined' do
it "defines metrics accessing method" do it "defines metrics accessing method" do
expect(subject).not_to respond_to(metric_name) expect(subject).not_to respond_to(metric_name)
subject.send(define_method, metric_name, docstring: docstring) define_metric_method(docstring: docstring)
expect(subject).to respond_to(metric_name) expect(subject).to respond_to(metric_name)
end end
...@@ -22,16 +26,16 @@ describe Gitlab::Metrics::Concern do ...@@ -22,16 +26,16 @@ describe Gitlab::Metrics::Concern do
context 'metrics access method defined' do context 'metrics access method defined' do
before do before do
subject.send(define_method, metric_name, docstring: docstring) define_metric_method(docstring: docstring)
end end
it 'raises error when trying to redefine method' do it 'raises error when trying to redefine method' do
expect { subject.send(define_method, metric_name, docstring: docstring) }.to raise_error(ArgumentError) expect { define_metric_method(docstring: docstring) }.to raise_error(ArgumentError)
end end
context 'metric is not cached' do context 'metric is not cached' do
it 'calls fetch_metric' do it 'calls fetch_metric' do
expect(subject).to receive(:fetch_metric).with(metric_type, metric_name, docstring: docstring) expect(subject).to receive(:init_metric).with(metric_type, metric_name, docstring: docstring)
subject.send(metric_name) subject.send(metric_name)
end end
...@@ -43,7 +47,7 @@ describe Gitlab::Metrics::Concern do ...@@ -43,7 +47,7 @@ describe Gitlab::Metrics::Concern do
end end
it 'returns cached metric' do it 'returns cached metric' do
expect(subject).not_to receive(:fetch_metric) expect(subject).not_to receive(:init_metric)
subject.send(metric_name) subject.send(metric_name)
end end
...@@ -52,16 +56,18 @@ describe Gitlab::Metrics::Concern do ...@@ -52,16 +56,18 @@ describe Gitlab::Metrics::Concern do
end end
describe "#fetch_#{metric_type}" do describe "#fetch_#{metric_type}" do
let(:fetch_method) { "fetch_#{metric_type}".to_sym }
let(:_metric_type) { metric_type } let(:_metric_type) { metric_type }
let(:null_metric) { Gitlab::Metrics::NullMetric.instance } let(:null_metric) { Gitlab::Metrics::NullMetric.instance }
def fetch_metric_method(**args)
subject.send("fetch_#{_metric_type}", metric_name, **args)
end
context "when #{metric_type} is not cached" do context "when #{metric_type} is not cached" do
it 'initializes counter metric' do it 'initializes counter metric' do
allow(Gitlab::Metrics).to receive(metric_type).and_return(null_metric) allow(Gitlab::Metrics).to receive(metric_type).and_return(null_metric)
subject.send(fetch_method, metric_name, docstring: docstring) fetch_metric_method(docstring: docstring)
expect(Gitlab::Metrics).to have_received(metric_type).with(metric_name, docstring, *args) expect(Gitlab::Metrics).to have_received(metric_type).with(metric_name, docstring, *args)
end end
...@@ -69,13 +75,13 @@ describe Gitlab::Metrics::Concern do ...@@ -69,13 +75,13 @@ describe Gitlab::Metrics::Concern do
context "when #{metric_type} is cached" do context "when #{metric_type} is cached" do
before do before do
subject.send(fetch_method, metric_name, docstring: docstring) fetch_metric_method(docstring: docstring)
end end
it 'uses class metric cache' do it 'uses class metric cache' do
expect(Gitlab::Metrics).not_to receive(metric_type) expect(Gitlab::Metrics).not_to receive(metric_type)
subject.send(fetch_method, metric_name, docstring: docstring) fetch_metric_method(docstring: docstring)
end end
context 'when metric is reloaded' do context 'when metric is reloaded' do
...@@ -86,7 +92,7 @@ describe Gitlab::Metrics::Concern do ...@@ -86,7 +92,7 @@ describe Gitlab::Metrics::Concern do
it "initializes #{metric_type} metric" do it "initializes #{metric_type} metric" do
allow(Gitlab::Metrics).to receive(metric_type).and_return(null_metric) allow(Gitlab::Metrics).to receive(metric_type).and_return(null_metric)
subject.send(fetch_method, metric_name, docstring: docstring) fetch_metric_method(docstring: docstring)
expect(Gitlab::Metrics).to have_received(metric_type).with(metric_name, docstring, *args) expect(Gitlab::Metrics).to have_received(metric_type).with(metric_name, docstring, *args)
end end
...@@ -95,7 +101,7 @@ describe Gitlab::Metrics::Concern do ...@@ -95,7 +101,7 @@ describe Gitlab::Metrics::Concern do
context 'when metric is configured with feature' do context 'when metric is configured with feature' do
let(:feature_name) { :some_metric_feature } let(:feature_name) { :some_metric_feature }
let(:metric) { subject.send(fetch_method, metric_name, docstring: docstring, with_feature: feature_name) } let(:metric) { fetch_metric_method(docstring: docstring, with_feature: feature_name) }
context 'when feature is enabled' do context 'when feature is enabled' do
before do before do
...@@ -117,7 +123,7 @@ describe Gitlab::Metrics::Concern do ...@@ -117,7 +123,7 @@ describe Gitlab::Metrics::Concern do
end end
it "returns NullMetric" do it "returns NullMetric" do
allow(Gitlab::Metrics).to receive(metric_type).and_return(null_metric) allow(Gitlab::Metrics).to receive(metric_type)
expect(metric).to be_instance_of(Gitlab::Metrics::NullMetric) expect(metric).to be_instance_of(Gitlab::Metrics::NullMetric)
......
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