示例#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());
 }
示例#2
0
        public void DataContract_serialize_streamNotEmpty()
        {
            var serializer = new ProtobufObjectSerializer();
            var stream = new MemoryStream();
            var obj = new DataV1 { Prop1 = 1 };
            serializer.WriteObject(stream, obj);

            Assert.IsTrue(stream.Length > 0);
        }
示例#3
0
 public void DataContract_serializeDeserialize_theSame()
 {
     var serializer = new ProtobufObjectSerializer();
     var stream = new MemoryStream();
     var obj = new DataV1 { Prop1 = 1 };
     serializer.WriteObject(stream, obj);
     stream.Position = 0;
     var de = (DataV1)serializer.ReadObject(stream, typeof(DataV1));
     Assert.AreEqual(obj.Prop1, de.Prop1);
 }
示例#4
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);
 }
示例#5
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);
 }
示例#6
0
 public void Serialize_DataMembersWithoutOrder()
 {
     var serializer = new ProtobufObjectSerializer();
     var stream = new MemoryStream();
     var obj = new DataMembersWithoutOrder { Prop = "value", };
     serializer.WriteObject(stream, obj);
     Assert.AreEqual(0,stream.Length);
 }
示例#7
0
 public void SerializeMessage()
 {
     var req = new MessageRequest { Session = Guid.NewGuid().ToString() };
     var serializer = new ProtobufObjectSerializer();
     var stream = new MemoryStream();
     serializer.WriteObject(stream, req);
     Assert.IsTrue(stream.ToArray().Length > 0);
 }
示例#8
0
 public void SerializeNull()
 {
     var serializer = new ProtobufObjectSerializer();
     var stream = new MemoryStream();
     serializer.WriteObject(stream, null);
 }
示例#9
0
 public void SerializeList()
 {
     var serializer = new ProtobufObjectSerializer();
     var stream = new MemoryStream();
     serializer.WriteObject(stream, new List<string> { ":)" });
     Assert.IsTrue(stream.ToArray().Length > 0);
 }
示例#10
0
 public void SerializeGuid()
 {
     var serializer = new ProtobufObjectSerializer();
     var stream = new MemoryStream();
     serializer.WriteObject(stream, Guid.NewGuid());
     Assert.IsTrue(stream.ToArray().Length > 0);
 }
示例#11
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);
        }
示例#12
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);
 }
示例#13
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);
        }