Skip to content

Commit

Permalink
Do reflect.TypeOf() in helper function instead
Browse files Browse the repository at this point in the history
  • Loading branch information
zenador committed Apr 26, 2023
1 parent d6119e5 commit d0a8045
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 29 deletions.
26 changes: 5 additions & 21 deletions pkg/mimirpb/compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package mimirpb
import (
stdlibjson "encoding/json"
"math"
"reflect"
"strconv"
"testing"
"unsafe"
Expand Down Expand Up @@ -226,10 +225,7 @@ func TestFromFPointsToSamples(t *testing.T) {
// Check that Prometheus FPoint and Mimir Sample types converted
// into each other with unsafe.Pointer are compatible
func TestPrometheusFPointInSyncWithMimirPbSample(t *testing.T) {
protoType := reflect.TypeOf(Sample{})
prometheusType := reflect.TypeOf(promql.FPoint{})

test.RequireSameShape(t, prometheusType, protoType, true)
test.RequireSameShape(t, promql.FPoint{}, Sample{}, true)
}

func BenchmarkFromFPointsToSamples(b *testing.B) {
Expand Down Expand Up @@ -257,10 +253,7 @@ func TestFromHPointsToHistograms(t *testing.T) {
// Check that Prometheus HPoint and Mimir FloatHistogramPair types converted
// into each other with unsafe.Pointer are compatible
func TestPrometheusHPointInSyncWithMimirPbFloatHistogramPair(t *testing.T) {
protoType := reflect.TypeOf(FloatHistogramPair{})
prometheusType := reflect.TypeOf(promql.HPoint{})

test.RequireSameShape(t, prometheusType, protoType, true)
test.RequireSameShape(t, promql.HPoint{}, FloatHistogramPair{}, true)
}

func BenchmarkFromHPointsToHistograms(b *testing.B) {
Expand Down Expand Up @@ -637,26 +630,17 @@ func TestFromFloatHistogramToPromHistogram(t *testing.T) {
// Check that Prometheus and Mimir SampleHistogram types converted
// into each other with unsafe.Pointer are compatible
func TestPrometheusSampleHistogramInSyncWithMimirPbSampleHistogram(t *testing.T) {
protoType := reflect.TypeOf(SampleHistogram{})
prometheusType := reflect.TypeOf(model.SampleHistogram{})

test.RequireSameShape(t, prometheusType, protoType, false)
test.RequireSameShape(t, model.SampleHistogram{}, SampleHistogram{}, false)
}

// Check that Prometheus Label and MimirPb LabelAdapter types converted
// into each other with unsafe.Pointer are compatible
func TestPrometheusLabelsInSyncWithMimirPbLabelAdapter(t *testing.T) {
protoType := reflect.TypeOf(LabelAdapter{})
prometheusType := reflect.TypeOf(labels.Label{})

test.RequireSameShape(t, prometheusType, protoType, false)
test.RequireSameShape(t, labels.Label{}, LabelAdapter{}, false)
}

// Check that Prometheus histogram.Span and MimirPb BucketSpan types converted
// into each other with unsafe.Pointer are compatible
func TestPrometheusHistogramSpanInSyncWithMimirPbBucketSpan(t *testing.T) {
protoType := reflect.TypeOf(BucketSpan{})
prometheusType := reflect.TypeOf(histogram.Span{})

test.RequireSameShape(t, prometheusType, protoType, false)
test.RequireSameShape(t, histogram.Span{}, BucketSpan{}, false)
}
6 changes: 1 addition & 5 deletions pkg/mimirpb/query_response_extra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package mimirpb
import (
"os"
"path/filepath"
"reflect"
"testing"

"github.com/grafana/regexp"
Expand Down Expand Up @@ -78,8 +77,5 @@ func extractPrometheusStrings(t *testing.T, constantType string) []string {
// FloatHistogram types converted into each other with unsafe.Pointer
// are compatible
func TestFloatHistogramProtobufTypeRemainsInSyncWithPrometheus(t *testing.T) {
protoType := reflect.TypeOf(FloatHistogram{})
prometheusType := reflect.TypeOf(histogram.FloatHistogram{})

test.RequireSameShape(t, prometheusType, protoType, false)
test.RequireSameShape(t, histogram.FloatHistogram{}, FloatHistogram{}, false)
}
6 changes: 3 additions & 3 deletions pkg/util/test/shape.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const ignoredFieldName = "<name ignored>"
// but we also check the names are the same here to ensure there's no confusion
// (eg. two bool fields swapped) when ignoreName is false. However, when you
// know the names are different, you can set ignoreName to true.
func RequireSameShape(t *testing.T, expectedType reflect.Type, actualType reflect.Type, ignoreName bool) {
expectedFormatted := prettyPrintType(expectedType, ignoreName)
actualFormatted := prettyPrintType(actualType, ignoreName)
func RequireSameShape(t *testing.T, expectedType, actualType any, ignoreName bool) {
expectedFormatted := prettyPrintType(reflect.TypeOf(expectedType), ignoreName)
actualFormatted := prettyPrintType(reflect.TypeOf(actualType), ignoreName)

require.Equal(t, expectedFormatted, actualFormatted)
}
Expand Down

0 comments on commit d0a8045

Please sign in to comment.