Skip to content

Commit 6ca58b5

Browse files
committed
[FAB-3461] Gossip protobuff test coverage
This change set improves the test coverage of the generated code in protos/gossip and makes the code coverage of the total package to be >= 85%. Change-Id: Ib5e1eddf4c4feee94d81f750f4d4fbf87e52019f Signed-off-by: Yacov Manevich <[email protected]>
1 parent 1195f2f commit 6ca58b5

File tree

1 file changed

+197
-0
lines changed

1 file changed

+197
-0
lines changed

protos/gossip/message_test.go

+197
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package gossip
18+
19+
import (
20+
"reflect"
21+
"testing"
22+
23+
"github.com/golang/protobuf/proto"
24+
"github.com/stretchr/testify/assert"
25+
)
26+
27+
type protoMsg interface {
28+
Reset()
29+
String() string
30+
ProtoMessage()
31+
Descriptor() ([]byte, []int)
32+
}
33+
34+
func TestMethods(t *testing.T) {
35+
msgs := []protoMsg{
36+
&Envelope{},
37+
&SecretEnvelope{},
38+
&GossipMessage{},
39+
&Secret{},
40+
&StateInfo{},
41+
&ConnEstablish{},
42+
&AliveMessage{},
43+
&MembershipRequest{},
44+
&MembershipResponse{},
45+
&DataMessage{},
46+
&GossipHello{},
47+
&DataDigest{},
48+
&DataRequest{},
49+
&DataUpdate{},
50+
&Empty{},
51+
&StateInfoSnapshot{},
52+
&StateInfoPullRequest{},
53+
&RemoteStateRequest{},
54+
&RemoteStateResponse{},
55+
&LeadershipMessage{},
56+
&PeerIdentity{},
57+
}
58+
59+
for _, msg := range msgs {
60+
msg.Reset()
61+
_, _ = msg.Descriptor()
62+
msg.ProtoMessage()
63+
assert.Empty(t, msg.String())
64+
65+
}
66+
67+
contentTypes := []isGossipMessage_Content{
68+
&GossipMessage_AliveMsg{},
69+
&GossipMessage_MemReq{},
70+
&GossipMessage_MemRes{},
71+
&GossipMessage_DataMsg{},
72+
&GossipMessage_Hello{},
73+
&GossipMessage_DataDig{},
74+
&GossipMessage_DataReq{},
75+
&GossipMessage_DataUpdate{},
76+
&GossipMessage_Empty{},
77+
&GossipMessage_Conn{},
78+
&GossipMessage_StateInfo{},
79+
&GossipMessage_StateSnapshot{},
80+
&GossipMessage_StateInfoPullReq{},
81+
&GossipMessage_StateRequest{},
82+
&GossipMessage_StateResponse{},
83+
&GossipMessage_LeadershipMsg{},
84+
&GossipMessage_PeerIdentity{},
85+
}
86+
87+
for _, ct := range contentTypes {
88+
ct.isGossipMessage_Content()
89+
gMsg := &GossipMessage{
90+
Content: ct,
91+
}
92+
v := reflect.ValueOf(gMsg)
93+
for i := 0; i < v.NumMethod(); i++ {
94+
func() {
95+
defer func() {
96+
recover()
97+
}()
98+
v.Method(i).Call([]reflect.Value{})
99+
}()
100+
}
101+
gMsg = &GossipMessage{
102+
Content: ct,
103+
}
104+
_GossipMessage_OneofSizer(gMsg)
105+
gMsg = &GossipMessage{
106+
Content: ct,
107+
}
108+
_GossipMessage_OneofMarshaler(gMsg, &proto.Buffer{})
109+
gMsg = &GossipMessage{
110+
Content: ct,
111+
}
112+
113+
for i := 5; i < 22; i++ {
114+
_GossipMessage_OneofUnmarshaler(gMsg, i, 2, &proto.Buffer{})
115+
}
116+
}
117+
118+
assert.NotZero(t, _Secret_OneofSizer(&Secret{
119+
Content: &Secret_InternalEndpoint{
120+
InternalEndpoint: "internalEndpoint",
121+
},
122+
}))
123+
124+
assert.Nil(t, (&Envelope{}).GetSecretEnvelope())
125+
}
126+
127+
func TestGrpc(t *testing.T) {
128+
cl := NewGossipClient(nil)
129+
f1 := func() {
130+
cl.GossipStream(nil)
131+
}
132+
assert.Panics(t, f1)
133+
f2 := func() {
134+
cl.Ping(nil, nil)
135+
}
136+
assert.Panics(t, f2)
137+
gscl := &gossipGossipStreamClient{}
138+
f3 := func() {
139+
gscl.Send(nil)
140+
}
141+
assert.Panics(t, f3)
142+
f4 := func() {
143+
gscl.Recv()
144+
}
145+
assert.Panics(t, f4)
146+
f5 := func() {
147+
gscl.Header()
148+
}
149+
assert.Panics(t, f5)
150+
f6 := func() {
151+
gscl.CloseSend()
152+
}
153+
assert.Panics(t, f6)
154+
f7 := func() {
155+
gscl.Context()
156+
}
157+
assert.Panics(t, f7)
158+
gss := &gossipGossipStreamServer{}
159+
f8 := func() {
160+
gss.Recv()
161+
}
162+
assert.Panics(t, f8)
163+
f9 := func() {
164+
gss.Send(nil)
165+
}
166+
assert.Panics(t, f9)
167+
f10 := func() {
168+
gss.Context()
169+
}
170+
assert.Panics(t, f10)
171+
f11 := func() {
172+
gss.RecvMsg(nil)
173+
}
174+
assert.Panics(t, f11)
175+
f12 := func() {
176+
gss.SendHeader(nil)
177+
}
178+
assert.Panics(t, f12)
179+
f13 := func() {
180+
gss.RecvMsg(nil)
181+
}
182+
assert.Panics(t, f13)
183+
f14 := func() {
184+
gss.SendMsg(nil)
185+
}
186+
assert.Panics(t, f14)
187+
f15 := func() {
188+
gss.SetTrailer(nil)
189+
}
190+
assert.Panics(t, f15)
191+
f16 := func() {
192+
_Gossip_Ping_Handler(nil, nil, func(interface{}) error {
193+
return nil
194+
}, nil)
195+
}
196+
assert.Panics(t, f16)
197+
}

0 commit comments

Comments
 (0)