MCPcopy
hub / github.com/prometheus/client_golang / TestCounterAdd

Function TestCounterAdd

prometheus/counter_test.go:27–79  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

25)
26
27func TestCounterAdd(t *testing.T) {
28 now := time.Now()
29
30 counter := NewCounter(CounterOpts{
31 Name: "test",
32 Help: "test help",
33 ConstLabels: Labels{"a": "1", "b": "2"},
34 now: func() time.Time { return now },
35 }).(*counter)
36 counter.Inc()
37 if expected, got := 0.0, math.Float64frombits(counter.valBits); expected != got {
38 t.Errorf("Expected %f, got %f.", expected, got)
39 }
40 if expected, got := uint64(1), counter.valInt; expected != got {
41 t.Errorf("Expected %d, got %d.", expected, got)
42 }
43 counter.Add(42)
44 if expected, got := 0.0, math.Float64frombits(counter.valBits); expected != got {
45 t.Errorf("Expected %f, got %f.", expected, got)
46 }
47 if expected, got := uint64(43), counter.valInt; expected != got {
48 t.Errorf("Expected %d, got %d.", expected, got)
49 }
50
51 counter.Add(24.42)
52 if expected, got := 24.42, math.Float64frombits(counter.valBits); expected != got {
53 t.Errorf("Expected %f, got %f.", expected, got)
54 }
55 if expected, got := uint64(43), counter.valInt; expected != got {
56 t.Errorf("Expected %d, got %d.", expected, got)
57 }
58
59 if expected, got := "counter cannot decrease in value", decreaseCounter(counter).Error(); expected != got {
60 t.Errorf("Expected error %q, got %q.", expected, got)
61 }
62
63 m := &dto.Metric{}
64 counter.Write(m)
65
66 expected := &dto.Metric{
67 Label: []*dto.LabelPair{
68 {Name: proto.String("a"), Value: proto.String("1")},
69 {Name: proto.String("b"), Value: proto.String("2")},
70 },
71 Counter: &dto.Counter{
72 Value: proto.Float64(67.42),
73 CreatedTimestamp: timestamppb.New(now),
74 },
75 }
76 if !proto.Equal(expected, m) {
77 t.Errorf("expected %q, got %q", expected, m)
78 }
79}
80
81func decreaseCounter(c *counter) (err error) {
82 defer func() {

Callers

nothing calls this directly

Calls 7

decreaseCounterFunction · 0.85
NewCounterFunction · 0.70
IncMethod · 0.65
AddMethod · 0.65
WriteMethod · 0.65
ErrorMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected