11
22describe (' prometheus' , function ()
3- before_each (function () package.loaded [' apicast.prometheus' ] = nil end )
3+ before_each (function ()
4+ package.loaded [' apicast.prometheus' ] = nil
5+ package.loaded [' prometheus' ] = nil
6+ end )
47
58 describe (' shared dictionary is missing' , function ()
69 before_each (function () ngx .shared .prometheus_metrics = nil end )
710
811 it (' can be called' , function ()
9- assert .is_nil (require (' apicast.prometheus' )())
12+ local prom = require (' apicast.prometheus' )
13+ local metric = prom ()
14+ assert .is_not_nil (metric )
15+ assert .is_function (metric .inc )
1016 end )
1117
1218 it (' can be collected' , function ()
@@ -15,37 +21,47 @@ describe('prometheus', function()
1521 end )
1622
1723 describe (' shared dictionary is there' , function ()
24+ local saved_get_phase
25+
1826 before_each (function ()
1927 ngx .shared .prometheus_metrics = {
20- set = function () end ,
21- get_keys = function () return {} end
28+ set = function () return true end ,
29+ safe_set = function () return true end ,
30+ safe_add = function () return true end ,
31+ incr = function () return 0 end ,
32+ get = function () end ,
33+ delete = function () return true end ,
34+ get_keys = function () return {} end ,
2235 }
36+ saved_get_phase = ngx .get_phase
37+ ngx .get_phase = function () return ' init' end
2338 end )
2439
25- local prometheus
26- local Prometheus
27-
28- before_each (function ()
29- prometheus = assert (require (' apicast.prometheus' ))
30- Prometheus = getmetatable (prometheus ).__index
40+ after_each (function ()
41+ ngx .get_phase = saved_get_phase
3142 end )
3243
33- for _ ,metric_type in pairs { ' counter' , ' gauge' , ' histogram' } do
34- describe (metric_type , function ()
35- it (' can be called' , function ()
36- stub (Prometheus , metric_type )
37-
38- prometheus (metric_type , ' some_metric' )
44+ it (' returns a callable wrapper' , function ()
45+ local prometheus = assert (require (' apicast.prometheus' ))
46+ local metric = prometheus (' counter' , ' test_metric' , ' A test counter' )
47+ assert .is_not_nil (metric )
48+ end )
3949
40- assert .stub (Prometheus [metric_type ]).was .called_with (Prometheus , ' some_metric' )
41- end )
42- end )
43- end
50+ it (' caches metrics by name' , function ()
51+ local prometheus = assert (require (' apicast.prometheus' ))
52+ local m1 = prometheus (' counter' , ' cached_metric' , ' A counter' )
53+ local m2 = prometheus (' counter' , ' cached_metric' , ' A counter' )
54+ assert .are .equal (m1 , m2 )
55+ end )
4456
57+ it (' exposes collect' , function ()
58+ local prometheus = assert (require (' apicast.prometheus' ))
59+ assert .is_function (prometheus .collect )
60+ end )
4561
46- it (' can be collected ' , function ()
47- ngx . header = { }
48- assert .is_nil ( require ( ' apicast. prometheus' ): collect () )
62+ it (' exposes init_worker ' , function ()
63+ local prometheus = assert ( require ( ' apicast.prometheus ' ))
64+ assert .is_function ( prometheus . init_worker )
4965 end )
5066 end )
5167
0 commit comments