forked from dunglas/mercure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics_test.go
90 lines (69 loc) · 2.1 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
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)
s1 := NewSubscriber("", zap.NewNop())
s1.SetTopics([]string{"topic1", "topic2"}, nil)
m.SubscriberConnected(s1)
assertGaugeValue(t, 1.0, m.subscribers)
s2 := NewSubscriber("", zap.NewNop())
s2.SetTopics([]string{"topic2"}, nil)
m.SubscriberConnected(s2)
assertGaugeValue(t, 2.0, m.subscribers)
m.SubscriberDisconnected(s1)
assertGaugeValue(t, 1.0, m.subscribers)
m.SubscriberDisconnected(s2)
assertGaugeValue(t, 0.0, m.subscribers)
}
func TestTotalNumberOfHandledSubscribers(t *testing.T) {
m := NewPrometheusMetrics(nil)
s1 := NewSubscriber("", zap.NewNop())
s1.SetTopics([]string{"topic1", "topic2"}, nil)
m.SubscriberConnected(s1)
assertCounterValue(t, 1.0, m.subscribersTotal)
s2 := NewSubscriber("", zap.NewNop())
s2.SetTopics([]string{"topic2"}, nil)
m.SubscriberConnected(s2)
assertCounterValue(t, 2.0, m.subscribersTotal)
m.SubscriberDisconnected(s1)
m.SubscriberDisconnected(s2)
assertCounterValue(t, 2.0, m.subscribersTotal)
}
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, 4.0, m.updatesTotal)
}
func assertGaugeValue(t *testing.T, v float64, g prometheus.Gauge) {
t.Helper()
var metricOut dto.Metric
if err := g.Write(&metricOut); err != nil {
t.Fatal(err)
}
assert.Equal(t, v, metricOut.GetGauge().GetValue()) //nolint:testifylint
}
func assertCounterValue(t *testing.T, v float64, c prometheus.Counter) {
t.Helper()
var metricOut dto.Metric
if err := c.Write(&metricOut); err != nil {
t.Fatal(err)
}
assert.Equal(t, v, metricOut.GetCounter().GetValue()) // nolint:testifylint
}