1
0
mirror of https://github.com/golang/go synced 2024-11-06 11:36:16 -07:00
go/internal/telemetry/export/ocagent/wire/metrics_test.go
Nathan Dias 2d3bb8ce05 internal/telemetry/export/ocagent: correctly JSON marshal BucketOptionsExplicit
This change adds a custom MarshalJSON method to BucketOptionsExplicit for
marshalling parity with protobuf/jsonpb. This allows the OpenCensus service
to correctly decode a distribution's BucketOptions.

Updates golang/go#33819

Change-Id: Ia9dc868e1cbfc32a956f6a276dfd1591f7d4d31a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/208398
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-11-22 06:42:13 +00:00

77 lines
1.2 KiB
Go

package wire
import (
"reflect"
"testing"
)
func TestMarshalJSON(t *testing.T) {
tests := []struct {
name string
pt *Point
want string
}{
{
"PointInt64",
&Point{
Value: PointInt64Value{
Int64Value: 5,
},
},
`{"int64Value":5}`,
},
{
"PointDouble",
&Point{
Value: PointDoubleValue{
DoubleValue: 3.14,
},
},
`{"doubleValue":3.14}`,
},
{
"PointDistribution",
&Point{
Value: PointDistributionValue{
DistributionValue: &DistributionValue{
Count: 3,
Sum: 10,
Buckets: []*Bucket{
{
Count: 1,
},
{
Count: 2,
},
},
BucketOptions: &BucketOptionsExplicit{
Bounds: []float64{
0, 5,
},
},
},
},
},
`{"distributionValue":{"count":3,"sum":10,"bucket_options":{"explicit":{"bounds":[0,5]}},"buckets":[{"count":1},{"count":2}]}}`,
},
{
"nil point",
nil,
`null`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf, err := tt.pt.MarshalJSON()
if err != nil {
t.Fatalf("Got:\n%v\nWant:\n%v", err, nil)
}
got := string(buf)
if !reflect.DeepEqual(got, tt.want) {
t.Fatalf("Got:\n%s\nWant:\n%s", got, tt.want)
}
})
}
}