@@ -103,3 +103,84 @@ func TestPgReplicationSlotCollectorInActive(t *testing.T) {
103
103
}
104
104
105
105
}
106
+
107
+ func TestPgReplicationSlotCollectorActiveNil (t * testing.T ) {
108
+ db , mock , err := sqlmock .New ()
109
+ if err != nil {
110
+ t .Fatalf ("Error opening a stub db connection: %s" , err )
111
+ }
112
+ defer db .Close ()
113
+
114
+ inst := & instance {db : db }
115
+
116
+ columns := []string {"slot_name" , "current_wal_lsn" , "confirmed_flush_lsn" , "active" }
117
+ rows := sqlmock .NewRows (columns ).
118
+ AddRow ("test_slot" , 6 , 12 , nil )
119
+ mock .ExpectQuery (sanitizeQuery (pgReplicationSlotQuery )).WillReturnRows (rows )
120
+
121
+ ch := make (chan prometheus.Metric )
122
+ go func () {
123
+ defer close (ch )
124
+ c := PGReplicationSlotCollector {}
125
+
126
+ if err := c .Update (context .Background (), inst , ch ); err != nil {
127
+ t .Errorf ("Error calling PGReplicationSlotCollector.Update: %s" , err )
128
+ }
129
+ }()
130
+
131
+ expected := []MetricResult {
132
+ {labels : labelMap {"slot_name" : "test_slot" }, value : 6 , metricType : dto .MetricType_GAUGE },
133
+ {labels : labelMap {"slot_name" : "test_slot" }, value : 0 , metricType : dto .MetricType_GAUGE },
134
+ }
135
+
136
+ convey .Convey ("Metrics comparison" , t , func () {
137
+ for _ , expect := range expected {
138
+ m := readMetric (<- ch )
139
+ convey .So (expect , convey .ShouldResemble , m )
140
+ }
141
+ })
142
+ if err := mock .ExpectationsWereMet (); err != nil {
143
+ t .Errorf ("there were unfulfilled exceptions: %s" , err )
144
+ }
145
+ }
146
+
147
+ func TestPgReplicationSlotCollectorTestNilValues (t * testing.T ) {
148
+ db , mock , err := sqlmock .New ()
149
+ if err != nil {
150
+ t .Fatalf ("Error opening a stub db connection: %s" , err )
151
+ }
152
+ defer db .Close ()
153
+
154
+ inst := & instance {db : db }
155
+
156
+ columns := []string {"slot_name" , "current_wal_lsn" , "confirmed_flush_lsn" , "active" }
157
+ rows := sqlmock .NewRows (columns ).
158
+ AddRow (nil , nil , nil , true )
159
+ mock .ExpectQuery (sanitizeQuery (pgReplicationSlotQuery )).WillReturnRows (rows )
160
+
161
+ ch := make (chan prometheus.Metric )
162
+ go func () {
163
+ defer close (ch )
164
+ c := PGReplicationSlotCollector {}
165
+
166
+ if err := c .Update (context .Background (), inst , ch ); err != nil {
167
+ t .Errorf ("Error calling PGReplicationSlotCollector.Update: %s" , err )
168
+ }
169
+ }()
170
+
171
+ expected := []MetricResult {
172
+ {labels : labelMap {"slot_name" : "unknown" }, value : 0 , metricType : dto .MetricType_GAUGE },
173
+ {labels : labelMap {"slot_name" : "unknown" }, value : 0 , metricType : dto .MetricType_GAUGE },
174
+ {labels : labelMap {"slot_name" : "unknown" }, value : 1 , metricType : dto .MetricType_GAUGE },
175
+ }
176
+
177
+ convey .Convey ("Metrics comparison" , t , func () {
178
+ for _ , expect := range expected {
179
+ m := readMetric (<- ch )
180
+ convey .So (expect , convey .ShouldResemble , m )
181
+ }
182
+ })
183
+ if err := mock .ExpectationsWereMet (); err != nil {
184
+ t .Errorf ("there were unfulfilled exceptions: %s" , err )
185
+ }
186
+ }
0 commit comments