Browse Source

scrape tests: check NaN values directly

Normally, a NaN value is never equal to any other value. Compare sample
values via `Float64bits` so that NaN values which are exactly the same
will compare equal.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
pull/13452/head
Bryan Boreham 10 months ago
parent
commit
d0dee51aac
  1. 6
      scrape/helpers_test.go
  2. 27
      scrape/scrape_test.go

6
scrape/helpers_test.go

@ -18,6 +18,7 @@ import (
"context"
"encoding/binary"
"fmt"
"math"
"math/rand"
"strings"
"sync"
@ -71,6 +72,11 @@ type floatSample struct {
f float64
}
func equalFloatSamples(a, b floatSample) bool {
// Compare Float64bits so NaN values which are exactly the same will compare equal.
return labels.Equal(a.metric, b.metric) && a.t == b.t && math.Float64bits(a.f) == math.Float64bits(b.f)
}
type histogramSample struct {
t int64
h *histogram.Histogram

27
scrape/scrape_test.go

@ -1342,7 +1342,7 @@ func TestScrapeLoopAppend(t *testing.T) {
scrapeLabels: `metric NaN`,
discoveryLabels: nil,
expLset: labels.FromStrings("__name__", "metric"),
expValue: float64(value.NormalNaN),
expValue: math.Float64frombits(value.NormalNaN),
},
}
@ -1376,13 +1376,6 @@ func TestScrapeLoopAppend(t *testing.T) {
},
}
// When the expected value is NaN
// DeepEqual will report NaNs as being different,
// so replace it with the expected one.
if test.expValue == float64(value.NormalNaN) {
app.resultFloats[0].f = expected[0].f
}
t.Logf("Test:%s", test.title)
requireEqual(t, expected, app.resultFloats)
}
@ -1390,7 +1383,7 @@ func TestScrapeLoopAppend(t *testing.T) {
func requireEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) {
testutil.RequireEqualWithOptions(t, expected, actual,
[]cmp.Option{cmp.AllowUnexported(floatSample{}), cmp.AllowUnexported(histogramSample{})},
[]cmp.Option{cmp.Comparer(equalFloatSamples), cmp.AllowUnexported(histogramSample{})},
msgAndArgs...)
}
@ -1691,7 +1684,6 @@ func TestScrapeLoop_ChangingMetricString(t *testing.T) {
require.NoError(t, err)
require.NoError(t, slApp.Commit())
// DeepEqual will report NaNs as being different, so replace with a different value.
want := []floatSample{
{
metric: labels.FromStrings("__name__", "metric_a", "a", "1", "b", "1"),
@ -1723,11 +1715,6 @@ func TestScrapeLoopAppendStaleness(t *testing.T) {
require.NoError(t, err)
require.NoError(t, slApp.Commit())
ingestedNaN := math.Float64bits(app.resultFloats[1].f)
require.Equal(t, value.StaleNaN, ingestedNaN, "Appended stale sample wasn't as expected")
// DeepEqual will report NaNs as being different, so replace with a different value.
app.resultFloats[1].f = 42
want := []floatSample{
{
metric: labels.FromStrings(model.MetricNameLabel, "metric_a"),
@ -1737,10 +1724,10 @@ func TestScrapeLoopAppendStaleness(t *testing.T) {
{
metric: labels.FromStrings(model.MetricNameLabel, "metric_a"),
t: timestamp.FromTime(now.Add(time.Second)),
f: 42,
f: math.Float64frombits(value.StaleNaN),
},
}
require.Equal(t, want, app.resultFloats, "Appended samples not as expected:\n%s", appender)
requireEqual(t, want, app.resultFloats, "Appended samples not as expected:\n%s", appender)
}
func TestScrapeLoopAppendNoStalenessIfTimestamp(t *testing.T) {
@ -1783,8 +1770,6 @@ func TestScrapeLoopAppendStalenessIfTrackTimestampStaleness(t *testing.T) {
require.NoError(t, err)
require.NoError(t, slApp.Commit())
// DeepEqual will report NaNs as being different, so replace with a different value.
app.resultFloats[1].f = 42
want := []floatSample{
{
metric: labels.FromStrings(model.MetricNameLabel, "metric_a"),
@ -1794,10 +1779,10 @@ func TestScrapeLoopAppendStalenessIfTrackTimestampStaleness(t *testing.T) {
{
metric: labels.FromStrings(model.MetricNameLabel, "metric_a"),
t: timestamp.FromTime(now.Add(time.Second)),
f: 42,
f: math.Float64frombits(value.StaleNaN),
},
}
require.Equal(t, want, app.resultFloats, "Appended samples not as expected:\n%s", appender)
requireEqual(t, want, app.resultFloats, "Appended samples not as expected:\n%s", appender)
}
func TestScrapeLoopAppendExemplar(t *testing.T) {

Loading…
Cancel
Save