Debian Codegraph @

prometheus/collector_test.go

raw
1 // Copyright 2018 The Prometheus Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package prometheus
15
16 import "testing"
17
18 type collectorDescribedByCollect struct {
19 cnt Counter
20 gge Gauge
21 }
22
23 func (c collectorDescribedByCollect) Collect(ch chan<- Metric) {
24 ch <- c.cnt
25 ch <- c.gge
26 }
27
28 func (c collectorDescribedByCollect) Describe(ch chan<- *Desc) {
29 DescribeByCollect(c, ch)
30 }
31
32 func TestDescribeByCollect(t *testing.T) {
33 goodCollector := collectorDescribedByCollect{
34 cnt: NewCounter(CounterOpts{Name: "c1", Help: "help c1"}),
35 gge: NewGauge(GaugeOpts{Name: "g1", Help: "help g1"}),
36 }
37 collidingCollector := collectorDescribedByCollect{
38 cnt: NewCounter(CounterOpts{Name: "c2", Help: "help c2"}),
39 gge: NewGauge(GaugeOpts{Name: "g1", Help: "help g1"}),
40 }
41 inconsistentCollector := collectorDescribedByCollect{
42 cnt: NewCounter(CounterOpts{Name: "c3", Help: "help c3"}),
43 gge: NewGauge(GaugeOpts{Name: "c3", Help: "help inconsistent"}),
44 }
45
46 reg := NewPedanticRegistry()
47
48 if err := reg.Register(goodCollector); err != nil {
49 t.Error("registration failed:", err)
50 }
51 if err := reg.Register(collidingCollector); err == nil {
52 t.Error("registration unexpectedly succeeded")
53 }
54 if err := reg.Register(inconsistentCollector); err == nil {
55 t.Error("registration unexpectedly succeeded")
56 }
57
58 if _, err := reg.Gather(); err != nil {
59 t.Error("gathering failed:", err)
60 }
61 }