Skip to content
This repository was archived by the owner on Dec 3, 2024. It is now read-only.

Commit eeede3e

Browse files
committed
🧹 cleanup rpc test with a custom message comparison helper
1 parent da1b5e1 commit eeede3e

File tree

1 file changed

+86
-5
lines changed

1 file changed

+86
-5
lines changed

stackdriver_test.go

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ import (
2525
monitoring "cloud.google.com/go/monitoring/apiv3"
2626
metrics "github.com/armon/go-metrics"
2727
emptypb "github.com/golang/protobuf/ptypes/empty"
28+
"github.com/google/go-cmp/cmp"
2829
"google.golang.org/api/option"
30+
distributionpb "google.golang.org/genproto/googleapis/api/distribution"
31+
"google.golang.org/genproto/googleapis/api/metric"
32+
metricpb "google.golang.org/genproto/googleapis/api/metric"
2933
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
3034
"google.golang.org/grpc"
3135
"google.golang.org/grpc/test/bufconn"
@@ -119,11 +123,33 @@ func TestSample(t *testing.T) {
119123
},
120124
createFn: func(t *testing.T) func(context.Context, *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
121125
return func(_ context.Context, req *monitoringpb.CreateTimeSeriesRequest) (*emptypb.Empty, error) {
122-
if req.TimeSeries[0].Points[0].Value.GetDistributionValue().BucketCounts[0] == 1 {
123-
return &emptypb.Empty{}, nil
126+
want := &monitoringpb.CreateTimeSeriesRequest{
127+
Name: "projects/foo",
128+
TimeSeries: []*monitoringpb.TimeSeries{
129+
&monitoringpb.TimeSeries{
130+
Metric: &metricpb.Metric{
131+
Type: "custom.googleapis.com/go-metrics/foo_bar",
132+
},
133+
MetricKind: metric.MetricDescriptor_CUMULATIVE,
134+
Points: []*monitoringpb.Point{
135+
&monitoringpb.Point{
136+
Value: &monitoringpb.TypedValue{
137+
Value: &monitoringpb.TypedValue_DistributionValue{
138+
DistributionValue: &distributionpb.Distribution{
139+
BucketCounts: []int64{1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
140+
Count: 3,
141+
},
142+
},
143+
},
144+
},
145+
},
146+
},
147+
},
124148
}
125-
t.Errorf("unexpected CreateTimeSeriesRequest\nwant: %s\ngot: %v", "bucket 0 count 1", req)
126-
return nil, errors.New("unexpected CreateTimeSeriesRequest")
149+
if diff := diffCreateMsg(want, req); diff != "" {
150+
t.Errorf("unexpected CreateTimeSeriesRequest (-want +got):\n%s", diff)
151+
}
152+
return &emptypb.Empty{}, nil
127153
}
128154
},
129155
},
@@ -363,11 +389,66 @@ func (s *mockMetricServer) CreateTimeSeries(ctx context.Context, req *monitoring
363389
// Skips defaults that are not appropriate for tests.
364390
func newTestSink(interval time.Duration, client *monitoring.MetricClient) *Sink {
365391
s := &Sink{}
366-
s.taskInfo = &taskInfo{}
392+
s.taskInfo = &taskInfo{
393+
ProjectID: "foo",
394+
}
367395
s.interval = interval
368396
s.bucketer = DefaultBucketer
369397
s.extractor = DefaultLabelExtractor
370398
s.reset()
371399
go s.flushMetrics(context.Background())
372400
return s
373401
}
402+
403+
func diffCreateMsg(want, got *monitoringpb.CreateTimeSeriesRequest) string {
404+
out := ""
405+
if want.GetName() != "" && (want.GetName() != got.GetName()) {
406+
out += fmt.Sprintf("Unexpected Name, got: %s, want:%s\n", got.GetName(), want.GetName())
407+
}
408+
409+
for i := range want.GetTimeSeries() {
410+
w := want.GetTimeSeries()[i]
411+
g := got.GetTimeSeries()[i]
412+
413+
if w.GetMetricKind() != g.GetMetricKind() {
414+
out += fmt.Sprintf("Unexpected MetricKind, got: %s, want:%s\n", g.GetMetricKind(), w.GetMetricKind())
415+
}
416+
417+
if w.GetMetric().GetType() != g.GetMetric().GetType() {
418+
out += fmt.Sprintf("Unexpected Metric Type, got: %s, want:%s\n", g.GetMetric().GetType(), w.GetMetric().GetType())
419+
}
420+
421+
if len(w.GetMetric().GetLabels()) != 0 {
422+
d := cmp.Diff(g.GetMetric().GetLabels(), w.GetMetric().GetLabels())
423+
if d != "" {
424+
out += fmt.Sprintf("Unexpected metric labels diff:%s \n", d)
425+
}
426+
}
427+
428+
for j := range w.GetPoints() {
429+
wp := w.GetPoints()[j]
430+
gp := g.GetPoints()[j]
431+
432+
// TODO: support diffing the start/end times
433+
434+
// gauge/count
435+
if wp.GetValue().GetDoubleValue() != gp.GetValue().GetDoubleValue() {
436+
out += fmt.Sprintf("Unexpected value (@point %d), got: %v, want:%v\n", j, gp.GetValue().GetDoubleValue(), wp.GetValue().GetDoubleValue())
437+
}
438+
439+
// distribution
440+
if wd := wp.GetValue().GetDistributionValue(); wd != nil {
441+
gd := gp.GetValue().GetDistributionValue()
442+
// TODO: support diffing custom buckets
443+
d := cmp.Diff(gd.GetBucketCounts(), wd.GetBucketCounts())
444+
if d != "" {
445+
out += fmt.Sprintf("Unexpected bucket counts diff (@point %d):%s \n", j, d)
446+
}
447+
if gd.GetCount() != wd.GetCount() {
448+
out += fmt.Sprintf("Unexpected count (@point %d), got: %v, want: %v\n", j, gd.GetCount(), wd.GetCount())
449+
}
450+
}
451+
}
452+
}
453+
return out
454+
}

0 commit comments

Comments
 (0)