Skip to content

Commit 9e5407b

Browse files
author
Guy Baron
committed
added tests for protobuf serializer error scenarios
1 parent 905f904 commit 9e5407b

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

tests/protoSerialization_test.go

+48-9
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,46 @@ import (
88
"github.com/wework/grabbit/gbus/serialization"
99
)
1010

11-
func TestProtoSerialization(t *testing.T) {
11+
func getProtoCommand() *ProtoCommand {
12+
return &ProtoCommand{
13+
SomeNumber: 15,
14+
SomeData: "rhinof"}
15+
}
1216

17+
func TestProtoSerialization(t *testing.T) {
18+
// so the tests logs do not get littered with log entries from serlializer
19+
log.SetLevel(log.PanicLevel)
20+
defer log.SetLevel(log.InfoLevel)
1321
logger := log.WithField("test", "proto_serialization")
22+
1423
serializer := serialization.NewProtoSerializer(logger)
15-
cmd := &ProtoCommand{}
24+
25+
name := serializer.Name()
26+
if name != "proto" {
27+
t.Fatalf("incorrect serializer name. expected:proto actual:%s", name)
28+
}
29+
cmd := getProtoCommand()
1630
schemaName := cmd.SchemaName()
17-
cmd.SomeNumber = 15
18-
cmd.SomeData = "rhinof"
19-
encodedBytes, encErr := serializer.Encode(cmd)
2031

32+
encodedBytes, encErr := serializer.Encode(cmd)
2133
if encErr != nil {
22-
t.Errorf("encoding returned an error: %v", encErr)
34+
t.Fatalf("encoding returned an error: %v", encErr)
2335
}
2436

2537
//Calling Decode with out first registering the schema should fail and return an error
2638
_, decErr := serializer.Decode(encodedBytes, schemaName)
2739
if decErr == nil {
28-
t.Error("decoding expected to fail but did not return an error")
40+
t.Fatalf("decoding expected to fail but did not return an error")
2941
}
3042

3143
serializer.Register(cmd)
44+
3245
decodedMsg, noErr := serializer.Decode(encodedBytes, schemaName)
3346
if noErr != nil {
34-
t.Errorf("decoding of message failed with error:%v", noErr)
47+
t.Fatalf("decoding of message failed with error:%v", noErr)
3548
}
3649

3750
cmdCopy, ok := decodedMsg.(*ProtoCommand)
38-
3951
if !ok {
4052
t.Errorf("decoded message was of wrong type. expected:%v actual:%v", reflect.TypeOf(cmd), reflect.TypeOf(decodedMsg))
4153
}
@@ -47,3 +59,30 @@ func TestProtoSerialization(t *testing.T) {
4759
}
4860

4961
}
62+
63+
func TestProtoSerializationErrors(t *testing.T) {
64+
// so the tests logs do not get littered with log entries from serlializer
65+
log.SetLevel(log.PanicLevel)
66+
defer log.SetLevel(log.InfoLevel)
67+
logger := log.WithField("test", "proto_serialization")
68+
69+
serializer := serialization.NewProtoSerializer(logger)
70+
71+
// test that encoding a non protobuf generated strcut fails and returns an error
72+
_, encErr := serializer.Encode(Command1{})
73+
if encErr == nil {
74+
t.Errorf("serializer expected to return an error for non proto generated messages but di not")
75+
}
76+
77+
cmd := getProtoCommand()
78+
encodedBytes, encErr := serializer.Encode(cmd)
79+
if encErr != nil {
80+
t.Fatalf("encoding returned an error: %v", encErr)
81+
}
82+
83+
//decoding an unregistered schema fails and returns an error
84+
if _, decErr := serializer.Decode(encodedBytes, "kong"); decErr == nil {
85+
t.Errorf("decoding an unregistred schema is expected to return an error but did not")
86+
}
87+
88+
}

0 commit comments

Comments
 (0)