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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
c9dcab30
Commit
c9dcab30
authored
Mar 08, 2021
by
Quang-Minh Nguyen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor background transaction tests
Issue
https://gitlab.com/gitlab-org/gitlab/-/issues/323163
parent
e99ee95c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
81 additions
and
2 deletions
+81
-2
spec/lib/gitlab/metrics/background_transaction_spec.rb
spec/lib/gitlab/metrics/background_transaction_spec.rb
+67
-0
spec/lib/gitlab/sidekiq_middleware/server_metrics_spec.rb
spec/lib/gitlab/sidekiq_middleware/server_metrics_spec.rb
+8
-0
spec/support/shared_examples/metrics/active_record_subscriber_shared_examples.rb
...mples/metrics/active_record_subscriber_shared_examples.rb
+6
-2
No files found.
spec/lib/gitlab/metrics/background_transaction_spec.rb
0 → 100644
View file @
c9dcab30
# frozen_string_literal: true
require
'spec_helper'
RSpec
.
describe
Gitlab
::
Metrics
::
BackgroundTransaction
do
let
(
:transaction
)
{
described_class
.
new
}
let
(
:prometheus_metric
)
{
instance_double
(
Prometheus
::
Client
::
Metric
,
base_labels:
{})
}
before
do
allow
(
described_class
).
to
receive
(
:prometheus_metric
).
and_return
(
prometheus_metric
)
end
describe
'#run'
do
it
'yields the supplied block'
do
expect
{
|
b
|
transaction
.
run
(
&
b
)
}.
to
yield_control
end
it
'stores the transaction in the current thread'
do
transaction
.
run
do
expect
(
Thread
.
current
[
described_class
::
BACKGROUND_THREAD_KEY
]).
to
eq
(
transaction
)
end
end
it
'removes the transaction from the current thread upon completion'
do
transaction
.
run
{
}
expect
(
Thread
.
current
[
described_class
::
BACKGROUND_THREAD_KEY
]).
to
be_nil
end
end
describe
'#labels'
do
it
'provides labels with endpoint_id and feature_category'
do
Labkit
::
Context
.
with_context
(
feature_category:
'projects'
,
caller_id:
'TestWorker'
)
do
expect
(
transaction
.
labels
).
to
eq
({
endpoint_id:
'TestWorker'
,
feature_category:
'projects'
})
end
end
end
RSpec
.
shared_examples
'metric with labels'
do
|
metric_method
|
it
'measures with correct labels and value'
do
value
=
1
expect
(
prometheus_metric
).
to
receive
(
metric_method
).
with
({
endpoint_id:
'TestWorker'
,
feature_category:
'projects'
},
value
)
Labkit
::
Context
.
with_context
(
feature_category:
'projects'
,
caller_id:
'TestWorker'
)
do
transaction
.
send
(
metric_method
,
:test_metric
,
value
)
end
end
end
describe
'#increment'
do
let
(
:prometheus_metric
)
{
instance_double
(
Prometheus
::
Client
::
Counter
,
:increment
,
base_labels:
{})
}
it_behaves_like
'metric with labels'
,
:increment
end
describe
'#set'
do
let
(
:prometheus_metric
)
{
instance_double
(
Prometheus
::
Client
::
Gauge
,
:set
,
base_labels:
{})
}
it_behaves_like
'metric with labels'
,
:set
end
describe
'#observe'
do
let
(
:prometheus_metric
)
{
instance_double
(
Prometheus
::
Client
::
Histogram
,
:observe
,
base_labels:
{})
}
it_behaves_like
'metric with labels'
,
:observe
end
end
spec/lib/gitlab/sidekiq_middleware/server_metrics_spec.rb
View file @
c9dcab30
...
...
@@ -113,6 +113,14 @@ RSpec.describe Gitlab::SidekiqMiddleware::ServerMetrics do
expect
{
|
b
|
subject
.
call
(
worker
,
job
,
:test
,
&
b
)
}.
to
yield_control
.
once
end
it
'calls BackgroundTransaction'
do
expect_next_instance_of
(
Gitlab
::
Metrics
::
BackgroundTransaction
)
do
|
instance
|
expect
(
instance
).
to
receive
(
:run
)
end
subject
.
call
(
worker
,
job
,
:test
)
{}
end
it
'sets queue specific metrics'
do
expect
(
running_jobs_metric
).
to
receive
(
:increment
).
with
(
labels
,
-
1
)
expect
(
running_jobs_metric
).
to
receive
(
:increment
).
with
(
labels
,
1
)
...
...
spec/support/shared_examples/metrics/active_record_subscriber_shared_examples.rb
View file @
c9dcab30
...
...
@@ -99,26 +99,30 @@ RSpec.shared_examples 'record ActiveRecord metrics' do |db_role|
end
context
'when web transaction is available'
do
let
(
:transaction
)
{
double
(
'Gitlab::Metrics::WebTransaction'
,
increment:
nil
,
observe:
nil
)
}
let
(
:transaction
)
{
double
(
'Gitlab::Metrics::WebTransaction'
)
}
before
do
allow
(
::
Gitlab
::
Metrics
::
WebTransaction
).
to
receive
(
:current
)
.
and_return
(
transaction
)
allow
(
::
Gitlab
::
Metrics
::
BackgroundTransaction
).
to
receive
(
:current
)
.
and_return
(
nil
)
allow
(
transaction
).
to
receive
(
:increment
)
allow
(
transaction
).
to
receive
(
:observe
)
end
it_behaves_like
'record ActiveRecord metrics in a metrics transaction'
,
db_role
end
context
'when background transaction is available'
do
let
(
:transaction
)
{
double
(
'Gitlab::Metrics::BackgroundTransaction'
,
increment:
nil
,
observe:
nil
)
}
let
(
:transaction
)
{
double
(
'Gitlab::Metrics::BackgroundTransaction'
)
}
before
do
allow
(
::
Gitlab
::
Metrics
::
WebTransaction
).
to
receive
(
:current
)
.
and_return
(
nil
)
allow
(
::
Gitlab
::
Metrics
::
BackgroundTransaction
).
to
receive
(
:current
)
.
and_return
(
transaction
)
allow
(
transaction
).
to
receive
(
:increment
)
allow
(
transaction
).
to
receive
(
:observe
)
end
it_behaves_like
'record ActiveRecord metrics in a metrics transaction'
,
db_role
...
...
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