Skip to content

Commit 905f904

Browse files
author
Guy Baron
committed
added tests for protobuf serlializer
1 parent a3069de commit 905f904

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

tests/protoMessages.pb.go

Lines changed: 86 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/protoMessages.proto

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax = "proto3";
2+
package tests;
3+
4+
message ProtoCommand {
5+
int64 some_number = 1;
6+
string some_data = 2;
7+
}

tests/protoMessagesBase.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package tests
2+
3+
func (*ProtoCommand) SchemaName() string {
4+
return "ProtoCommand"
5+
}

tests/protoSerialization_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package tests
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
log "github.com/sirupsen/logrus"
8+
"github.com/wework/grabbit/gbus/serialization"
9+
)
10+
11+
func TestProtoSerialization(t *testing.T) {
12+
13+
logger := log.WithField("test", "proto_serialization")
14+
serializer := serialization.NewProtoSerializer(logger)
15+
cmd := &ProtoCommand{}
16+
schemaName := cmd.SchemaName()
17+
cmd.SomeNumber = 15
18+
cmd.SomeData = "rhinof"
19+
encodedBytes, encErr := serializer.Encode(cmd)
20+
21+
if encErr != nil {
22+
t.Errorf("encoding returned an error: %v", encErr)
23+
}
24+
25+
//Calling Decode with out first registering the schema should fail and return an error
26+
_, decErr := serializer.Decode(encodedBytes, schemaName)
27+
if decErr == nil {
28+
t.Error("decoding expected to fail but did not return an error")
29+
}
30+
31+
serializer.Register(cmd)
32+
decodedMsg, noErr := serializer.Decode(encodedBytes, schemaName)
33+
if noErr != nil {
34+
t.Errorf("decoding of message failed with error:%v", noErr)
35+
}
36+
37+
cmdCopy, ok := decodedMsg.(*ProtoCommand)
38+
39+
if !ok {
40+
t.Errorf("decoded message was of wrong type. expected:%v actual:%v", reflect.TypeOf(cmd), reflect.TypeOf(decodedMsg))
41+
}
42+
43+
if cmdCopy.SomeNumber != cmd.SomeNumber || cmdCopy.SomeData != cmd.SomeData {
44+
log.Infof("expected:%v\n", cmd)
45+
log.Infof("actual:%v\n", cmdCopy)
46+
t.Errorf("decoded message has unexpected or missing data")
47+
}
48+
49+
}

0 commit comments

Comments
 (0)