|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | + |
| 8 | + log "github.com/sirupsen/logrus" |
| 9 | + "github.com/wework/grabbit/gbus/serialization" |
| 10 | +) |
| 11 | + |
| 12 | +func getProtoCommand() *ProtoCommand { |
| 13 | + return &ProtoCommand{ |
| 14 | + SomeNumber: 15, |
| 15 | + SomeData: "rhinof"} |
| 16 | +} |
| 17 | + |
| 18 | +func TestProtoSerialization(t *testing.T) { |
| 19 | + // so the tests logs do not get littered with log entries from serlializer |
| 20 | + log.SetLevel(log.PanicLevel) |
| 21 | + defer log.SetLevel(log.InfoLevel) |
| 22 | + logger := log.WithField("test", "proto_serialization") |
| 23 | + |
| 24 | + serializer := serialization.NewProtoSerializer(logger) |
| 25 | + |
| 26 | + name := serializer.Name() |
| 27 | + if name != "proto" { |
| 28 | + t.Fatalf("incorrect serializer name. expected:proto actual:%s", name) |
| 29 | + } |
| 30 | + cmd := getProtoCommand() |
| 31 | + schemaName := cmd.SchemaName() |
| 32 | + |
| 33 | + encodedBytes, encErr := serializer.Encode(cmd) |
| 34 | + if encErr != nil { |
| 35 | + t.Fatalf("encoding returned an error: %v", encErr) |
| 36 | + } |
| 37 | + |
| 38 | + //Calling Decode with out first registering the schema should fail and return an error |
| 39 | + _, decErr := serializer.Decode(encodedBytes, schemaName) |
| 40 | + if decErr == nil { |
| 41 | + t.Fatalf("decoding expected to fail but did not return an error") |
| 42 | + } |
| 43 | + |
| 44 | + serializer.Register(cmd) |
| 45 | + |
| 46 | + decodedMsg, noErr := serializer.Decode(encodedBytes, schemaName) |
| 47 | + if noErr != nil { |
| 48 | + t.Fatalf("decoding of message failed with error:%v", noErr) |
| 49 | + } |
| 50 | + |
| 51 | + cmdCopy, ok := decodedMsg.(*ProtoCommand) |
| 52 | + if !ok { |
| 53 | + t.Errorf("decoded message was of wrong type. expected:%v actual:%v", reflect.TypeOf(cmd), reflect.TypeOf(decodedMsg)) |
| 54 | + } |
| 55 | + |
| 56 | + if cmdCopy.SomeNumber != cmd.SomeNumber || cmdCopy.SomeData != cmd.SomeData { |
| 57 | + log.Infof("expected:%v\n", cmd) |
| 58 | + log.Infof("actual:%v\n", cmdCopy) |
| 59 | + t.Errorf("decoded message has unexpected or missing data") |
| 60 | + } |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +func TestProtoSerializationErrors(t *testing.T) { |
| 65 | + // so the tests logs do not get littered with log entries from serlializer |
| 66 | + log.SetLevel(log.PanicLevel) |
| 67 | + defer log.SetLevel(log.InfoLevel) |
| 68 | + logger := log.WithField("test", "proto_serialization") |
| 69 | + |
| 70 | + serializer := serialization.NewProtoSerializer(logger) |
| 71 | + |
| 72 | + // test that encoding a non protobuf generated strcut fails and returns an error |
| 73 | + _, encErr := serializer.Encode(Command1{}) |
| 74 | + if encErr == nil { |
| 75 | + t.Errorf("serializer expected to return an error for non proto generated messages but di not") |
| 76 | + } |
| 77 | + |
| 78 | + cmd := getProtoCommand() |
| 79 | + encodedBytes, encErr := serializer.Encode(cmd) |
| 80 | + if encErr != nil { |
| 81 | + t.Fatalf("encoding returned an error: %v", encErr) |
| 82 | + } |
| 83 | + |
| 84 | + //decoding an unregistered schema fails and returns an error |
| 85 | + if _, decErr := serializer.Decode(encodedBytes, "kong"); decErr == nil { |
| 86 | + t.Errorf("decoding an unregistred schema is expected to return an error but did not") |
| 87 | + } |
| 88 | + |
| 89 | + serializer.Register(cmd) |
| 90 | + //decoding junk fails and returns an error |
| 91 | + junk := make([]byte, 16) |
| 92 | + rand.Read(junk) |
| 93 | + if _, decErr := serializer.Decode(junk, cmd.SchemaName()); decErr == nil { |
| 94 | + t.Errorf("decoding junk is expected to return an error but did not") |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments