Skip to content

Commit

Permalink
cleanup: fix generic comparisons on protobuf messages (#3153)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dsnet authored and menghanl committed Nov 6, 2019
1 parent dd568c0 commit 2d2f656
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
8 changes: 6 additions & 2 deletions status/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
6 changes: 5 additions & 1 deletion test/end2end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions test/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"fmt"
"io"
"os"
"reflect"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion xds/internal/balancer/lrs/lrs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 2d2f656

Please sign in to comment.