Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
e3fe6554
Commit
e3fe6554
authored
Jan 17, 2018
by
Pawel Chojnacki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Tests for Metrics::Concern
parent
d7edc4b4
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
104 additions
and
3 deletions
+104
-3
lib/gitlab/metrics/concern.rb
lib/gitlab/metrics/concern.rb
+3
-3
spec/lib/gitlab/metrics/concern_spec.rb
spec/lib/gitlab/metrics/concern_spec.rb
+101
-0
No files found.
lib/gitlab/metrics/concern.rb
View file @
e3fe6554
...
...
@@ -12,7 +12,7 @@ module Gitlab
private
def
define_metric
(
type
,
name
,
opts
=
{},
&
block
)
if
instance_methods
(
false
).
include
?
(
name
)
if
respond_to
?
(
name
)
raise
ArgumentError
,
"metrics method
#{
name
}
already exists"
end
...
...
@@ -71,7 +71,7 @@ module Gitlab
# @param [Symbol] name
# @param [Hash] opts
def
fetch_gauge
(
name
,
opts
=
{},
&
block
)
fetch_metric
(
:
counter
,
name
,
opts
,
&
block
)
fetch_metric
(
:
gauge
,
name
,
opts
,
&
block
)
end
# Fetch and/or initialize histogram metric
...
...
@@ -99,7 +99,7 @@ module Gitlab
# @param [Symbol] name
# @param [Hash] opts
def
define_gauge
(
name
,
opts
=
{},
&
block
)
define_metric
(
:
counter
,
name
,
opts
,
&
block
)
define_metric
(
:
gauge
,
name
,
opts
,
&
block
)
end
# Define metric accessor method for a Histogram
...
...
spec/lib/gitlab/metrics/concern_spec.rb
0 → 100644
View file @
e3fe6554
require
'spec_helper'
describe
Gitlab
::
Metrics
::
Concern
do
subject
{
Class
.
new
{
include
Gitlab
::
Metrics
::
Concern
}
}
let
(
:null_metric
)
{
Gitlab
::
Metrics
::
NullMetric
.
new
}
shared_context
'metric'
do
|
metric_type
,
*
args
|
let
(
:docstring
)
{
'description'
}
let
(
:metric
)
{
: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
)
subject
.
send
(
define_method
,
metric
,
docstring:
docstring
)
expect
(
subject
).
to
respond_to
(
metric
)
end
end
context
'metrics access method defined'
do
before
do
subject
.
send
(
define_method
,
metric
,
docstring:
docstring
)
end
it
'raises error when trying to redefine method'
do
expect
{
subject
.
send
(
define_method
,
metric
,
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
)
subject
.
send
(
metric
)
end
end
context
'metric is cached'
do
before
do
subject
.
send
(
metric
)
end
it
'returns cached metric'
do
expect
(
subject
).
not_to
receive
(
:fetch_metric
)
subject
.
send
(
metric
)
end
end
end
end
describe
"#fetch_
#{
metric_type
}
"
do
let
(
:fetch_method
)
{
"fetch_
#{
metric_type
}
"
.
to_sym
}
context
"when
#{
metric_type
}
fetched first time"
do
it
'initializes counter metric'
do
allow
(
Gitlab
::
Metrics
).
to
receive
(
metric_type
).
and_return
(
null_metric
)
subject
.
send
(
fetch_method
,
metric
,
docstring:
docstring
)
expect
(
Gitlab
::
Metrics
).
to
have_received
(
metric_type
).
with
(
metric
,
docstring
,
*
args
)
end
end
context
"when
#{
metric_type
}
is fetched second time"
do
before
do
subject
.
send
(
fetch_method
,
metric
,
docstring:
docstring
)
end
it
'uses class metric cache'
do
expect
(
Gitlab
::
Metrics
).
not_to
receive
(
metric_type
)
subject
.
send
(
fetch_method
,
metric
,
docstring:
docstring
)
end
context
'when metric is reloaded'
do
before
do
subject
.
reload_metric!
(
metric
)
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
)
expect
(
Gitlab
::
Metrics
).
to
have_received
(
metric_type
).
with
(
metric
,
docstring
,
*
args
)
end
end
end
end
end
include_examples
'metric'
,
:counter
,
{}
include_examples
'metric'
,
:gauge
,
{},
:all
include_examples
'metric'
,
:histogram
,
{},
[
0.005
,
0.01
,
0.025
,
0.05
,
0.1
,
0.25
,
0.5
,
1
,
2.5
,
5
,
10
]
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment