@@ -263,3 +263,43 @@ func TestBytesBetweenLSN(t *testing.T) {
263263 })
264264 }
265265}
266+
267+ func TestPGStatArchiverLagCollectorReplica (t * testing.T ) {
268+ db , mock , err := sqlmock .New ()
269+ if err != nil {
270+ t .Fatalf ("Error opening a stub db connection: %s" , err )
271+ }
272+ defer db .Close ()
273+
274+ inst := & Instance {db : db }
275+
276+ columns := []string {"last_archived_wal" , "current_lsn" }
277+ // Simulate replica where current_lsn is NULL (which is what the query returns when in recovery)
278+ rows := sqlmock .NewRows (columns ).
279+ AddRow ("000000010000000000000001" , nil )
280+ mock .ExpectQuery (sanitizeQuery (statArchiverLagQuery )).WillReturnRows (rows )
281+
282+ ch := make (chan prometheus.Metric )
283+ go func () {
284+ defer close (ch )
285+ c := PGStatArchiverLagCollector {}
286+
287+ if err := c .Update (context .Background (), inst , ch ); err != nil {
288+ t .Errorf ("Error calling PGStatArchiverLagCollector.Update: %s" , err )
289+ }
290+ }()
291+
292+ expected := []MetricResult {
293+ {labels : labelMap {}, value : 0 , metricType : dto .MetricType_GAUGE },
294+ }
295+
296+ convey .Convey ("Metrics comparison" , t , func () {
297+ for _ , expect := range expected {
298+ m := readMetric (<- ch )
299+ convey .So (expect , convey .ShouldResemble , m )
300+ }
301+ })
302+ if err := mock .ExpectationsWereMet (); err != nil {
303+ t .Errorf ("there were unfulfilled exceptions: %s" , err )
304+ }
305+ }
0 commit comments