22
33import sys
44import threading
5+ import time
56
67if sys .version_info < (2 , 7 ):
78 # We need the skip decorators from unittest2 on Python 2.6.
@@ -29,6 +30,13 @@ class TestGenerateText(unittest.TestCase):
2930 def setUp (self ):
3031 self .registry = CollectorRegistry ()
3132
33+ # Mock time so _created values are fixed.
34+ self .old_time = time .time
35+ time .time = lambda : 123.456
36+
37+ def tearDown (self ):
38+ time .time = self .old_time
39+
3240 def custom_collector (self , metric_family ):
3341 class CustomCollector (object ):
3442 def collect (self ):
@@ -38,12 +46,23 @@ def collect(self):
3846 def test_counter (self ):
3947 c = Counter ('cc' , 'A counter' , registry = self .registry )
4048 c .inc ()
41- self .assertEqual (b'# HELP cc_total A counter\n # TYPE cc_total counter\n cc_total 1.0\n ' , generate_latest (self .registry ))
49+ self .assertEqual (b'''# HELP cc_total A counter
50+ # TYPE cc_total counter
51+ cc_total 1.0
52+ # TYPE cc_created gauge
53+ cc_created 123.456
54+ ''' , generate_latest (self .registry ))
4255
4356 def test_counter_total (self ):
4457 c = Counter ('cc_total' , 'A counter' , registry = self .registry )
4558 c .inc ()
46- self .assertEqual (b'# HELP cc_total A counter\n # TYPE cc_total counter\n cc_total 1.0\n ' , generate_latest (self .registry ))
59+ self .assertEqual (b'''# HELP cc_total A counter
60+ # TYPE cc_total counter
61+ cc_total 1.0
62+ # TYPE cc_created gauge
63+ cc_created 123.456
64+ ''' , generate_latest (self .registry ))
65+
4766 def test_gauge (self ):
4867 g = Gauge ('gg' , 'A gauge' , registry = self .registry )
4968 g .set (17 )
@@ -52,7 +71,13 @@ def test_gauge(self):
5271 def test_summary (self ):
5372 s = Summary ('ss' , 'A summary' , ['a' , 'b' ], registry = self .registry )
5473 s .labels ('c' , 'd' ).observe (17 )
55- self .assertEqual (b'# HELP ss A summary\n # TYPE ss summary\n ss_count{a="c",b="d"} 1.0\n ss_sum{a="c",b="d"} 17.0\n ' , generate_latest (self .registry ))
74+ self .assertEqual (b'''# HELP ss A summary
75+ # TYPE ss summary
76+ ss_count{a="c",b="d"} 1.0
77+ ss_sum{a="c",b="d"} 17.0
78+ # TYPE ss_created gauge
79+ ss_created{a="c",b="d"} 123.456
80+ ''' , generate_latest (self .registry ))
5681
5782 @unittest .skipIf (sys .version_info < (2 , 7 ), "Test requires Python 2.7+." )
5883 def test_histogram (self ):
@@ -77,11 +102,21 @@ def test_histogram(self):
77102hh_bucket{le="+Inf"} 1.0
78103hh_count 1.0
79104hh_sum 0.05
105+ # TYPE hh_created gauge
106+ hh_created 123.456
80107''' , generate_latest (self .registry ))
81108
82109 def test_gaugehistogram (self ):
83- self .custom_collector (GaugeHistogramMetricFamily ('gh' , 'help' , buckets = [('1.0' , 4 ), ('+Inf' , (5 ))]))
84- self .assertEqual (b'''# HELP gh help\n # TYPE gh histogram\n gh_bucket{le="1.0"} 4.0\n gh_bucket{le="+Inf"} 5.0\n ''' , generate_latest (self .registry ))
110+ self .custom_collector (GaugeHistogramMetricFamily ('gh' , 'help' , buckets = [('1.0' , 4 ), ('+Inf' , 5 )], gsum_value = 7 ))
111+ self .assertEqual (b'''# HELP gh help
112+ # TYPE gh histogram
113+ gh_bucket{le="1.0"} 4.0
114+ gh_bucket{le="+Inf"} 5.0
115+ # TYPE gh_gcount gauge
116+ gh_gcount 5.0
117+ # TYPE gh_gsum gauge
118+ gh_gsum 7.0
119+ ''' , generate_latest (self .registry ))
85120
86121 def test_info (self ):
87122 i = Info ('ii' , 'A info' , ['a' , 'b' ], registry = self .registry )
@@ -94,14 +129,14 @@ def test_enum(self):
94129 self .assertEqual (b'# HELP ee An enum\n # TYPE ee gauge\n ee{a="c",b="d",ee="foo"} 0.0\n ee{a="c",b="d",ee="bar"} 1.0\n ' , generate_latest (self .registry ))
95130
96131 def test_unicode (self ):
97- c = Counter ('cc' , '\u4500 ' , ['l' ], registry = self .registry )
132+ c = Gauge ('cc' , '\u4500 ' , ['l' ], registry = self .registry )
98133 c .labels ('\u4500 ' ).inc ()
99- self .assertEqual (b'# HELP cc_total \xe4 \x94 \x80 \n # TYPE cc_total counter \n cc_total {l="\xe4 \x94 \x80 "} 1.0\n ' , generate_latest (self .registry ))
134+ self .assertEqual (b'# HELP cc \xe4 \x94 \x80 \n # TYPE cc gauge \n cc {l="\xe4 \x94 \x80 "} 1.0\n ' , generate_latest (self .registry ))
100135
101136 def test_escaping (self ):
102- c = Counter ('cc' , 'A\n count \\ er ' , ['a' ], registry = self .registry )
103- c .labels ('\\ x\n "' ).inc (1 )
104- self .assertEqual (b'# HELP cc_total A\\ ncount \\ \\ er \n # TYPE cc_total counter \n cc_total {a="\\ \\ x\\ n\\ ""} 1.0\n ' , generate_latest (self .registry ))
137+ g = Gauge ('cc' , 'A\n gaug \\ e ' , ['a' ], registry = self .registry )
138+ g .labels ('\\ x\n "' ).inc (1 )
139+ self .assertEqual (b'# HELP cc A\\ ngaug \\ \\ e \n # TYPE cc gauge \n cc {a="\\ \\ x\\ n\\ ""} 1.0\n ' , generate_latest (self .registry ))
105140
106141 def test_nonnumber (self ):
107142
0 commit comments