forked from dunglas/mercure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics_test.go
115 lines (90 loc) · 2.86 KB
/
metrics_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package mercure
import (
"testing"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)
func TestNumberOfRunningSubscribers(t *testing.T) {
m := NewPrometheusMetrics(nil)
tss := &TopicSelectorStore{}
s1 := NewSubscriber("", zap.NewNop(), tss)
s1.Topics = []string{"topic1", "topic2"}
m.SubscriberConnected(s1)
assertGaugeLabelValue(t, 1.0, m.subscribers, "topic1")
assertGaugeLabelValue(t, 1.0, m.subscribers, "topic2")
s2 := NewSubscriber("", zap.NewNop(), tss)
s2.Topics = []string{"topic2"}
m.SubscriberConnected(s2)
assertGaugeLabelValue(t, 1.0, m.subscribers, "topic1")
assertGaugeLabelValue(t, 2.0, m.subscribers, "topic2")
m.SubscriberDisconnected(s1)
assertGaugeLabelValue(t, 0.0, m.subscribers, "topic1")
assertGaugeLabelValue(t, 1.0, m.subscribers, "topic2")
m.SubscriberDisconnected(s2)
assertGaugeLabelValue(t, 0.0, m.subscribers, "topic1")
assertGaugeLabelValue(t, 0.0, m.subscribers, "topic2")
}
func TestTotalNumberOfHandledSubscribers(t *testing.T) {
m := NewPrometheusMetrics(nil)
tss := &TopicSelectorStore{}
s1 := NewSubscriber("", zap.NewNop(), tss)
s1.Topics = []string{"topic1", "topic2"}
m.SubscriberConnected(s1)
assertCounterValue(t, 1.0, m.subscribersTotal, "topic1")
assertCounterValue(t, 1.0, m.subscribersTotal, "topic2")
s2 := NewSubscriber("", zap.NewNop(), tss)
s2.Topics = []string{"topic2"}
m.SubscriberConnected(s2)
assertCounterValue(t, 1.0, m.subscribersTotal, "topic1")
assertCounterValue(t, 2.0, m.subscribersTotal, "topic2")
m.SubscriberDisconnected(s1)
m.SubscriberDisconnected(s2)
assertCounterValue(t, 1.0, m.subscribersTotal, "topic1")
assertCounterValue(t, 2.0, m.subscribersTotal, "topic2")
}
func TestTotalOfHandledUpdates(t *testing.T) {
m := NewPrometheusMetrics(nil)
m.UpdatePublished(&Update{
Topics: []string{"topic1", "topic2"},
})
m.UpdatePublished(&Update{
Topics: []string{"topic2", "topic3"},
})
m.UpdatePublished(&Update{
Topics: []string{"topic2"},
})
m.UpdatePublished(&Update{
Topics: []string{"topic3"},
})
assertCounterValue(t, 1.0, m.updatesTotal, "topic1")
assertCounterValue(t, 3.0, m.updatesTotal, "topic2")
assertCounterValue(t, 2.0, m.updatesTotal, "topic3")
}
func assertGaugeLabelValue(t *testing.T, v float64, g *prometheus.GaugeVec, l string) {
t.Helper()
var metricOut dto.Metric
m, err := g.GetMetricWithLabelValues(l)
if err != nil {
t.Fatal(err)
}
err = m.Write(&metricOut)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, v, *metricOut.Gauge.Value)
}
func assertCounterValue(t *testing.T, v float64, c *prometheus.CounterVec, l string) {
t.Helper()
var metricOut dto.Metric
m, err := c.GetMetricWithLabelValues(l)
if err != nil {
t.Fatal(err)
}
err = m.Write(&metricOut)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, v, *metricOut.Counter.Value)
}