-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spec/metrics: add specs for upstream metrics
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
describe('upstream metrics', function() | ||
describe('report', function() | ||
local upstream_metrics | ||
local test_counter = { inc = function() end } | ||
local test_histogram = { observe = function() end } | ||
|
||
before_each(function() | ||
-- Make Prometheus return stubbed counters and histograms | ||
stub(test_counter, 'inc') | ||
stub(test_histogram, 'observe') | ||
|
||
local Prometheus = require('apicast.prometheus') | ||
getmetatable(Prometheus).__call = function(_, type) | ||
if type == 'counter' then | ||
return test_counter | ||
elseif type == 'histogram' then | ||
return test_histogram | ||
end | ||
end | ||
|
||
package.loaded['apicast.metrics.upstream'] = nil | ||
upstream_metrics = require('apicast.metrics.upstream') | ||
end) | ||
|
||
after_each(function() | ||
package.loaded['apicast.prometheus'] = nil | ||
require('apicast.prometheus') | ||
|
||
package.loaded['apicast.metrics.upstream'] = nil | ||
require('apicast.metrics.upstream') | ||
end) | ||
|
||
it('increases the counter of status codes', function() | ||
upstream_metrics.report(200, 0.1) | ||
assert.stub(test_counter.inc).was_called_with(test_counter, 1, { 200 }) | ||
end) | ||
|
||
it('adds the latency to the histogram', function() | ||
upstream_metrics.report(200, 0.1) | ||
assert.stub(test_histogram.observe).was_called_with(test_histogram, 0.1) | ||
end) | ||
|
||
describe('when the status is nil or empty', function() | ||
it('does not increase the counter of status codes', function() | ||
upstream_metrics.report(nil, 0.1) | ||
upstream_metrics.report('', 0.1) | ||
assert.stub(test_counter.inc).was_not_called() | ||
end) | ||
end) | ||
|
||
describe('when the latency is nil or empty', function() | ||
it('does not add the latency to the histogram', function() | ||
upstream_metrics.report(200, nil) | ||
upstream_metrics.report(200, '') | ||
assert.stub(test_histogram.observe).was_not_called() | ||
end) | ||
end) | ||
end) | ||
end) |