cleanup: fix generic comparisons on protobuf messages (#3153)

Generated protobuf messages contain internal data structures
that general purpose comparison functions (e.g., reflect.DeepEqual,
pretty.Compare, etc) do not properly compare. It is already the case
today that these functions may report a difference when two messages
are actually semantically equivalent.

Fix all usages by either calling proto.Equal directly if
the top-level types are themselves proto.Message, or by calling
cmp.Equal with the cmp.Comparer(proto.Equal) option specified.
This option teaches cmp to use proto.Equal anytime it encounters
proto.Message types.
This commit is contained in:
Joe Tsai 2019-11-06 14:25:07 -08:00 committed by Menghan Li
parent dd568c0669
commit 2d2f65684c
4 changed files with 13 additions and 6 deletions

View File

@ -22,13 +22,13 @@ import (
"context"
"errors"
"fmt"
"reflect"
"testing"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
apb "github.com/golang/protobuf/ptypes/any"
dpb "github.com/golang/protobuf/ptypes/duration"
"github.com/google/go-cmp/cmp"
cpb "google.golang.org/genproto/googleapis/rpc/code"
epb "google.golang.org/genproto/googleapis/rpc/errdetails"
spb "google.golang.org/genproto/googleapis/rpc/status"
@ -318,12 +318,16 @@ func TestStatus_ErrorDetails_Fail(t *testing.T) {
}
for _, tc := range tests {
got := tc.s.Details()
if !reflect.DeepEqual(got, tc.i) {
if !cmp.Equal(got, tc.i, cmp.Comparer(proto.Equal), cmp.Comparer(equalError)) {
t.Errorf("(%v).Details() = %+v, want %+v", str(tc.s), got, tc.i)
}
}
}
func equalError(x, y error) bool {
return x == y || (x != nil && y != nil && x.Error() == y.Error())
}
func str(s *Status) string {
if s == nil {
return "nil"

View File

@ -3919,11 +3919,15 @@ func testFailedServerStreaming(t *testing.T, e env) {
t.Fatalf("%v.StreamingOutputCall(_) = _, %v, want <nil>", tc, err)
}
wantErr := status.Error(codes.DataLoss, "error for testing: "+failAppUA)
if _, err := stream.Recv(); !reflect.DeepEqual(err, wantErr) {
if _, err := stream.Recv(); !equalError(err, wantErr) {
t.Fatalf("%v.Recv() = _, %v, want _, %v", stream, err, wantErr)
}
}
func equalError(x, y error) bool {
return x == y || (x != nil && y != nil && x.Error() == y.Error())
}
// concurrentSendServer is a TestServiceServer whose
// StreamingOutputCall makes ten serial Send calls, sending payloads
// "0".."9", inclusive. TestServerStreamingConcurrent verifies they

View File

@ -23,7 +23,6 @@ import (
"fmt"
"io"
"os"
"reflect"
"strconv"
"strings"
"testing"
@ -362,7 +361,7 @@ func (s) TestRetryStreaming(t *testing.T) {
res, err := stream.Recv()
if res != nil ||
((err == nil) != (want == nil)) ||
(want != nil && !reflect.DeepEqual(err, want)) {
(want != nil && err.Error() != want.Error()) {
return fmt.Errorf("client: Recv() = %v, %v; want <nil>, %v", res, err, want)
}
return nil

View File

@ -90,7 +90,7 @@ func equalClusterStats(a, b []*endpointpb.ClusterStats) bool {
s.LoadReportInterval = nil
}
}
return reflect.DeepEqual(a, b)
return cmp.Equal(a, b, cmp.Comparer(proto.Equal))
}
func Test_lrsStore_buildStats_drops(t *testing.T) {