This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
generated from ContainerSSH/library-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter_test.go
85 lines (71 loc) · 2.56 KB
/
counter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package metrics_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/containerssh/metrics"
)
// TestCounter tests the functionality of counters
func TestCounter(t *testing.T) {
collector := metrics.New(&geoIpLookupProvider{})
counter, err := collector.CreateCounter("test", "seconds", "Hello world!")
assert.Nil(t, err, "creating counter returned an error")
m := collector.ListMetrics()
assert.Equal(t, 1, len(m))
assert.Equal(t, "test", m[0].Name)
assert.Equal(t, "seconds", m[0].Unit)
assert.Equal(t, "Hello world!", m[0].Help)
assert.Equal(t, metrics.MetricTypeCounter, m[0].Type)
assert.Equal(t, 0, len(collector.GetMetric("test")))
counter.Increment()
metric := collector.GetMetric("test")
assert.Equal(t, 1, len(metric))
assert.Equal(t, "test", metric[0].Name)
assert.Equal(t, float64(1), metric[0].Value)
assert.Equal(t, 0, len(metric[0].Labels))
counter.Increment()
metric = collector.GetMetric("test")
assert.Equal(t, 1, len(metric))
assert.Equal(t, "test", metric[0].Name)
assert.Equal(t, float64(2), metric[0].Value)
assert.Equal(t, 0, len(metric[0].Labels))
err = counter.IncrementBy(2)
assert.Nil(t, err, "incrementing a counter returned an error")
metric = collector.GetMetric("test")
assert.Equal(t, 1, len(metric))
assert.Equal(t, "test", metric[0].Name)
assert.Equal(t, float64(4), metric[0].Value)
assert.Equal(t, 0, len(metric[0].Labels))
err = counter.IncrementBy(-1)
assert.EqualError(
t,
err,
metrics.CounterCannotBeIncrementedByNegative.Error(),
"incrementing a counter by negative number did not return an error",
)
counter.Increment(metrics.Label("foo", "bar"))
metric = collector.GetMetric("test")
for _, m := range metric {
if m.CombinedName() == "test{foo=\"bar\"}" {
assert.Equal(t, float64(1), m.Value)
} else {
assert.Equal(t, float64(4), m.Value)
}
}
}
// TestCounter tests the functionality of counters
func TestCounterLabel(t *testing.T) {
collector := metrics.New(&geoIpLookupProvider{})
counter, err := collector.CreateCounter("test", "seconds", "Hello world!")
assert.Nil(t, err, "creating counter returned an error")
counter.Increment()
newCounter := counter.WithLabels(metrics.Label("foo", "bar"))
newCounter.Increment(metrics.Label("baz", "bar"))
metric := collector.GetMetric("test")
assert.Equal(t, 2, len(metric))
assert.Equal(t, "test", metric[0].Name)
assert.Equal(t, float64(1), metric[0].Value)
assert.Equal(t, 0, len(metric[0].Labels))
assert.Equal(t, "test", metric[1].Name)
assert.Equal(t, float64(1), metric[1].Value)
assert.Equal(t, 2, len(metric[1].Labels))
}