@@ -79,54 +79,69 @@ func testFunction(t *testing.T, testToRun func(*testing.T)) {
7979 isRunningWithTimeout (t , time .Second )
8080 testToRun (t )
8181 Stop ()
82+
83+ if len (metricsInternal .GetMetrics ()) != 0 {
84+ t .Fatal ("didn't clear metrics properly after tests" )
85+ }
86+ if len (metricsHistogramInternal .GetMetrics ()) != 0 {
87+ t .Fatal ("didn't clear histo metrics properly after tests" )
88+ }
8289}
8390
8491func TestSendingSingleMetric (t * testing.T ) {
8592 testFunction (t , func (t * testing.T ) {
86- metric := & Metric {name : "test " }
93+ metric := & Metric {name : "testsinglemetric " }
8794 Send (metric )
8895 hasNumberOfMetrics (t , time .Second , 1 )
8996
9097 aggregatedMetrics := metricsInternal .GetMetrics ()
9198
92- if aggregatedMetrics [0 ].name != "test " {
99+ if aggregatedMetrics [0 ].name != "testsinglemetric " {
93100 t .Errorf ("Got wrong name %s in stored metric" , aggregatedMetrics [0 ].name )
94101 }
95102 })
96103}
97104
98105func TestExportingMetrics (t * testing.T ) {
99106 testFunction (t , func (t * testing.T ) {
100- metric := & Metric {name : "test " }
107+ metric := & Metric {name : "testexporting " }
101108 Send (metric )
102109 hasNumberOfMetrics (t , time .Second , 1 )
103110
104111 metricsRequestedChannel <- struct {}{}
105112 metricsToExport := <- metricsExportedToRequest
106113
107114 if len (metricsToExport ) != 1 {
108- t .Errorf ("Got wrong amount of metrics %d, expected 1" , len (metricsToExport ))
115+ t .Fatalf ("Got wrong amount of metrics %d, expected 1" , len (metricsToExport ))
109116 }
110117
111- if metricsToExport [0 ].name != "test " {
112- t .Errorf ("Got wrong name of metric %s, expected test " , metricsToExport [0 ].name )
118+ if metricsToExport [0 ].name != "testexporting " {
119+ t .Fatalf ("Got wrong name of metric %s, expected testexporting " , metricsToExport [0 ].name )
113120 }
114121 })
115122}
116123
117124func TestHttpRun (t * testing.T ) {
118- go Run (9876 , "/metrics" )
125+ port := uint16 (9876 )
126+ endpoint := "/metrics"
127+ go Run (port , endpoint )
119128 defer Stop ()
120129
121130 isRunningWithTimeout (t , 5 * time .Second )
122131
123- metric := Metric {name : "test" }
124- metric .timestamp = time .Unix (10 , 0 )
125- metric .AddTag ("tag1" , "42" )
126- metric .SetFieldInt64 ("value1" , 11 )
132+ sendAndTestMetric (t , port , endpoint , "testhttprun1" , time .Unix (10 , 0 ), "tag1" , "42" , "value1" , 11 )
133+ sendAndTestMetric (t , port , endpoint , "testhttprun2" , time .Unix (11 , 0 ), "tag2" , "43" , "value2" , 12 )
134+ sendAndTestMetric (t , port , endpoint , "testhttprun3" , time .Unix (12 , 0 ), "tag3" , "44" , "value3" , 13 )
135+ sendAndTestMetric (t , port , endpoint , "testhttprun4" , time .Unix (13 , 0 ), "tag4" , "45" , "value4" , 14 )
136+ }
137+
138+ func sendAndTestMetric (t * testing.T , port uint16 , endpoint string , metricName string , timestamp time.Time , tagName string , tagVal string , fieldName string , fieldVal int64 ) {
139+ metric := Metric {name : metricName , timestamp : timestamp }
140+ metric .AddTag (tagName , tagVal )
141+ metric .SetFieldInt64 (fieldName , fieldVal )
127142 Send (& metric )
128143
129- response , err := http .Get ("http://localhost:9876/metrics" )
144+ response , err := http .Get (fmt . Sprintf ( "http://localhost:%d%s" , port , endpoint ) )
130145 if err != nil {
131146 t .Fatalf ("Failed to GET metrics at port 9876: %v" , err )
132147 }
@@ -142,28 +157,28 @@ func TestHttpRun(t *testing.T) {
142157
143158 receivedMetric := receivedMetrics [0 ]
144159
145- if receivedMetric .Name != "test" {
146- t .Errorf ("Got wrong name of metric %s, expected test " , receivedMetric .Name )
160+ if receivedMetric .Name != metricName {
161+ t .Errorf ("Got wrong name of metric %s, expected %s " , receivedMetric .Name , metricName )
147162 }
148163
149- if receivedMetric .Timestamp != time . Unix ( 10 , 0 ) .UnixNano () {
150- t .Errorf ("Got wrong timestamp of metric %d, expected 10 " , receivedMetric .Timestamp )
164+ if receivedMetric .Timestamp != timestamp .UnixNano () {
165+ t .Errorf ("Got wrong timestamp of metric %d, expected %v " , receivedMetric .Timestamp , timestamp . UnixNano () )
151166 }
152167
153168 if len (receivedMetric .Tags ) != 1 {
154169 t .Errorf ("Got wrong number of tags %d, expected 1" , len (receivedMetric .Tags ))
155170 }
156171
157- if receivedMetric .Tags ["tag1" ] != "42" {
172+ if receivedMetric .Tags [tagName ] != tagVal {
158173 t .Errorf ("Failed to retreive tags: tag1 with value 42, %+v" , receivedMetric .Tags )
159174 }
160175
161176 if len (receivedMetric .Fields ) != 1 {
162177 t .Errorf ("Got wrong number of values %d, expected 1" , len (receivedMetric .Fields ))
163178 }
164179
165- if receivedMetric .Fields ["value1" ] != "11i" {
166- t .Errorf ("Failed to retreive tags: value1 with value 11 : %+v" , receivedMetric .Fields )
180+ if receivedMetric .Fields [fieldName ] != fmt . Sprintf ( "%di" , fieldVal ) {
181+ t .Errorf ("Failed to retreive tags: %s with value %d : %+v" , fieldName , fieldVal , receivedMetric .Fields )
167182 }
168183}
169184
0 commit comments