Skip to content

Commit 99056e4

Browse files
committed
Fix the NaN tests
Signed-off-by: Felix Yuan <[email protected]>
1 parent fdd5e21 commit 99056e4

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

Diff for: collector/pg_archiver_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ package collector
1414

1515
import (
1616
"context"
17+
"math"
1718
"testing"
1819

1920
"github.com/DATA-DOG/go-sqlmock"
@@ -56,3 +57,40 @@ func TestPgArchiverCollector(t *testing.T) {
5657
t.Errorf("there were unfulfilled exceptions: %s", err)
5758
}
5859
}
60+
61+
func TestPgArchiverNaNCollector(t *testing.T) {
62+
db, mock, err := sqlmock.New()
63+
if err != nil {
64+
t.Fatalf("Error opening a stub db connection: %s", err)
65+
}
66+
defer db.Close()
67+
68+
inst := &instance{db: db}
69+
mock.ExpectQuery(sanitizeQuery(pgArchiverQuery)).WillReturnRows(sqlmock.NewRows([]string{"pending_wal_count"}).
70+
AddRow(math.NaN()))
71+
72+
ch := make(chan prometheus.Metric)
73+
go func() {
74+
defer close(ch)
75+
c := PGArchiverCollector{}
76+
77+
if err := c.Update(context.Background(), inst, ch); err != nil {
78+
t.Errorf("Error calling PGArchiverCollector.Update: %s", err)
79+
}
80+
}()
81+
82+
expected := []MetricResult{
83+
{labels: labelMap{}, value: math.NaN(), metricType: dto.MetricType_GAUGE},
84+
}
85+
convey.Convey("Metrics comparison", t, func() {
86+
for _, expect := range expected {
87+
m := readMetric(<-ch)
88+
convey.So(expect.labels, convey.ShouldResemble, m.labels)
89+
convey.So(math.IsNaN(m.value), convey.ShouldResemble, math.IsNaN(expect.value))
90+
convey.So(expect.metricType, convey.ShouldEqual, m.metricType)
91+
}
92+
})
93+
if err := mock.ExpectationsWereMet(); err != nil {
94+
t.Errorf("there were unfulfilled exceptions: %s", err)
95+
}
96+
}

Diff for: collector/pg_xid_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ func TestPgXidCollector(t *testing.T) {
6565
}
6666
}
6767

68-
// This test is turned off for now
69-
// Because convey cannot compare NaN
70-
func xTestPgNanCollector(t *testing.T) {
68+
func TestPgNanCollector(t *testing.T) {
7169
db, mock, err := sqlmock.New()
7270
if err != nil {
7371
t.Fatalf("Error opening a stub db connection: %s", err)
@@ -101,7 +99,10 @@ func xTestPgNanCollector(t *testing.T) {
10199
convey.Convey("Metrics comparison", t, func() {
102100
for _, expect := range expected {
103101
m := readMetric(<-ch)
104-
convey.So(expect, convey.ShouldResemble, m)
102+
103+
convey.So(expect.labels, convey.ShouldResemble, m.labels)
104+
convey.So(math.IsNaN(m.value), convey.ShouldResemble, math.IsNaN(expect.value))
105+
convey.So(expect.metricType, convey.ShouldEqual, m.metricType)
105106
}
106107
})
107108
if err := mock.ExpectationsWereMet(); err != nil {

0 commit comments

Comments
 (0)