@@ -8,34 +8,46 @@ import (
8
8
"github.com/wework/grabbit/gbus/serialization"
9
9
)
10
10
11
- func TestProtoSerialization (t * testing.T ) {
11
+ func getProtoCommand () * ProtoCommand {
12
+ return & ProtoCommand {
13
+ SomeNumber : 15 ,
14
+ SomeData : "rhinof" }
15
+ }
12
16
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 )
13
21
logger := log .WithField ("test" , "proto_serialization" )
22
+
14
23
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 ()
16
30
schemaName := cmd .SchemaName ()
17
- cmd .SomeNumber = 15
18
- cmd .SomeData = "rhinof"
19
- encodedBytes , encErr := serializer .Encode (cmd )
20
31
32
+ encodedBytes , encErr := serializer .Encode (cmd )
21
33
if encErr != nil {
22
- t .Errorf ("encoding returned an error: %v" , encErr )
34
+ t .Fatalf ("encoding returned an error: %v" , encErr )
23
35
}
24
36
25
37
//Calling Decode with out first registering the schema should fail and return an error
26
38
_ , decErr := serializer .Decode (encodedBytes , schemaName )
27
39
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" )
29
41
}
30
42
31
43
serializer .Register (cmd )
44
+
32
45
decodedMsg , noErr := serializer .Decode (encodedBytes , schemaName )
33
46
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 )
35
48
}
36
49
37
50
cmdCopy , ok := decodedMsg .(* ProtoCommand )
38
-
39
51
if ! ok {
40
52
t .Errorf ("decoded message was of wrong type. expected:%v actual:%v" , reflect .TypeOf (cmd ), reflect .TypeOf (decodedMsg ))
41
53
}
@@ -47,3 +59,30 @@ func TestProtoSerialization(t *testing.T) {
47
59
}
48
60
49
61
}
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