6
6
"testing"
7
7
8
8
"github.com/prometheus/client_golang/prometheus"
9
- dto "github.com/prometheus/client_model/go"
10
9
"github.com/stretchr/testify/assert"
11
10
"github.com/stretchr/testify/require"
12
11
@@ -43,8 +42,8 @@ func TestPGPubsub_Metrics(t *testing.T) {
43
42
44
43
metrics , err := registry .Gather ()
45
44
require .NoError (t , err )
46
- require .True (t , gaugeHasValue (t , metrics , 0 , "coder_pubsub_current_events" ))
47
- require .True (t , gaugeHasValue (t , metrics , 0 , "coder_pubsub_current_subscribers" ))
45
+ require .True (t , testutil . PromGaugeHasValue (t , metrics , 0 , "coder_pubsub_current_events" ))
46
+ require .True (t , testutil . PromGaugeHasValue (t , metrics , 0 , "coder_pubsub_current_subscribers" ))
48
47
49
48
event := "test"
50
49
data := "testing"
@@ -63,14 +62,14 @@ func TestPGPubsub_Metrics(t *testing.T) {
63
62
require .Eventually (t , func () bool {
64
63
metrics , err = registry .Gather ()
65
64
assert .NoError (t , err )
66
- return gaugeHasValue (t , metrics , 1 , "coder_pubsub_current_events" ) &&
67
- gaugeHasValue (t , metrics , 1 , "coder_pubsub_current_subscribers" ) &&
68
- gaugeHasValue (t , metrics , 1 , "coder_pubsub_connected" ) &&
69
- counterHasValue (t , metrics , 1 , "coder_pubsub_publishes_total" , "true" ) &&
70
- counterHasValue (t , metrics , 1 , "coder_pubsub_subscribes_total" , "true" ) &&
71
- counterHasValue (t , metrics , 1 , "coder_pubsub_messages_total" , "normal" ) &&
72
- counterHasValue (t , metrics , 7 , "coder_pubsub_received_bytes_total" ) &&
73
- counterHasValue (t , metrics , 7 , "coder_pubsub_published_bytes_total" )
65
+ return testutil . PromGaugeHasValue (t , metrics , 1 , "coder_pubsub_current_events" ) &&
66
+ testutil . PromGaugeHasValue (t , metrics , 1 , "coder_pubsub_current_subscribers" ) &&
67
+ testutil . PromGaugeHasValue (t , metrics , 1 , "coder_pubsub_connected" ) &&
68
+ testutil . PromCounterHasValue (t , metrics , 1 , "coder_pubsub_publishes_total" , "true" ) &&
69
+ testutil . PromCounterHasValue (t , metrics , 1 , "coder_pubsub_subscribes_total" , "true" ) &&
70
+ testutil . PromCounterHasValue (t , metrics , 1 , "coder_pubsub_messages_total" , "normal" ) &&
71
+ testutil . PromCounterHasValue (t , metrics , 7 , "coder_pubsub_received_bytes_total" ) &&
72
+ testutil . PromCounterHasValue (t , metrics , 7 , "coder_pubsub_published_bytes_total" )
74
73
}, testutil .WaitShort , testutil .IntervalFast )
75
74
76
75
colossalData := make ([]byte , 7600 )
@@ -93,54 +92,14 @@ func TestPGPubsub_Metrics(t *testing.T) {
93
92
require .Eventually (t , func () bool {
94
93
metrics , err = registry .Gather ()
95
94
assert .NoError (t , err )
96
- return gaugeHasValue (t , metrics , 1 , "coder_pubsub_current_events" ) &&
97
- gaugeHasValue (t , metrics , 2 , "coder_pubsub_current_subscribers" ) &&
98
- gaugeHasValue (t , metrics , 1 , "coder_pubsub_connected" ) &&
99
- counterHasValue (t , metrics , 2 , "coder_pubsub_publishes_total" , "true" ) &&
100
- counterHasValue (t , metrics , 2 , "coder_pubsub_subscribes_total" , "true" ) &&
101
- counterHasValue (t , metrics , 1 , "coder_pubsub_messages_total" , "normal" ) &&
102
- counterHasValue (t , metrics , 1 , "coder_pubsub_messages_total" , "colossal" ) &&
103
- counterHasValue (t , metrics , 7607 , "coder_pubsub_received_bytes_total" ) &&
104
- counterHasValue (t , metrics , 7607 , "coder_pubsub_published_bytes_total" )
95
+ return testutil . PromGaugeHasValue (t , metrics , 1 , "coder_pubsub_current_events" ) &&
96
+ testutil . PromGaugeHasValue (t , metrics , 2 , "coder_pubsub_current_subscribers" ) &&
97
+ testutil . PromGaugeHasValue (t , metrics , 1 , "coder_pubsub_connected" ) &&
98
+ testutil . PromCounterHasValue (t , metrics , 2 , "coder_pubsub_publishes_total" , "true" ) &&
99
+ testutil . PromCounterHasValue (t , metrics , 2 , "coder_pubsub_subscribes_total" , "true" ) &&
100
+ testutil . PromCounterHasValue (t , metrics , 1 , "coder_pubsub_messages_total" , "normal" ) &&
101
+ testutil . PromCounterHasValue (t , metrics , 1 , "coder_pubsub_messages_total" , "colossal" ) &&
102
+ testutil . PromCounterHasValue (t , metrics , 7607 , "coder_pubsub_received_bytes_total" ) &&
103
+ testutil . PromCounterHasValue (t , metrics , 7607 , "coder_pubsub_published_bytes_total" )
105
104
}, testutil .WaitShort , testutil .IntervalFast )
106
105
}
107
-
108
- func gaugeHasValue (t testing.TB , metrics []* dto.MetricFamily , value float64 , name string , label ... string ) bool {
109
- t .Helper ()
110
- for _ , family := range metrics {
111
- if family .GetName () != name {
112
- continue
113
- }
114
- ms := family .GetMetric ()
115
- for _ , m := range ms {
116
- require .Equal (t , len (label ), len (m .GetLabel ()))
117
- for i , lv := range label {
118
- if lv != m .GetLabel ()[i ].GetValue () {
119
- continue
120
- }
121
- }
122
- return value == m .GetGauge ().GetValue ()
123
- }
124
- }
125
- return false
126
- }
127
-
128
- func counterHasValue (t testing.TB , metrics []* dto.MetricFamily , value float64 , name string , label ... string ) bool {
129
- t .Helper ()
130
- for _ , family := range metrics {
131
- if family .GetName () != name {
132
- continue
133
- }
134
- ms := family .GetMetric ()
135
- for _ , m := range ms {
136
- require .Equal (t , len (label ), len (m .GetLabel ()))
137
- for i , lv := range label {
138
- if lv != m .GetLabel ()[i ].GetValue () {
139
- continue
140
- }
141
- }
142
- return value == m .GetCounter ().GetValue ()
143
- }
144
- }
145
- return false
146
- }
0 commit comments