TestCheckMetricConsistency
(t *testing.T)
| 1308 | |
| 1309 | // TestCheckMetricConsistency |
| 1310 | func TestCheckMetricConsistency(t *testing.T) { |
| 1311 | reg := prometheus.NewRegistry() |
| 1312 | timestamp := time.Now() |
| 1313 | |
| 1314 | desc := prometheus.NewDesc("metric_a", "", nil, nil) |
| 1315 | metric := prometheus.MustNewConstMetric(desc, prometheus.CounterValue, 1) |
| 1316 | |
| 1317 | validCollector := &customCollector{ |
| 1318 | collectFunc: func(ch chan<- prometheus.Metric) { |
| 1319 | ch <- prometheus.NewMetricWithTimestamp(timestamp.Add(-1*time.Minute), metric) |
| 1320 | ch <- prometheus.NewMetricWithTimestamp(timestamp, metric) |
| 1321 | }, |
| 1322 | } |
| 1323 | reg.MustRegister(validCollector) |
| 1324 | _, err := reg.Gather() |
| 1325 | if err != nil { |
| 1326 | t.Error("metric validation should succeed:", err) |
| 1327 | } |
| 1328 | reg.Unregister(validCollector) |
| 1329 | |
| 1330 | invalidCollector := &customCollector{ |
| 1331 | collectFunc: func(ch chan<- prometheus.Metric) { |
| 1332 | ch <- prometheus.NewMetricWithTimestamp(timestamp, metric) |
| 1333 | ch <- prometheus.NewMetricWithTimestamp(timestamp, metric) |
| 1334 | }, |
| 1335 | } |
| 1336 | reg.MustRegister(invalidCollector) |
| 1337 | _, err = reg.Gather() |
| 1338 | if err == nil { |
| 1339 | t.Error("metric validation should return an error") |
| 1340 | } |
| 1341 | reg.Unregister(invalidCollector) |
| 1342 | } |
| 1343 | |
| 1344 | func TestGatherDoesNotLeakGoroutines(t *testing.T) { |
| 1345 | // Use goleak to verify that no unexpected goroutines are leaked during the test. |
nothing calls this directly
no test coverage detected