-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
codec_test.go
98 lines (92 loc) · 2.77 KB
/
codec_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package rpc
import (
"reflect"
"strings"
"testing"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/stretchr/testify/require"
"go.etcd.io/etcd/raft/v3/raftpb"
"google.golang.org/grpc/health/grpc_health_v1"
)
func TestCodecMarshalUnmarshal(t *testing.T) {
defer leaktest.AfterTest(t)()
testCodec := codec{}
for _, test := range []struct {
name string
filledMsgBuilder func() interface{}
emptyMsgBuilder func() interface{}
}{
{"rpc.PingRequest",
func() interface{} { return &PingRequest{Ping: "pong"} },
func() interface{} { return &PingRequest{} }},
{"raftpb.Message",
func() interface{} {
return &raftpb.Message{
To: 531,
From: 550,
}
},
func() interface{} { return &raftpb.Message{} }},
{"grpc_health_v1.HealthCheckRequest",
func() interface{} {
return &grpc_health_v1.HealthCheckRequest{
Service: "wombats",
}
},
func() interface{} { return &grpc_health_v1.HealthCheckRequest{} }},
{"roachpb.GetRequest",
func() interface{} {
return &roachpb.GetRequest{
RequestHeader: roachpb.RequestHeader{
Key: roachpb.Key("turtle"),
},
}
},
func() interface{} { return &roachpb.GetRequest{} }},
} {
t.Run(test.name, func(t *testing.T) {
input := test.filledMsgBuilder()
marshaled, err := testCodec.Marshal(input)
require.NoError(t, err, "marshal failed")
output := test.emptyMsgBuilder()
err = testCodec.Unmarshal(marshaled, output)
require.NoError(t, err, "unmarshal failed")
// reflect.DeepEqual/require.Equal can fail
// because of XXX_sizecache fields
//
// google's proto Equal doesn't understand all
// gogoproto generated types and panics.
//
// gogoproto's proto Equal fails because of
// https://github.com/gogo/protobuf/issues/13
//
// Here, we zero any top-level fields that
// start with XXX_ and then use require.Equal
// (which uses require.DeepEqual). I doubt
// this would work for the general case, but
// it works for the protobufs tested here.
zeroXXXFields(input)
zeroXXXFields(output)
require.Equal(t, input, output)
})
}
}
func zeroXXXFields(v interface{}) {
val := reflect.Indirect(reflect.ValueOf(v))
typ := val.Type()
for i := 0; i < val.NumField(); i++ {
if strings.HasPrefix(typ.Field(i).Name, "XXX_") {
val.Field(i).Set(reflect.Zero(val.Field(i).Type()))
}
}
}