Ejemplo n.º 1
0
        public void ConcurentSerialization()
        {
            var tasks = new List <Task>();
            var types = new[] { typeof(DataV1), typeof(DataV2), typeof(OtherDataV1), typeof(OtherDataV2) };

            for (int i = 0; i < 5; i++)
            {
                var task = new Task(() =>
                {
                    var serializer = new ProtobufObjectSerializer();
                    var childTasks = new List <Task>();
                    foreach (var type in types)
                    {
                        var childDask = new Task(() =>
                        {
                            var memoryStream = new MemoryStream();
                            serializer.WriteObject(memoryStream, Activator.CreateInstance(type));
                            serializer.ReadObject(memoryStream, type);
                        });
                        childTasks.Add(childDask);
                        childDask.Start();
                    }
                    Task.WaitAll(childTasks.ToArray());
                });
                tasks.Add(task);
                task.Start();
            }
            Task.WaitAll(tasks.ToArray());
        }
Ejemplo n.º 2
0
        public void NullListIsNull()
        {
            var ser    = new ProtobufObjectSerializer();
            var stream = new MemoryStream();

            ser.WriteObject(stream, new Data());
            stream.Position = 0;
            var data = ser.ReadObject(stream, typeof(Data)) as Data;

            Assert.IsTrue(data.List == null);
        }
Ejemplo n.º 3
0
        public void DeSerializeNull()
        {
            var serializer = new ProtobufObjectSerializer();
            var stream     = new MemoryStream();

            serializer.WriteObject(stream, null);
            stream.Position = 0;
            var deserialized = serializer.ReadObject(stream, typeof(object));

            Assert.IsNull(deserialized);
        }
Ejemplo n.º 4
0
        public void DataContract_serializeDeserializeOtherTypeSameSignature_theSame()
        {
            var serializer = new ProtobufObjectSerializer();
            var stream     = new MemoryStream();
            var obj        = new DataV1 {
                Prop1 = 1
            };

            serializer.WriteObject(stream, obj);
            stream.Position = 0;
            var de = (DataV2)serializer.ReadObject(stream, typeof(DataV2));

            Assert.AreEqual(obj.Prop1, de.Prop1);
        }
Ejemplo n.º 5
0
        public void EmptyListIsNull()
        {
            var ser    = new ProtobufObjectSerializer();
            var stream = new MemoryStream();

            ser.WriteObject(stream, new Data {
                List = new List <string>()
            });
            stream.Position = 0;

            var data = ser.ReadObject(stream, typeof(Data)) as Data;

            Assert.IsNull(data.List);
        }
Ejemplo n.º 6
0
        public void DeSerializeMessage()
        {
            var session = Guid.NewGuid().ToString();
            var req     = new MessageRequest {
                Session = session
            };
            var serializer = new ProtobufObjectSerializer();
            var stream     = new MemoryStream();

            serializer.WriteObject(stream, req);
            stream.Position = 0;
            var deserialized = (MessageRequest)serializer.ReadObject(stream, typeof(MessageRequest));

            Assert.AreEqual(session, deserialized.Session);
        }
Ejemplo n.º 7
0
        public void SerializeComplextObject()
        {
            var serializer = new ProtobufObjectSerializer();
            var stream     = new MemoryStream();
            var obj        = new ComplexObject {
                Prop1 = new DataV1 {
                    Prop1 = 1
                }, Prop2 = new DataV2 {
                    Prop1 = 2
                }
            };

            serializer.WriteObject(stream, obj);
            Assert.IsTrue(stream.ToArray().Length > 0);
            stream.Position = 0;
            var deserialized = (ComplexObject)serializer.ReadObject(stream, typeof(ComplexObject));

            Assert.IsNotNull(deserialized);
            Assert.IsNotNull(deserialized.Prop1);
            Assert.IsNotNull(deserialized.Prop2);
        }